diff --git a/rrule/examples/manual_iter.rs b/rrule/examples/manual_iter.rs index 607ff52..edafcca 100644 --- a/rrule/examples/manual_iter.rs +++ b/rrule/examples/manual_iter.rs @@ -13,6 +13,14 @@ fn main() { let iter = rrule.into_iter(); + // Or: + // + // let iter: RRuleSetIter = "DTSTART;TZID=America/New_York:20200902T130000\n\ + // RRULE:FREQ=Weekly" + // .parse() + // .expect("The RRule is not valid"); + // + for next in iter.take(50) { if next.year() == 2021 { println!("These are all the weeks before 2021."); diff --git a/rrule/src/iter/iterinfo.rs b/rrule/src/iter/iterinfo.rs index d2976d7..1110e38 100644 --- a/rrule/src/iter/iterinfo.rs +++ b/rrule/src/iter/iterinfo.rs @@ -7,21 +7,21 @@ use crate::{Frequency, NWeekday, RRule}; use chrono::{Datelike, NaiveTime, TimeZone}; #[derive(Debug, Clone)] -pub(crate) struct IterInfo<'a> { +pub(crate) struct IterInfo { year_info: YearInfo, month_info: Option, easter_mask: Option>, - rrule: &'a RRule, + rrule: RRule, } -impl<'a> IterInfo<'a> { - pub fn new(rrule: &'a RRule, dt_start: &DateTime) -> Self { +impl IterInfo { + pub fn new(rrule: &RRule, dt_start: &DateTime) -> Self { let year = dt_start.year(); let month = get_month(dt_start); let year_info = YearInfo::new(year, rrule); let mut ii = Self { - rrule, + rrule: rrule.clone(), year_info, month_info: None, easter_mask: None, @@ -35,7 +35,7 @@ impl<'a> IterInfo<'a> { if !skip_year_info && !matches!(&self.month_info, Some(month_info) if month_info.last_year == year) { - self.year_info = YearInfo::new(year, self.rrule); + self.year_info = YearInfo::new(year, &self.rrule); } let contains_nth_by_weekday = self @@ -47,7 +47,7 @@ impl<'a> IterInfo<'a> { if contains_nth_by_weekday && !(matches!(&self.month_info, Some(month_info) if month_info.last_month == month && month_info.last_year == year)) { - let new_month_info = MonthInfo::new(&self.year_info, month, self.rrule); + let new_month_info = MonthInfo::new(&self.year_info, month, &self.rrule); self.month_info = Some(new_month_info); } @@ -269,6 +269,6 @@ impl<'a> IterInfo<'a> { } pub fn rrule(&self) -> &RRule { - self.rrule + &self.rrule } } diff --git a/rrule/src/iter/rrule_iter.rs b/rrule/src/iter/rrule_iter.rs index 3c36c82..34cb97b 100644 --- a/rrule/src/iter/rrule_iter.rs +++ b/rrule/src/iter/rrule_iter.rs @@ -8,10 +8,10 @@ use chrono::{NaiveTime, TimeZone}; use std::collections::VecDeque; #[derive(Debug, Clone)] -pub(crate) struct RRuleIter<'a> { +pub(crate) struct RRuleIter { /// Date the iterator is currently at. pub(crate) counter_date: DateTimeIter, - pub(crate) ii: IterInfo<'a>, + pub(crate) ii: IterInfo, pub(crate) timeset: Vec, pub(crate) dt_start: DateTime, /// Buffer of datetimes is not yet yielded @@ -28,8 +28,8 @@ pub(crate) struct RRuleIter<'a> { pub(crate) was_limited: bool, } -impl<'a> RRuleIter<'a> { - pub(crate) fn new(rrule: &'a RRule, dt_start: &DateTime, limited: bool) -> Self { +impl RRuleIter { + pub(crate) fn new(rrule: &RRule, dt_start: &DateTime, limited: bool) -> Self { let ii = IterInfo::new(rrule, dt_start); let hour = get_hour(dt_start); @@ -204,7 +204,7 @@ impl<'a> RRuleIter<'a> { } } -impl<'a> Iterator for RRuleIter<'a> { +impl Iterator for RRuleIter { type Item = DateTime; fn next(&mut self) -> Option { @@ -229,7 +229,7 @@ pub(crate) trait WasLimited { fn was_limited(&self) -> bool; } -impl<'a> WasLimited for RRuleIter<'a> { +impl WasLimited for RRuleIter { fn was_limited(&self) -> bool { self.was_limited } diff --git a/rrule/src/iter/rruleset_iter.rs b/rrule/src/iter/rruleset_iter.rs index b777a73..3178f62 100644 --- a/rrule/src/iter/rruleset_iter.rs +++ b/rrule/src/iter/rruleset_iter.rs @@ -1,23 +1,25 @@ use super::rrule_iter::WasLimited; use super::{rrule_iter::RRuleIter, MAX_ITER_LOOP}; +use crate::RRuleError; use crate::{core::DateTime, RRuleSet}; use std::collections::BTreeSet; +use std::str::FromStr; use std::{collections::HashMap, iter::Iterator}; #[derive(Debug, Clone)] /// Iterator over all the dates in an [`RRuleSet`]. -pub struct RRuleSetIter<'a> { +pub struct RRuleSetIter { queue: HashMap, limited: bool, - rrule_iters: Vec>, - exrules: Vec>, + rrule_iters: Vec, + exrules: Vec, exdates: BTreeSet, /// Sorted additional dates in descending order rdates: Vec, was_limited: bool, } -impl<'a> RRuleSetIter<'a> { +impl RRuleSetIter { fn generate_date( dates: &mut Vec, exrules: &mut [RRuleIter], @@ -104,7 +106,7 @@ impl<'a> RRuleSetIter<'a> { } } -impl<'a> Iterator for RRuleSetIter<'a> { +impl Iterator for RRuleSetIter { type Item = DateTime; fn next(&mut self) -> Option { @@ -191,10 +193,10 @@ impl<'a> Iterator for RRuleSetIter<'a> { } } -impl<'a> IntoIterator for &'a RRuleSet { +impl IntoIterator for &RRuleSet { type Item = DateTime; - type IntoIter = RRuleSetIter<'a>; + type IntoIter = RRuleSetIter; fn into_iter(self) -> Self::IntoIter { // Sort in decreasing order @@ -224,8 +226,16 @@ impl<'a> IntoIterator for &'a RRuleSet { } } -impl<'a> WasLimited for RRuleSetIter<'a> { +impl WasLimited for RRuleSetIter { fn was_limited(&self) -> bool { self.was_limited } } + +impl FromStr for RRuleSetIter { + type Err = RRuleError; + + fn from_str(s: &str) -> Result { + Ok(RRuleSet::from_str(s)?.into_iter()) + } +}