diff options
author | Fredrik Meringdal <fmeringdal@hotmail.com> | 2020-10-20 23:47:50 +0200 |
---|---|---|
committer | Fredrik Meringdal <fmeringdal@hotmail.com> | 2020-10-20 23:47:50 +0200 |
commit | 0f992d031ade1119e7e081e75caba93b25099b0b (patch) | |
tree | be074ca4e61f65d91a3e091e2cc3ee883c0fa0e8 /src/rruleset.rs | |
parent | 8b062ddeb1143e6894653274a524b63dc7c5b165 (diff) | |
download | rust_rrule-0f992d031ade1119e7e081e75caba93b25099b0b.zip |
restructure
Diffstat (limited to 'src/rruleset.rs')
-rw-r--r-- | src/rruleset.rs | 85 |
1 files changed, 25 insertions, 60 deletions
diff --git a/src/rruleset.rs b/src/rruleset.rs index 320dca4..30a2c72 100644 --- a/src/rruleset.rs +++ b/src/rruleset.rs @@ -1,9 +1,9 @@ +use crate::iter::*; +use crate::iter_set::{iter_v2, TIterResult}; +use crate::options::*; use crate::rrule::*; use chrono::prelude::*; use chrono_tz::{Tz, UTC}; -use crate::options::*; -use crate::iter_set::{iter_v2, TIterResult}; -use crate::iter::*; use std::collections::HashMap; struct RRuleSet { @@ -12,41 +12,33 @@ struct RRuleSet { exrule: Vec<RRule>, exdate: Vec<DateTime<Utc>>, dtstart: Option<DateTime<Utc>>, - exdate_hash: HashMap<i64, ()> + exdate_hash: HashMap<i64, ()>, } struct RRuleSetIter<'a> { exdate_hash: HashMap<i64, ()>, iter_res: IterResult, - rrule_set: &'a mut RRuleSet - + rrule_set: &'a mut RRuleSet, } - impl<'a> RRuleSetIter<'a> { - pub fn new(rrule_set: &'a mut RRuleSet) -> Self { - let iter_args = IterArgs { inc: true, before: UTC.ymd(2020, 1, 1).and_hms(0, 0, 0), after: UTC.ymd(2020, 1, 1).and_hms(0, 0, 0), dt: UTC.ymd(2020, 1, 1).and_hms(0, 0, 0), }; - let mut iter_res = IterResult::new(QueryMethodTypes::ALL, iter_args); + let iter_res = IterResult::new(QueryMethodTypes::ALL, iter_args); Self { exdate_hash: HashMap::new(), iter_res, - rrule_set + rrule_set, } } - pub fn eval_exdate( - &mut self, - after: &DateTime<Tz>, - before: &DateTime<Tz>, - ) { + pub fn eval_exdate(&mut self, after: &DateTime<Tz>, before: &DateTime<Tz>) { for rrule in self.rrule_set.exrule.iter_mut() { for date in rrule.between(after, before, true) { self.exdate_hash.insert(date.timestamp(), ()); @@ -54,29 +46,20 @@ impl<'a> RRuleSetIter<'a> { } } - fn accept_2( - &mut self, - date: DateTime<Tz> - ) -> bool { + fn accept_2(&mut self, date: DateTime<Tz>) -> bool { let dt = date.timestamp(); if !self.exdate_hash.contains_key(&dt) { - self.eval_exdate( - &UTC.timestamp(dt - 1, 0), - &UTC.timestamp(dt + 1, 0), - ); + self.eval_exdate(&UTC.timestamp(dt - 1, 0), &UTC.timestamp(dt + 1, 0)); if !self.exdate_hash.contains_key(&dt) { self.exdate_hash.insert(dt, ()); return self.iter_res.accept(date.clone()); } } - + true } - - fn accept_1( - &mut self, - date: DateTime<Tz> - ) -> bool { + + fn accept_1(&mut self, date: DateTime<Tz>) -> bool { let dt = date.timestamp(); if !self.exdate_hash.contains_key(&dt) { if !self.exdate_hash.contains_key(&dt) { @@ -84,19 +67,18 @@ impl<'a> RRuleSetIter<'a> { return self.iter_res.accept(date.clone()); } } - + true } pub fn iter(&mut self, tzid: Option<String>) -> Vec<DateTime<Tz>> { - let tzid: Tz = tzid.unwrap_or(String::from("UTC")).parse().unwrap_or(UTC); - + for date in &self.rrule_set.exdate { let zoned_date = date.with_timezone(&tzid); self.exdate_hash.insert(zoned_date.timestamp(), ()); } - + match &self.iter_res.method { QueryMethodTypes::BETWEEN => { self.eval_exdate( @@ -106,18 +88,18 @@ impl<'a> RRuleSetIter<'a> { } _ => (), }; - + for date in &self.rrule_set.rdate.clone() { let zoned_date = date.with_timezone(&tzid); - if !self.accept(zoned_date){ + if !self.accept(zoned_date) { break; } } - + for rule in self.rrule_set.rrule.clone().iter_mut() { iter_v2(self, &mut rule.options); } - + let mut res = self.iter_res.get_value(); res.sort(); res @@ -125,9 +107,7 @@ impl<'a> RRuleSetIter<'a> { } impl<'a> TIterResult for RRuleSetIter<'a> { - fn accept( - &mut self, - date: DateTime<Tz>) -> bool { + fn accept(&mut self, date: DateTime<Tz>) -> bool { match &self.iter_res.method { QueryMethodTypes::BETWEEN => self.accept_1(date), _ => self.accept_2(date), @@ -139,9 +119,6 @@ impl<'a> TIterResult for RRuleSetIter<'a> { } } - - - impl RRuleSet { pub fn new() -> Self { Self { @@ -150,7 +127,7 @@ impl RRuleSet { exrule: vec![], exdate: vec![], dtstart: None, - exdate_hash: HashMap::new() + exdate_hash: HashMap::new(), } } @@ -171,7 +148,6 @@ impl RRuleSet { } pub fn all(&mut self) -> Vec<DateTime<Tz>> { - let mut iter = RRuleSetIter::new(self); // self.iter(&mut iter_res, None) iter.iter(None) @@ -215,7 +191,6 @@ impl RRuleSet { } } - #[cfg(test)] mod test_iter_set { use super::*; @@ -257,7 +232,6 @@ mod test_iter_set { #[test] fn rrule_and_exrule() { - let mut set = RRuleSet::new(); let mut options1 = ParsedOptions { @@ -306,7 +280,7 @@ mod test_iter_set { }; let exrule = RRule::new(options2); set.exrule(exrule); - + test_recurring( set.all(), vec![ @@ -319,7 +293,6 @@ mod test_iter_set { #[test] fn setdate_and_exdate() { - let mut set = RRuleSet::new(); set.rdate(ymd_hms(1997, 9, 2, 9, 0, 0)); @@ -328,11 +301,10 @@ mod test_iter_set { set.rdate(ymd_hms(1997, 9, 11, 9, 0, 0)); set.rdate(ymd_hms(1997, 9, 16, 9, 0, 0)); set.rdate(ymd_hms(1997, 9, 18, 9, 0, 0)); - + set.exdate(ymd_hms(1997, 9, 4, 9, 0, 0)); set.exdate(ymd_hms(1997, 9, 11, 9, 0, 0)); set.exdate(ymd_hms(1997, 9, 18, 9, 0, 0)); - test_recurring( set.all(), @@ -346,9 +318,6 @@ mod test_iter_set { #[test] fn setdate_and_exrule() { - - - let mut set = RRuleSet::new(); set.rdate(ymd_hms(1997, 9, 2, 9, 0, 0)); @@ -358,7 +327,6 @@ mod test_iter_set { set.rdate(ymd_hms(1997, 9, 16, 9, 0, 0)); set.rdate(ymd_hms(1997, 9, 18, 9, 0, 0)); - let mut options = ParsedOptions { freq: Frequenzy::YEARLY, count: Some(3), @@ -395,8 +363,6 @@ mod test_iter_set { #[test] fn rrule_and_exdate() { - - let mut set = RRuleSet::new(); let mut options = ParsedOptions { @@ -422,12 +388,11 @@ mod test_iter_set { }; let rrule = RRule::new(options); set.rrule(rrule); - + set.exdate(ymd_hms(1997, 9, 2, 9, 0, 0)); set.exdate(ymd_hms(1997, 9, 4, 9, 0, 0)); set.exdate(ymd_hms(1997, 9, 9, 9, 0, 0)); - test_recurring( set.all(), vec![ |