| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 8 | | pub struct Cron { |
| 9 | | minute: Vec<bool>, |
| 10 | | hour: Vec<bool>, |
| 11 | | day: Vec<bool>, |
| 12 | | month: Vec<bool>, |
| 13 | | weekday: Vec<bool>, |
| 14 | | |
| 15 | | either_day: bool, |
| 16 | | } |
| 17 | |
|
| 18 | 64× | fn field(text: &str, lo: u32, hi: u32) -> Option<(Vec<bool>, bool)> { |
| 19 | 64× | let mut allowed = vec![false; hi as usize + 1]; |
| 20 | 64× | let star = text == "*"; |
| 21 | 64× | for part in text.split(',') { |
| 22 | 64× | let (range, step) = match part.split_once('/') { |
| 23 | 2× | Some((r, s)) => (r, s.parse::<u32>().ok().filter(|s| *s > 0)?), |
| 24 | 62× | None => (part, 1), |
| 25 | | }; |
| 26 | 63× | let (a, b) = if range == "*" { |
| 27 | 33× | (lo, hi) |
| 28 | 30× | } else if let Some((a, b)) = range.split_once('-') { |
| 29 | 3× | (a.parse().ok()?, b.parse().ok()?) |
| 30 | | } else { |
| 31 | 27× | let a: u32 = range.parse().ok()?; |
| 32 | | |
| 33 | 26× | (a, if part.contains('/') { hi } else { a }) |
| 34 | | }; |
| 35 | 62× | if a < lo || b > hi || a > b { |
| 36 | 4× | return None; |
| 37 | 58× | } |
| 38 | 58× | let mut v = a; |
| 39 | 749× | while v <= b { |
| 40 | 691× | allowed[v as usize] = true; |
| 41 | 691× | v += step; |
| 42 | 691× | } |
| 43 | | } |
| 44 | 58× | Some((allowed, star)) |
| 45 | 64× | } |
| 46 | |
|
| 47 | | impl Cron { |
| 48 | 20× | pub fn parse(line: &str) -> Option<Cron> { |
| 49 | 20× | let f: Vec<&str> = line.split_whitespace().collect(); |
| 50 | 20× | let [m, h, d, mo, w] = f.as_slice() else { return None }; |
| 51 | 17× | let (minute, _) = field(m, 0, 59)?; |
| 52 | 13× | let (hour, _) = field(h, 0, 23)?; |
| 53 | 12× | let (day, day_star) = field(d, 1, 31)?; |
| 54 | 11× | let (month, _) = field(mo, 1, 12)?; |
| 55 | 11× | let (mut weekday, week_star) = field(w, 0, 7)?; |
| 56 | 11× | if weekday[7] { |
| 57 | 9× | weekday[0] = true; |
| 58 | 9× | } |
| 59 | 11× | Some(Cron { minute, hour, day, month, weekday, either_day: !day_star && !week_star }) |
| 60 | 20× | } |
| 61 | |
|
| 62 | | |
| 63 | 10× | pub fn matches(&self, minute: u32, hour: u32, day: u32, month: u32, weekday: u32) -> bool { |
| 64 | 44× | let get = |v: &Vec<bool>, i: u32| v.get(i as usize).copied().unwrap_or(false); |
| 65 | 10× | let (d, w) = (get(&self.day, day), get(&self.weekday, weekday % 7)); |
| 66 | 10× | let days = if self.either_day { d || w } else { d && w }; |
| 67 | 10× | get(&self.minute, minute) && get(&self.hour, hour) && get(&self.month, month) && days |
| 68 | 10× | } |
| 69 | | } |
| 70 | |
|
| 71 | | #[cfg(test)] |
| 72 | | mod tests { |
| 73 | | use super::Cron; |
| 74 | |
|
| 75 | | #[test] |
| 76 | 1× | fn lines_parse_and_match_as_cron_does() { |
| 77 | 1× | let nightly = Cron::parse("0 3 * * *").unwrap(); |
| 78 | 1× | assert!(nightly.matches(0, 3, 15, 6, 2)); |
| 79 | 1× | assert!(!nightly.matches(1, 3, 15, 6, 2)); |
| 80 | 1× | let quarter = Cron::parse("*/15 9-17 * * 1-5").unwrap(); |
| 81 | 1× | assert!(quarter.matches(45, 17, 2, 10, 5)); |
| 82 | 1× | assert!(!quarter.matches(50, 17, 2, 10, 5)); |
| 83 | 1× | assert!(!quarter.matches(0, 12, 3, 10, 6), "Saturday"); |
| 84 | 1× | let sunday = Cron::parse("30 4 * * 7").unwrap(); |
| 85 | 1× | assert!(sunday.matches(30, 4, 4, 10, 0)); |
| 86 | 1× | let either = Cron::parse("0 0 1 * 1").unwrap(); |
| 87 | 1× | assert!(either.matches(0, 0, 1, 10, 3) && either.matches(0, 0, 5, 10, 1), "the 1st or a Monday"); |
| 88 | 8× | for bad in ["", "* * * *", "60 * * * *", "* 24 * * *", "* * 0 * *", "*/0 * * * *", "a * * * *", "5-1 * * * *"] { |
| 89 | 8× | assert!(Cron::parse(bad).is_none(), "{bad:?}"); |
| 90 | | } |
| 91 | 1× | } |
| 92 | | } |