TimeUnit#

The unit in which a duration or lag value is counted. It's the type of SchedulingConfig::duration_unit (default TimeUnit::Day), TaskData::duration_unit, and the lag_unit field of LinkData, LinkAdd and LinkUpdate. Convert a string with TimeUnit::parse.

Declaration#

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeUnit {
    Minute,
    Hour,
    Day,
}

With the serde feature, TimeUnit implements Serialize and Deserialize, using the lowercase names "minute", "hour" and "day".

Variants#

Variant Description
Minute Values are counted in minutes.
Hour Values are counted in hours.
Day Values are counted in days.

The week, month and year units are reserved and not supported yet.

Associated functions#

parse#

pub fn parse(s: &str) -> Result<TimeUnit, String>

Converts a lowercase unit name to a TimeUnit.

Parameters

Name Type Description
s &str "minute", "hour" or "day". Matching is case-sensitive.

Returns - the matching variant.

Errors - Err(String) with a message when s is "week", "month" or "year" (reserved, not supported yet) or any other unknown name.

Methods#

as_str#

pub fn as_str(&self) -> &'static str

Returns the lowercase name of the unit: "minute", "hour" or "day". TimeUnit::parse accepts this name back.

Example#

use plen_core::{SchedulingConfig, TimeUnit};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let unit = TimeUnit::parse("hour")?;
    assert_eq!(unit.as_str(), "hour");
    assert!(TimeUnit::parse("week").is_err());

    let config = SchedulingConfig {
        duration_unit: unit,
        ..SchedulingConfig::default()
    };
    assert_eq!(config.duration_unit, TimeUnit::Hour);
    Ok(())
}