diff options
-rw-r--r-- | src/iter/iterinfo.rs | 8 | ||||
-rw-r--r-- | src/rrule.rs | 11 | ||||
-rw-r--r-- | src/rrule_iter.rs | 34 | ||||
-rw-r--r-- | src/rruleset.rs | 11 | ||||
-rw-r--r-- | src/rruleset_iter.rs | 38 |
5 files changed, 49 insertions, 53 deletions
diff --git a/src/iter/iterinfo.rs b/src/iter/iterinfo.rs index 1a1a283..35b769f 100644 --- a/src/iter/iterinfo.rs +++ b/src/iter/iterinfo.rs @@ -5,15 +5,15 @@ use crate::iter::yearinfo::{rebuild_year, YearInfo}; use crate::options::{Frequenzy, ParsedOptions}; use chrono::prelude::*; -pub struct IterInfo { +pub struct IterInfo<'a> { pub yearinfo: Option<YearInfo>, pub monthinfo: Option<MonthInfo>, pub eastermask: Option<Vec<isize>>, - pub options: ParsedOptions, + pub options: &'a ParsedOptions, } -impl IterInfo { - pub fn new(options: ParsedOptions) -> Self { +impl<'a> IterInfo<'a> { + pub fn new(options: &'a ParsedOptions) -> Self { let mut ii = Self { options, yearinfo: None, diff --git a/src/rrule.rs b/src/rrule.rs index 7ac6016..1d95fe9 100644 --- a/src/rrule.rs +++ b/src/rrule.rs @@ -16,15 +16,14 @@ impl RRule { /// Returns all the recurrences of the rrule pub fn all(&self) -> Vec<DateTime<Tz>> { - self.clone().into_iter().collect() + self.into_iter().collect() } /// Returns the last recurrence before the given datetime instance. /// The inc keyword defines what happens if dt is an recurrence. /// With inc == true, if dt itself is an recurrence, it will be returned. pub fn before(&self, dt: DateTime<Tz>, inc: bool) -> Option<DateTime<Tz>> { - self.clone() - .into_iter() + self.into_iter() .take_while(|d| if inc { *d <= dt } else { *d < dt }) .last() } @@ -33,8 +32,7 @@ impl RRule { /// The inc keyword defines what happens if dt is an recurrence. /// With inc == true, if dt itself is an recurrence, it will be returned. pub fn after(&self, dt: DateTime<Tz>, inc: bool) -> Option<DateTime<Tz>> { - self.clone() - .into_iter() + self.into_iter() .skip_while(|d| if inc { *d <= dt } else { *d < dt }) .next() } @@ -49,8 +47,7 @@ impl RRule { before: DateTime<Tz>, inc: bool, ) -> Vec<DateTime<Tz>> { - self.clone() - .into_iter() + self.into_iter() .skip_while(|d| if inc { *d <= after } else { *d < after }) .take_while(|d| if inc { *d <= before } else { *d < before }) .collect() diff --git a/src/rrule_iter.rs b/src/rrule_iter.rs index 59f9cd7..27d5e90 100644 --- a/src/rrule_iter.rs +++ b/src/rrule_iter.rs @@ -4,30 +4,30 @@ use crate::iter::{ use crate::{datetime::from_ordinal, RRule}; use crate::{datetime::Time, Frequenzy}; use chrono::{prelude::*, Duration}; -use chrono_tz::Tz; -use chrono_tz::UTC; +use chrono_tz::{Tz, UTC}; use std::collections::VecDeque; const MAX_YEAR: i32 = 9999; -pub struct RRuleIter { +pub struct RRuleIter<'a> { pub counter_date: DateTime<Tz>, - pub ii: IterInfo, + pub ii: IterInfo<'a>, pub timeset: Vec<Time>, // Buffer of datetimes not yet yielded pub buffer: VecDeque<DateTime<Tz>>, pub finished: bool, + pub count: Option<u32>, } -impl RRuleIter { +impl<'a> RRuleIter<'a> { pub fn generate(&mut self) -> bool { - let options = self.ii.options.clone(); + let options = self.ii.options; if options.interval == 0 { return true; } - match options.count { + match self.count { Some(count) if count == 0 => return true, _ => (), }; @@ -71,9 +71,10 @@ impl RRuleIter { if res >= options.dtstart { self.buffer.push_back(res); - if let Some(count) = self.ii.options.count { + if let Some(count) = self.count { if count > 0 { - self.ii.options.count = Some(count - 1); + self.count = Some(count - 1); + // self.ii.options.count = Some(count - 1); } // This means that the real count is 0, because of the decrement above if count == 1 { @@ -108,9 +109,10 @@ impl RRuleIter { if res >= options.dtstart { self.buffer.push_back(res); - if let Some(count) = self.ii.options.count { + if let Some(count) = self.count { if count > 0 { - self.ii.options.count = Some(count - 1); + self.count = Some(count - 1); + // self.ii.options.count = Some(count - 1); } // This means that the real count is 0, because of the decrement above if count == 1 { @@ -152,7 +154,7 @@ impl RRuleIter { } } -impl Iterator for RRuleIter { +impl<'a> Iterator for RRuleIter<'a> { type Item = DateTime<Tz>; fn next(&mut self) -> Option<Self::Item> { @@ -173,17 +175,18 @@ impl Iterator for RRuleIter { } } -impl IntoIterator for RRule { +impl<'a> IntoIterator for &'a RRule { type Item = DateTime<Tz>; - type IntoIter = RRuleIter; + type IntoIter = RRuleIter<'a>; fn into_iter(self) -> Self::IntoIter { - let ii = IterInfo::new(self.options); + let ii = IterInfo::new(&self.options); let counter_date = ii.options.dtstart; // ii.rebuild(counter_date.year() as isize, counter_date.month() as usize); let timeset = make_timeset(&ii, &counter_date, &ii.options); + let count = ii.options.count.clone(); RRuleIter { counter_date, @@ -191,6 +194,7 @@ impl IntoIterator for RRule { timeset, buffer: VecDeque::new(), finished: false, + count, } } } diff --git a/src/rruleset.rs b/src/rruleset.rs index c194744..d10cc3a 100644 --- a/src/rruleset.rs +++ b/src/rruleset.rs @@ -44,15 +44,14 @@ impl RRuleSet { /// Returns all the recurrences of the rruleset pub fn all(&self) -> Vec<DateTime<Tz>> { - self.clone().into_iter().collect() + self.into_iter().collect() } /// Returns the last recurrence before the given datetime instance. /// The inc keyword defines what happens if dt is an recurrence. /// With inc == true, if dt itself is an recurrence, it will be returned. pub fn before(&self, dt: DateTime<Tz>, inc: bool) -> Option<DateTime<Tz>> { - self.clone() - .into_iter() + self.into_iter() .take_while(|d| if inc { *d <= dt } else { *d < dt }) .last() } @@ -61,8 +60,7 @@ impl RRuleSet { /// The inc keyword defines what happens if dt is an recurrence. /// With inc == true, if dt itself is an recurrence, it will be returned. pub fn after(&self, dt: DateTime<Tz>, inc: bool) -> Option<DateTime<Tz>> { - self.clone() - .into_iter() + self.into_iter() .skip_while(|d| if inc { *d <= dt } else { *d < dt }) .next() } @@ -77,8 +75,7 @@ impl RRuleSet { before: DateTime<Tz>, inc: bool, ) -> Vec<DateTime<Tz>> { - self.clone() - .into_iter() + self.into_iter() .skip_while(|d| if inc { *d <= after } else { *d < after }) .take_while(|d| if inc { *d <= before } else { *d < before }) .collect() diff --git a/src/rruleset_iter.rs b/src/rruleset_iter.rs index 7b83f39..c0bf047 100644 --- a/src/rruleset_iter.rs +++ b/src/rruleset_iter.rs @@ -5,16 +5,16 @@ use chrono_tz::Tz; use std::collections::HashMap; use std::iter::Iterator; -pub struct RRuleSetIter { +pub struct RRuleSetIter<'a> { pub queue: HashMap<usize, DateTime<Tz>>, - pub rrule_iters: Vec<RRuleIter>, - pub exrules: Vec<RRule>, + pub rrule_iters: Vec<RRuleIter<'a>>, + pub exrules: &'a Vec<RRule>, pub exdates: HashMap<i64, ()>, // Sorted additional dates in decreasing order pub rdates: Vec<DateTime<Tz>>, } -impl Iterator for RRuleSetIter { +impl<'a> Iterator for RRuleSetIter<'a> { type Item = DateTime<Tz>; fn next(&mut self) -> Option<Self::Item> { @@ -52,7 +52,7 @@ impl Iterator for RRuleSetIter { } // TODO: RDates should be prefiltered before starting iteration - match generate_date(&mut self.rdates, &mut self.exrules, &mut self.exdates) { + match generate_date(&mut self.rdates, &self.exrules, &mut self.exdates) { Some(first_rdate) => { let next_date = match next_date { Some(next_date) => { @@ -79,7 +79,7 @@ impl Iterator for RRuleSetIter { fn generate_date( dates: &mut Vec<DateTime<Tz>>, - exrules: &mut Vec<RRule>, + exrules: &Vec<RRule>, exdates: &mut HashMap<i64, ()>, ) -> Option<DateTime<Tz>> { if dates.is_empty() { @@ -99,7 +99,7 @@ fn generate_date( fn generate( rrule_iter: &mut RRuleIter, - exrules: &mut Vec<RRule>, + exrules: &Vec<RRule>, exdates: &mut HashMap<i64, ()>, ) -> Option<DateTime<Tz>> { let mut date = rrule_iter.next(); @@ -112,7 +112,7 @@ fn generate( fn accept_generated_date( date: &Option<DateTime<Tz>>, - exrules: &mut Vec<RRule>, + exrules: &Vec<RRule>, exdates: &mut HashMap<i64, ()>, ) -> bool { match date { @@ -123,7 +123,7 @@ fn accept_generated_date( if !exrules.is_empty() { let after = date.timezone().timestamp(dt - 1, 0); let before = date.timezone().timestamp(dt + 1, 0); - for exrule in exrules.iter_mut() { + for exrule in exrules { for date in exrule.between(after, before, true) { exdates.insert(date.timestamp(), ()); } @@ -139,23 +139,21 @@ fn accept_generated_date( } } -impl IntoIterator for RRuleSet { +impl<'a> IntoIterator for &'a RRuleSet { type Item = DateTime<Tz>; - type IntoIter = RRuleSetIter; + type IntoIter = RRuleSetIter<'a>; - fn into_iter(mut self) -> Self::IntoIter { + fn into_iter(self) -> Self::IntoIter { // Sort in decreasing order - self.rdate.sort_by(|d1, d2| d2.partial_cmp(d1).unwrap()); + let mut rdates_sorted = self.rdate.clone(); + rdates_sorted.sort_by(|d1, d2| d2.partial_cmp(d1).unwrap()); + RRuleSetIter { queue: Default::default(), - rrule_iters: self - .rrule - .into_iter() - .map(|rrule| rrule.into_iter()) - .collect(), - rdates: self.rdate, - exrules: self.exrule, + rrule_iters: self.rrule.iter().map(|rrule| rrule.into_iter()).collect(), + rdates: rdates_sorted, + exrules: &self.exrule, exdates: self .exdate .iter() |