diff options
author | Fredrik Meringdal <fredrikmeringdal@Fredriks-MacBook-Pro.local> | 2021-02-03 17:55:43 +0100 |
---|---|---|
committer | Fredrik Meringdal <fredrikmeringdal@Fredriks-MacBook-Pro.local> | 2021-02-03 17:55:43 +0100 |
commit | 61aac0d2773f7a073b63661ec150954414f5696e (patch) | |
tree | 739b82650bafd04cdd29ca7aa4f2af94c6afb1b8 /src/rrule.rs | |
parent | 4b12b6328955612dcb4a248a14d64864d9aecdf1 (diff) | |
download | rust_rrule-61aac0d2773f7a073b63661ec150954414f5696e.zip |
iterator for rrule
Diffstat (limited to 'src/rrule.rs')
-rw-r--r-- | src/rrule.rs | 59 |
1 files changed, 12 insertions, 47 deletions
diff --git a/src/rrule.rs b/src/rrule.rs index c2a080e..d372e06 100644 --- a/src/rrule.rs +++ b/src/rrule.rs @@ -1,4 +1,4 @@ -use crate::iter::iter; +use crate::iter::{generate, IterInfo}; use crate::options::*; use crate::rrule_iter::*; use crate::rrulestr::build_rrule; @@ -18,55 +18,25 @@ impl RRule { /// Returns all the recurrences of the rrule pub fn all(&mut self) -> Vec<DateTime<Tz>> { - let iter_args = IterArgs { - inc: true, - before: None, - after: None, - dt: None, - }; - let mut iter_res = RRuleIterRes::new(QueryMethodTypes::All, iter_args); - - let res = iter(&mut iter_res, &mut self.options); - res + 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(&mut self, dt: DateTime<Tz>, inc: bool) -> Option<DateTime<Tz>> { - let iter_args = IterArgs { - inc, - before: None, - after: None, - dt: Some(dt), - }; - let mut iter_res = RRuleIterRes::new(QueryMethodTypes::Before, iter_args); - - let recurrences = iter(&mut iter_res, &mut self.options); - if recurrences.is_empty() { - None - } else { - Some(recurrences[0]) - } + self.into_iter() + .take_while(|d| if inc { *d <= dt } else { *d < dt }) + .last() } /// Returns the last recurrence after 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 after(&mut self, dt: DateTime<Tz>, inc: bool) -> Option<DateTime<Tz>> { - let iter_args = IterArgs { - inc, - before: None, - after: None, - dt: Some(dt), - }; - let mut iter_res = RRuleIterRes::new(QueryMethodTypes::After, iter_args); - let recurrences = iter(&mut iter_res, &mut self.options); - if recurrences.is_empty() { - None - } else { - Some(recurrences[0]) - } + self.into_iter() + .skip_while(|d| if inc { *d <= dt } else { *d < dt }) + .next() } /// Returns all the recurrences of the rrule between after and before. @@ -79,15 +49,10 @@ impl RRule { before: DateTime<Tz>, inc: bool, ) -> Vec<DateTime<Tz>> { - let iter_args = IterArgs { - inc, - before: Some(before), - after: Some(after), - dt: None, - }; - let mut iter_res = RRuleIterRes::new(QueryMethodTypes::Between, iter_args); - - iter(&mut iter_res, &mut self.options) + self.into_iter() + .skip_while(|d| if inc { *d <= after } else { *d < after }) + .take_while(|d| if inc { *d <= before } else { *d < before }) + .collect() } } |