diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/lib.rs | 16 | ||||
-rw-r--r-- | src/rrule_iter.rs | 23 |
3 files changed, 31 insertions, 10 deletions
@@ -22,7 +22,7 @@ extern crate rrule; use rrule::RRule; // RRule that starts 2012.02.01 and occurs daily for 3 days. -let mut rrule: RRule = "DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3".parse().unwrap(); +let rrule: RRule = "DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3".parse().unwrap(); // Get all recurrences of the rrule let recurrences = rrule.all(); // Or rrule.into_iter().collect::<Vec<_>>(); if you want to leverage iterators @@ -31,7 +31,7 @@ //! use chrono_tz::UTC; //! use rrule::RRule; //! -//! let mut rrule: RRule = "DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3".parse().unwrap(); +//! let rrule: RRule = "DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3".parse().unwrap(); //! //! //! // All dates @@ -86,14 +86,14 @@ //! use rrule::RRule; //! //! // Parse a RRule string -//! let mut rrule: RRule = "DTSTART:20120201T093000Z\nRRULE:FREQ=WEEKLY;INTERVAL=5;UNTIL=20130130T230000Z;BYDAY=MO,FR".parse().unwrap(); +//! let rrule: RRule = "DTSTART:20120201T093000Z\nRRULE:FREQ=WEEKLY;INTERVAL=5;UNTIL=20130130T230000Z;BYDAY=MO,FR".parse().unwrap(); //! assert_eq!(rrule.all().len(), 21); //! //! //! use rrule::RRuleSet; //! //! // Parse a RRuleSet string -//! let mut rrule_set: RRuleSet = "DTSTART:20120201T023000Z\nRRULE:FREQ=MONTHLY;COUNT=5\nRDATE:20120701T023000Z,20120702T023000Z\nEXRULE:FREQ=MONTHLY;COUNT=2\nEXDATE:20120601T023000Z".parse().unwrap(); +//! let rrule_set: RRuleSet = "DTSTART:20120201T023000Z\nRRULE:FREQ=MONTHLY;COUNT=5\nRDATE:20120701T023000Z,20120702T023000Z\nEXRULE:FREQ=MONTHLY;COUNT=2\nEXDATE:20120601T023000Z".parse().unwrap(); //! assert_eq!(rrule_set.all().len(), 4); //! ``` //! @@ -119,7 +119,7 @@ //! .unwrap(); //! //! // Construct `RRule` from options -//! let mut rrule = RRule::new(options); +//! let rrule = RRule::new(options); //! let recurrences = rrule.all(); //! for i in 0..5 { //! assert_eq!(recurrences[i].year(), 2020); @@ -146,7 +146,7 @@ //! //! //! // Build options for rrule that occurs weekly on Tuesday and Wednesday -//! let mut rrule_options = Options::new() +//! let rrule_options = Options::new() //! .dtstart(UTC.ymd(2020, 1, 1).and_hms(9, 0, 0)) //! .count(4) //! .freq(Frequenzy::Weekly) @@ -155,7 +155,7 @@ //! .unwrap(); //! //! // Construct `RRule` from options -//! let mut rrule = RRule::new(rrule_options); +//! let rrule = RRule::new(rrule_options); //! //! //! // Build options for exrule that occurs weekly on Wednesday @@ -205,14 +205,14 @@ //! //! //! // Build options for rrule that occurs daily at 9 oclock for 4 times -//! let mut rrule_options = Options::new() +//! let rrule_options = Options::new() //! .dtstart(Berlin.ymd(2020, 1, 1).and_hms(9, 0, 0)) //! .count(4) //! .freq(Frequenzy::Daily) //! .build() //! .unwrap(); //! -//! let mut rrule = RRule::new(rrule_options); +//! let rrule = RRule::new(rrule_options); //! //! // Exdate in the UTC at 8 oclock which is 9 oclock in Berlin and therefore //! // collides with the second rrule occurence. diff --git a/src/rrule_iter.rs b/src/rrule_iter.rs index 9147b7d..34c4ff3 100644 --- a/src/rrule_iter.rs +++ b/src/rrule_iter.rs @@ -6,6 +6,8 @@ use crate::{datetime::Time, Frequenzy}; use chrono::prelude::*; use chrono_tz::Tz; +const MAX_YEAR: i32 = 9999; + pub struct RRuleIter { pub counter_date: DateTime<Tz>, pub ii: IterInfo, @@ -45,6 +47,10 @@ pub fn generate(iter: &mut RRuleIter) { _ => (), }; + if iter.counter_date.year() > MAX_YEAR { + return; + } + while iter.remain.is_empty() { let (dayset, start, end) = iter.ii.getdayset( &iter.ii.options.freq, @@ -138,7 +144,7 @@ pub fn generate(iter: &mut RRuleIter) { // Handle frequency and interval iter.counter_date = increment_counter_date(iter.counter_date, &options, filtered); - if iter.counter_date.year() > 2200 { + if iter.counter_date.year() > MAX_YEAR { return; } @@ -161,6 +167,7 @@ pub fn generate(iter: &mut RRuleIter) { iter.ii.rebuild(year as isize, month as usize); } } + impl IntoIterator for RRule { type Item = DateTime<Tz>; @@ -182,3 +189,17 @@ impl IntoIterator for RRule { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[ignore = "Takes too much time, only run when releasing"] + fn iteration_past_max_year_should_not_panic() { + let rrule = "DTSTART:20220201T100000Z\nRRULE:FREQ=DAILY" + .parse::<RRule>() + .unwrap(); + rrule.clone().into_iter().nth(15000000); + } +} |