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 | |
parent | 8b062ddeb1143e6894653274a524b63dc7c5b165 (diff) | |
download | rust_rrule-0f992d031ade1119e7e081e75caba93b25099b0b.zip |
restructure
-rw-r--r-- | src/iter.rs | 6 | ||||
-rw-r--r-- | src/iter_set.rs | 8 | ||||
-rw-r--r-- | src/lib.rs | 19 | ||||
-rw-r--r-- | src/poslist.rs | 1 | ||||
-rw-r--r-- | src/rrule.rs | 3 | ||||
-rw-r--r-- | src/rruleset.rs | 85 | ||||
-rw-r--r-- | tests/rrule.rs | 6 |
7 files changed, 41 insertions, 87 deletions
diff --git a/src/iter.rs b/src/iter.rs index 77560d9..c69eb3e 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -1,8 +1,8 @@ use crate::datetime::*; +use crate::iter_set::TIterResult; use crate::iterinfo::*; use crate::options::*; use crate::poslist::*; -use crate::iter_set::TIterResult; use crate::yearinfo::*; use chrono::prelude::*; use chrono::Duration; @@ -19,7 +19,7 @@ pub struct IterArgs { pub inc: bool, pub before: DateTime<Tz>, pub after: DateTime<Tz>, - pub dt: DateTime<Tz> + pub dt: DateTime<Tz>, } pub struct IterResult { @@ -62,7 +62,6 @@ impl IterResult { } } - impl TIterResult for IterResult { fn accept(&mut self, date: DateTime<Tz>) -> bool { self.total += 1; @@ -96,7 +95,6 @@ impl TIterResult for IterResult { } } - pub fn increment_counter_date( counter_date: DateTime<Utc>, options: &ParsedOptions, diff --git a/src/iter_set.rs b/src/iter_set.rs index 9f251d9..dcff97f 100644 --- a/src/iter_set.rs +++ b/src/iter_set.rs @@ -9,20 +9,15 @@ use chrono::{DateTime, Duration, Utc}; use chrono_tz::*; use std::collections::HashMap; - - pub trait TIterResult { fn accept(&mut self, date: DateTime<Tz>) -> bool; fn get_value(&self) -> Vec<DateTime<Tz>>; } - - pub fn iter_v2<T: TIterResult>( iter_result: &mut T, - options: &mut ParsedOptions + options: &mut ParsedOptions, ) -> Vec<DateTime<Tz>> { - if (options.count.is_some() && options.count.unwrap() == 0) || options.interval == 0 { return iter_result.get_value(); } @@ -140,4 +135,3 @@ pub fn iter_v2<T: TIterResult>( ii.rebuild(counter_date.year() as isize, counter_date.month() as usize); } } - @@ -2,15 +2,16 @@ extern crate chrono; extern crate chrono_tz; extern crate once_cell; -pub mod datetime; -pub mod easter; -pub mod iter; -pub mod iter_set; -pub mod iterinfo; -pub mod masks; -pub mod monthinfo; +mod datetime; +mod easter; +mod iter; +mod iter_set; +mod iterinfo; +mod masks; +mod monthinfo; +mod poslist; +mod yearinfo; + pub mod options; -pub mod poslist; pub mod rrule; pub mod rruleset; -pub mod yearinfo; diff --git a/src/poslist.rs b/src/poslist.rs index ebb780d..1dfb04b 100644 --- a/src/poslist.rs +++ b/src/poslist.rs @@ -3,7 +3,6 @@ use crate::iterinfo::*; use crate::yearinfo::*; use chrono::prelude::*; - pub fn from_ordinal(ordinal: isize) -> DateTime<Utc> { let timestamp = ordinal * 24 * 60 * 60; let naive = NaiveDateTime::from_timestamp(timestamp as i64, 0); diff --git a/src/rrule.rs b/src/rrule.rs index 0b7ad6b..ab13a9c 100644 --- a/src/rrule.rs +++ b/src/rrule.rs @@ -1,5 +1,5 @@ -use crate::iter_set::iter_v2; use crate::iter::*; +use crate::iter_set::iter_v2; use crate::options::*; use chrono::prelude::*; use chrono_tz::{Tz, UTC}; @@ -10,7 +10,6 @@ pub struct RRule { pub options: ParsedOptions, } - impl RRule { pub fn new(options: ParsedOptions) -> Self { Self { 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![ diff --git a/tests/rrule.rs b/tests/rrule.rs index 225ae4b..65aed6c 100644 --- a/tests/rrule.rs +++ b/tests/rrule.rs @@ -4,8 +4,8 @@ extern crate rrule; use chrono::prelude::*; use chrono_tz::UTC; -use rrule::rrule::RRule; use rrule::options::*; +use rrule::rrule::RRule; #[cfg(test)] mod test { @@ -23,7 +23,6 @@ mod test { } fn test_recurring(options: ParsedOptions, expected_dates: &Vec<DateTime<Utc>>) { - let mut rrule = RRule::new(options); let res = rrule.all(); @@ -41,8 +40,7 @@ mod test { #[test] fn yearly() { - let options = - ParsedOptions::new(Frequenzy::YEARLY, &ymd_hms(1997, 9, 2, 9, 0, 0)).count(3); + let options = ParsedOptions::new(Frequenzy::YEARLY, &ymd_hms(1997, 9, 2, 9, 0, 0)).count(3); test_recurring( options, &vec![ |