From a66cfcac58ae8c9662776741d9727dfb6585fbcc Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Thu, 9 Sep 2021 13:26:56 +0200 Subject: WIP: byday ocurrences --- src/periodic.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/periodic.rs b/src/periodic.rs index b557ef7..19874e4 100644 --- a/src/periodic.rs +++ b/src/periodic.rs @@ -1,6 +1,6 @@ use std::fmt; use std::str::FromStr; -use std::collections::HashSet; +use std::collections::HashMap; use chrono::{Duration, Weekday}; @@ -8,6 +8,8 @@ use date::Date; use event::{Event, End}; use errors::EventError; +pub type Byday = HashMap>; + #[derive(Debug)] pub struct Periodic { pub event: Event, @@ -15,7 +17,7 @@ pub struct Periodic { pub interval: i64, pub count: Option, pub until: Option, - pub byday: Option>, + pub byday: Option, pub wkst: Weekday, } @@ -115,7 +117,7 @@ impl<'a> Iter<'a> { if weekday == p.wkst { days += 7 * (p.interval - 1); } - while !byday.contains(&weekday) { + while !byday.contains_key(&weekday) { weekday = weekday.succ(); days += 1; if weekday == p.wkst { @@ -127,6 +129,7 @@ impl<'a> Iter<'a> { } } Freq::Monthly => { + // TODO: byday... let new_date = if date.month() == 12 { date.with_month(1) .unwrap() @@ -137,6 +140,7 @@ impl<'a> Iter<'a> { }; new_date - date } + // TODO: byday... Freq::Yearly => date.with_year(date.year() + 1).unwrap() - date, } } @@ -170,10 +174,21 @@ impl FromStr for Freq { } } -fn parse_byday(s: &str) -> Result, EventError> { - let mut byday = HashSet::new(); +fn parse_byday(s: &str) -> Result { + let mut byday = Byday::new(); for v in s.split(",") { - byday.insert(parse_weekday(v)?); + let weekday = parse_weekday(&v[v.len() - 2..])?; + let occurrence = if v.len() > 2 { + v[..v.len() - 2].parse()? + } else { + 0 + }; + match byday.get_mut(&weekday) { + Some(occurrences) => occurrences.push(occurrence), + None => { + byday.insert(weekday, vec![occurrence]); + } + }; } Ok(byday) } -- cgit v1.2.3