|
|
@ -8,6 +8,7 @@ use std::{
|
|
|
|
iter::FromIterator,
|
|
|
|
iter::FromIterator,
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Interval for job, in miliseconds.
|
|
|
|
/// Interval for job, in miliseconds.
|
|
|
|
#[derive(Debug,Clone,PartialEq,Eq,Hash,Ord,PartialOrd)]
|
|
|
|
#[derive(Debug,Clone,PartialEq,Eq,Hash,Ord,PartialOrd)]
|
|
|
|
pub enum Unit
|
|
|
|
pub enum Unit
|
|
|
@ -26,6 +27,7 @@ impl Unit
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// Multiplier to get to miliseconds
|
|
|
|
/// Multiplier to get to miliseconds
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
|
|
|
|
#[cfg(nightly)]
|
|
|
|
const fn multiplier(&self) -> u64 {
|
|
|
|
const fn multiplier(&self) -> u64 {
|
|
|
|
use Unit::*;
|
|
|
|
use Unit::*;
|
|
|
|
match self {
|
|
|
|
match self {
|
|
|
@ -38,6 +40,27 @@ impl Unit
|
|
|
|
Aeon => Year.multiplier() * 1000000,
|
|
|
|
Aeon => Year.multiplier() * 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(nightly))]
|
|
|
|
|
|
|
|
fn multiplier(&self) -> u64 {
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Unit::*;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
|
|
|
|
static ref MUL_TABLE: HashMap<Unit, u64> = {
|
|
|
|
|
|
|
|
let mut map = HashMap::with_capacity(8);
|
|
|
|
|
|
|
|
map.insert(Second, 1000);
|
|
|
|
|
|
|
|
map.insert(Minute, Second.multiplier() * 60);
|
|
|
|
|
|
|
|
map.insert(Hour, Minute.multiplier() * 60);
|
|
|
|
|
|
|
|
map.insert(Day, Hour.multiplier() * 24);
|
|
|
|
|
|
|
|
map.insert(Week, Day.multiplier() * 7);
|
|
|
|
|
|
|
|
map.insert(Year, Week.multiplier() * 52);
|
|
|
|
|
|
|
|
map.insert(Aeon, Year.multiplier() * 1000000);
|
|
|
|
|
|
|
|
map
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*MUL_TABLE.get(&self).unwrap()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// A time object parsed from the definition file.
|
|
|
|
/// A time object parsed from the definition file.
|
|
|
@ -68,7 +91,10 @@ impl Time
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fn into_abs(mut self) -> Result<Self, ParseError>
|
|
|
|
fn into_abs(mut self) -> Result<Self, ParseError>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
self.absolute = Some(NonZeroU64::try_from(self.unit.multiplier().checked_mul(u64::from(self.value)).ok_or(ParseError::InvalidNumber(None))?)?);
|
|
|
|
self.absolute = Some(NonZeroU64::new(self.unit.multiplier()
|
|
|
|
|
|
|
|
.checked_mul(u64::from(self.value))
|
|
|
|
|
|
|
|
.ok_or(ParseError::nz_err())?)
|
|
|
|
|
|
|
|
.ok_or(ParseError::nz_err())?);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -92,7 +118,7 @@ impl Time
|
|
|
|
/// If `value` is less than 1 second (1000ms)
|
|
|
|
/// If `value` is less than 1 second (1000ms)
|
|
|
|
pub fn from_ms(value: NonZeroU64) -> Self
|
|
|
|
pub fn from_ms(value: NonZeroU64) -> Self
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let value_sec = NonZeroU64::try_from(u64::from(value) / 1000).expect("absolute `value` is too small for `Time`");
|
|
|
|
let value_sec = NonZeroU64::new(u64::from(value) / 1000).expect("absolute `value` is too small for `Time`");
|
|
|
|
Self{
|
|
|
|
Self{
|
|
|
|
unit: Unit::Second,
|
|
|
|
unit: Unit::Second,
|
|
|
|
value: value_sec,
|
|
|
|
value: value_sec,
|
|
|
@ -101,9 +127,9 @@ impl Time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Attempt to create a new `Time` from miliseconds.
|
|
|
|
/// Attempt to create a new `Time` from miliseconds.
|
|
|
|
pub fn try_from_ms(value: NonZeroU64) -> Result<Self, num::TryFromIntError>
|
|
|
|
pub fn try_from_ms(value: NonZeroU64) -> Result<Self, ParseError>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let value_sec = NonZeroU64::try_from(u64::from(value) / 1000)?;
|
|
|
|
let value_sec = NonZeroU64::new(u64::from(value) / 1000).ok_or(ParseError::nz_err())?;
|
|
|
|
Ok(Self{
|
|
|
|
Ok(Self{
|
|
|
|
unit: Unit::Second,
|
|
|
|
unit: Unit::Second,
|
|
|
|
value: value_sec,
|
|
|
|
value: value_sec,
|
|
|
@ -301,6 +327,15 @@ impl From<num::TryFromIntError> for ParseError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl ParseError
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Non-zero conversion error
|
|
|
|
|
|
|
|
const fn nz_err() -> Self
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Self::InvalidNumber(None)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<num::ParseIntError> for ParseError
|
|
|
|
impl From<num::ParseIntError> for ParseError
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[allow(unused_variables)]
|
|
|
|