diff options
Diffstat (limited to 'src/model/repetition.rs')
-rw-r--r-- | src/model/repetition.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/model/repetition.rs b/src/model/repetition.rs index 872944a..eb8cb6d 100644 --- a/src/model/repetition.rs +++ b/src/model/repetition.rs @@ -6,6 +6,7 @@ use std::collections::HashSet; pub struct Repetition { pub frequency: Frequency, pub removed_occurences: HashSet<usize>, + pub until: Option<NaiveDate>, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] @@ -48,6 +49,7 @@ impl Repetition { let repeat = |mut date, next: Box<dyn Fn(NaiveDate) -> NaiveDate>| { let mut repetitions = vec![]; let mut iteration: usize = 0; + let end = self.until.unwrap_or(end); while date <= end { if date >= event { if date >= start && !self.removed_occurences.contains(&iteration) { @@ -195,6 +197,7 @@ mod tests { let repetition = Repetition { frequency: Frequency::Daily { period: 2 }, removed_occurences: HashSet::from([0, 2, 3]), + until: None, }; assert_eq!( repetition.between(d(2020, 7, 1), d(2020, 7, 1), d(2020, 7, 9)), @@ -209,6 +212,7 @@ mod tests { day: DayOfMonth::Day { day: 8 }, }, removed_occurences: HashSet::from([1, 3]), + until: None, }; assert_eq!( repetition.between(d(2020, 1, 8), d(2020, 1, 8), d(2020, 4, 8)), @@ -225,6 +229,7 @@ mod tests { }, }, removed_occurences: HashSet::from([1, 2, 3]), + until: None, }; assert_eq!( repetition.between(d(2020, 2, 1), d(2020, 2, 1), d(2020, 7, 1)), @@ -237,6 +242,7 @@ mod tests { let repetition = Repetition { frequency: Frequency::Yearly, removed_occurences: HashSet::from([3]), + until: None, }; assert_eq!( repetition.between(d(2018, 5, 5), d(2019, 8, 1), d(2022, 5, 5)), @@ -249,6 +255,7 @@ mod tests { let repetition = Repetition { frequency: Frequency::Yearly, removed_occurences: HashSet::from([1]), + until: None, }; assert_eq!( repetition.occurence_index(d(2020, 1, 1), d(2022, 1, 1)), @@ -256,6 +263,19 @@ mod tests { ) } + #[test] + fn repetition_stops_after_until() { + let repetition = Repetition { + frequency: Frequency::Yearly, + removed_occurences: HashSet::new(), + until: Some(d(2022, 1, 1)), + }; + assert_eq!( + repetition.between(d(2020, 1, 1), d(2020, 1, 1), d(2024, 1, 1)), + vec!(d(2020, 1, 1), d(2021, 1, 1), d(2022, 1, 1)) + ) + } + fn d(y: i32, m: u32, d: u32) -> NaiveDate { NaiveDate::from_ymd(y, m, d) } @@ -264,6 +284,7 @@ mod tests { Repetition { frequency, removed_occurences: HashSet::new(), + until: None, } } } |