From 36cd29b459bb41bff1cf6d079fd30100bd89cec1 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 20 Jan 2024 22:08:44 +0100 Subject: Allow to specify week of month for repetition --- src/model/repetition.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/model/repetition.rs') diff --git a/src/model/repetition.rs b/src/model/repetition.rs index 360cf5f..07fc4d4 100644 --- a/src/model/repetition.rs +++ b/src/model/repetition.rs @@ -19,7 +19,7 @@ pub enum Frequency { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub enum DayOfMonth { Day { day: u8 }, - Weekday { weekday: Weekday }, + Weekday { week: u8, day: Weekday }, } pub fn validate_period(str: &str) -> Result { @@ -74,10 +74,10 @@ impl Repetition { None => vec![], }, Frequency::Monthly { - day: DayOfMonth::Weekday { weekday }, + day: DayOfMonth::Weekday { week, day }, } => repeat( - first_weekday_of_month(event, weekday), - Box::new(|d| first_weekday_of_month(next_month(d), weekday)), + day_of_month(event, week, day), + Box::new(|d| day_of_month(next_month(d), week, day)), ), Frequency::Yearly => repeat( // TODO: error handling @@ -97,9 +97,9 @@ impl Repetition { } } -fn first_weekday_of_month(date: NaiveDate, weekday: Weekday) -> NaiveDate { +fn day_of_month(date: NaiveDate, week: u8, day: Weekday) -> NaiveDate { // TODO: error handling - NaiveDate::from_weekday_of_month_opt(date.year(), date.month(), weekday, 1).unwrap() + NaiveDate::from_weekday_of_month_opt(date.year(), date.month(), day, week).unwrap() } fn next_month(date: NaiveDate) -> NaiveDate { @@ -177,7 +177,8 @@ mod tests { fn weekday_of_month() { let repetition = from_freq(Frequency::Monthly { day: DayOfMonth::Weekday { - weekday: Weekday::Tue, + week: 1, + day: Weekday::Tue, }, }); assert_eq!( @@ -228,7 +229,8 @@ mod tests { let repetition = Repetition { frequency: Frequency::Monthly { day: DayOfMonth::Weekday { - weekday: Weekday::Fri, + week: 1, + day: Weekday::Fri, }, }, removed_occurences: HashSet::from([1, 2, 3]), -- cgit v1.2.3