diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/calendar.rs | 6 | ||||
-rw-r--r-- | src/periodic.rs | 57 |
2 files changed, 41 insertions, 22 deletions
diff --git a/src/calendar.rs b/src/calendar.rs index ab465ea..a835480 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -65,7 +65,11 @@ impl Calendar { pub fn get(&self, first: &Date, last: &Date) -> Vec<Event> { let mut events = event::get(&self.single, first, last).to_vec(); for p in &self.periodic { - events.append(&mut p.get(first, last)); + let mut p_events = p.iter() + .skip_while(|e| e.end_date() < *first) + .take_while(|e| e.start <= *last) + .collect(); + events.append(&mut p_events); } events.sort_by_key(|k| k.start); events diff --git a/src/periodic.rs b/src/periodic.rs index 0c52a9b..d69b817 100644 --- a/src/periodic.rs +++ b/src/periodic.rs @@ -49,29 +49,44 @@ impl Periodic { Ok(()) } - pub fn get(&self, first: &Date, last: &Date) -> Vec<Event> { - let mut start = self.event.start; - let mut end = self.event.end_date(); - let mut events = Vec::new(); - let mut count = 0; - while start <= *last { - if (self.until.is_some() && start <= self.until.unwrap()) || - (self.count.is_some() && count >= self.count.unwrap()) - { - break; - } + pub fn iter<'a>(&'a self) -> Iter<'a> { + Iter { + periodic: self, + start: self.event.start, + end: self.event.end_date(), + count: 0, + } + } +} - if start >= *first { - let mut e = self.event.clone(); - e.start = start; - e.end = End::Date(end); - events.push(e); - count += count; - } - start = self.freq.next_date(start, self.interval); - end = self.freq.next_date(end, self.interval); +pub struct Iter<'a> { + periodic: &'a Periodic, + start: Date, + end: Date, + count: i64, +} + +impl<'a> Iterator for Iter<'a> { + type Item = Event; + + fn next(&mut self) -> Option<Self::Item> { + let p = self.periodic; + + if (p.until.is_some() && self.start <= p.until.unwrap()) || + (p.count.is_some() && self.count >= p.count.unwrap()) + { + return None; } - events + + let mut event = p.event.clone(); + event.start = self.start; + event.end = End::Date(self.end); + + self.count += 1; + self.start = p.freq.next_date(self.start, p.interval); + self.end = p.freq.next_date(self.end, p.interval); + + Some(event) } } |