cron/time_unit/
months.rs

1use crate::error::*;
2use crate::ordinal::{Ordinal, OrdinalSet};
3use crate::time_unit::TimeUnitField;
4use once_cell::sync::Lazy;
5use std::borrow::Cow;
6
7static ALL: Lazy<OrdinalSet> = Lazy::new(Months::supported_ordinals);
8
9#[derive(Clone, Debug, Eq)]
10pub struct Months {
11    ordinals: Option<OrdinalSet>,
12}
13
14impl TimeUnitField for Months {
15    fn from_optional_ordinal_set(ordinal_set: Option<OrdinalSet>) -> Self {
16        Months {
17            ordinals: ordinal_set,
18        }
19    }
20    fn name() -> Cow<'static, str> {
21        Cow::from("Months")
22    }
23    fn inclusive_min() -> Ordinal {
24        1
25    }
26    fn inclusive_max() -> Ordinal {
27        12
28    }
29    fn ordinal_from_name(name: &str) -> Result<Ordinal, Error> {
30        //TODO: Use phf crate
31        let ordinal = match name.to_lowercase().as_ref() {
32            "jan" | "january" => 1,
33            "feb" | "february" => 2,
34            "mar" | "march" => 3,
35            "apr" | "april" => 4,
36            "may" => 5,
37            "jun" | "june" => 6,
38            "jul" | "july" => 7,
39            "aug" | "august" => 8,
40            "sep" | "september" => 9,
41            "oct" | "october" => 10,
42            "nov" | "november" => 11,
43            "dec" | "december" => 12,
44            _ => {
45                return Err(
46                    ErrorKind::Expression(format!("'{}' is not a valid month name.", name)).into(),
47                )
48            }
49        };
50        Ok(ordinal)
51    }
52    fn ordinals(&self) -> &OrdinalSet {
53        match &self.ordinals {
54            Some(ordinal_set) => ordinal_set,
55            None => &ALL,
56        }
57    }
58}
59
60impl PartialEq for Months {
61    fn eq(&self, other: &Months) -> bool {
62        self.ordinals() == other.ordinals()
63    }
64}