| 1 | //! # Manual [`rrule::RRuleSet`] |
| 2 | //! |
| 3 | //! Create an [`rrule::RRuleSet`] object manually. |
| 4 | |
| 5 | /// ## Construct [`rrule::RRuleSet`] from one `rrule` and `exrule` |
| 6 | /// The rrule will occur weekly on Tuesday and Wednesday, and the exrule |
| 7 | /// will occur weekly on Wednesday, and therefore the end result will contain |
| 8 | /// weekly recurrences on Wednesday only. |
| 9 | fn main() { |
| 10 | #[cfg(feature = "exrule")] |
| 11 | { |
| 12 | use chrono::{Datelike, TimeZone}; |
| 13 | use rrule::{Frequency, NWeekday, RRule, Tz, Weekday}; |
| 14 | // Build an rrule set that occurs weekly on Tuesday and Wednesday |
| 15 | let rrule_set = RRule::default() |
| 16 | .count(4) |
| 17 | .freq(Frequency::Weekly) |
| 18 | .by_weekday(vec![ |
| 19 | NWeekday::Every(Weekday::Tue), |
| 20 | NWeekday::Every(Weekday::Wed), |
| 21 | ]) |
| 22 | .build(Tz::UTC.with_ymd_and_hms(2020, 1, 1, 9, 0, 0).unwrap()) |
| 23 | .expect("RRule invalid"); |
| 24 | |
| 25 | // Build exrule that occurs weekly on Wednesday |
| 26 | let exrule = RRule::default() |
| 27 | .count(4) |
| 28 | .freq(Frequency::Weekly) |
| 29 | .by_weekday(vec![NWeekday::Every(Weekday::Wed)]) |
| 30 | .validate(Tz::UTC.with_ymd_and_hms(2020, 1, 1, 9, 0, 0).unwrap()) |
| 31 | .expect("RRule invalid"); |
| 32 | |
| 33 | let recurrences = rrule_set.exrule(exrule).all(10).dates; |
| 34 | |
| 35 | // Check that all the recurrences are on a Tuesday |
| 36 | for occurrence in &recurrences { |
| 37 | assert_eq!(occurrence.weekday(), Weekday::Tue); |
| 38 | } |
| 39 | |
| 40 | assert_eq!(recurrences.len(), 2); |
| 41 | println!("Done, everything worked."); |
| 42 | } |
| 43 | } |