diff options
author | Fredrik Meringdal <fredrikmeringdal@Fredriks-MacBook-Pro.local> | 2021-02-03 22:48:47 +0100 |
---|---|---|
committer | Fredrik Meringdal <fredrikmeringdal@Fredriks-MacBook-Pro.local> | 2021-02-03 22:48:47 +0100 |
commit | cc4ebe0e778298df981b38c853e58602946c98b6 (patch) | |
tree | 08659826241afc7994c5665572f589a391ee3145 /src/iter | |
parent | bc16f6e7e6bbd145d1d0f541f98d74061f7fa966 (diff) | |
download | rust_rrule-cc4ebe0e778298df981b38c853e58602946c98b6.zip |
refactoring
Diffstat (limited to 'src/iter')
-rw-r--r-- | src/iter/iter_v2.rs | 165 | ||||
-rw-r--r-- | src/iter/mod.rs | 136 |
2 files changed, 2 insertions, 299 deletions
diff --git a/src/iter/iter_v2.rs b/src/iter/iter_v2.rs deleted file mode 100644 index d2054f9..0000000 --- a/src/iter/iter_v2.rs +++ /dev/null @@ -1,165 +0,0 @@ -use std::cmp::{max, min}; - -use crate::{datetime::Time, Frequenzy}; -use chrono::prelude::*; -use chrono_tz::Tz; - -use super::{build_poslist, from_ordinal, increment_counter_date, remove_filtered_days, IterInfo}; - -pub struct RRuleIter { - pub counter_date: DateTime<Tz>, - pub ii: IterInfo, - pub timeset: Vec<Time>, - pub remain: Vec<DateTime<Tz>>, - pub finished: bool, -} - -impl Iterator for RRuleIter { - type Item = DateTime<Tz>; - - fn next(&mut self) -> Option<Self::Item> { - if self.finished { - return None; - } - - if !self.remain.is_empty() { - return Some(self.remain.remove(0)); - } - - generate(self); - - // println!("Done generating: {:?}", self.remain); - - if self.remain.is_empty() { - self.finished = true; - None - } else { - Some(self.remain.remove(0)) - } - } -} - -pub fn generate(iter: &mut RRuleIter) { - let options = iter.ii.options.clone(); - - match options.count { - Some(count) if count == 0 => return, - _ => (), - }; - - while iter.remain.is_empty() { - let (dayset, start, end) = iter.ii.getdayset( - &iter.ii.options.freq, - iter.counter_date.year() as isize, - iter.counter_date.month() as usize, - iter.counter_date.day() as usize, - ); - - let mut dayset = dayset - .into_iter() - .map(|s| Some(s as isize)) - .collect::<Vec<Option<isize>>>(); - - let filtered = remove_filtered_days(&mut dayset, start, end, &iter.ii); - - if options.bysetpos.len() > 0 { - let poslist = build_poslist( - &options.bysetpos, - &iter.timeset, - start, - end, - &iter.ii, - &dayset, - &options.tzid, - ); - - for j in 0..poslist.len() { - let res = poslist[j]; - if options.until.is_some() && res > options.until.unwrap() { - // return iter_result.get_value(); - continue; // or break ? - } - - if res >= options.dtstart { - iter.remain.push(res); - - if let Some(count) = iter.ii.options.count { - if count > 0 { - iter.ii.options.count = Some(count - 1); - } - // This means that the real count is 0, because of the decrement above - if count == 1 { - return; - } - } - } - } - } else { - for j in start..end { - let current_day = dayset[j]; - if current_day.is_none() { - continue; - } - - let current_day = current_day.unwrap(); - let date = - from_ordinal(iter.ii.yearordinal().unwrap() + current_day, &options.tzid); - for k in 0..iter.timeset.len() { - let res = options - .tzid - .ymd(date.year(), date.month(), date.day()) - .and_hms( - iter.timeset[k].hour as u32, - iter.timeset[k].minute as u32, - iter.timeset[k].second as u32, - ); - if options.until.is_some() && res > options.until.unwrap() { - return; - } - if res >= options.dtstart { - iter.remain.push(res); - - if let Some(count) = iter.ii.options.count { - if count > 0 { - iter.ii.options.count = Some(count - 1); - } - // This means that the real count is 0, because of the decrement above - if count == 1 { - return; - } - } - } - } - } - } - - if options.interval == 0 { - return; - } - - // Handle frequency and interval - iter.counter_date = increment_counter_date(iter.counter_date, &options, filtered); - - if iter.counter_date.year() > 2200 { - return; - } - - if options.freq == Frequenzy::Hourly - || options.freq == Frequenzy::Minutely - || options.freq == Frequenzy::Secondly - { - iter.timeset = iter.ii.gettimeset( - &options.freq, - iter.counter_date.hour() as usize, - iter.counter_date.minute() as usize, - iter.counter_date.second() as usize, - 0, - ); - } - - let year = iter.counter_date.year(); - let month = iter.counter_date.month(); - - iter.ii.rebuild(year as isize, month as usize); - } -} diff --git a/src/iter/mod.rs b/src/iter/mod.rs index 419dcbd..5fb439a 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -3,148 +3,16 @@ mod monthinfo; mod yearinfo; pub use iterinfo::IterInfo; mod poslist; -use poslist::build_poslist; +pub use poslist::build_poslist; mod easter; -mod iter_v2; mod masks; -pub use iter_v2::{generate, RRuleIter}; -use crate::datetime::{from_ordinal, get_weekday_val, DTime, Time}; +use crate::datetime::{get_weekday_val, DTime, Time}; use crate::options::*; use crate::utils::{includes, not_empty}; -use chrono::offset::TimeZone; use chrono::prelude::*; use chrono::Duration; -pub trait IterResult { - fn accept(&self, date: &DTime) -> (bool, bool); -} - -// pub fn iter<T: IterResult>(iter_result: &mut T, options: &mut ParsedOptions) -> Vec<DTime> { -// if (options.count.is_some() && options.count.unwrap() == 0) || options.interval == 0 { -// return iter_result.get_value(); -// } - -// let mut counter_date = options.dtstart; -// let mut ii = IterInfo::new(options); -// ii.rebuild(counter_date.year() as isize, counter_date.month() as usize); - -// let mut timeset = make_timeset(&ii, &counter_date, options); -// let mut count = match options.count { -// Some(count) => count, -// _ => 0, -// }; - -// loop { -// let (dayset, start, end) = ii.getdayset( -// &options.freq, -// counter_date.year() as isize, -// counter_date.month() as usize, -// counter_date.day() as usize, -// ); - -// let mut dayset = dayset -// .into_iter() -// .map(|s| Some(s as isize)) -// .collect::<Vec<Option<isize>>>(); - -// let filtered = remove_filtered_days(&mut dayset, start, end, &ii, options); - -// if not_empty(&options.bysetpos) { -// let poslist = build_poslist( -// &options.bysetpos, -// ×et, -// start, -// end, -// &ii, -// &dayset, -// &options.tzid, -// ); - -// for j in 0..poslist.len() { -// let res = poslist[j]; -// if options.until.is_some() && res > options.until.unwrap() { -// return iter_result.get_value(); -// } - -// if res >= options.dtstart { -// if !iter_result.accept(res) { -// return iter_result.get_value(); -// } - -// if count > 0 { -// count -= 1; -// if count == 0 { -// return iter_result.get_value(); -// } -// } -// } -// } -// } else { -// for j in start..end { -// let current_day = dayset[j]; -// if current_day.is_none() { -// continue; -// } - -// let current_day = current_day.unwrap(); -// let date = from_ordinal(ii.yearordinal().unwrap() + current_day, &options.tzid); -// for k in 0..timeset.len() { -// let res = options -// .tzid -// .ymd(date.year(), date.month(), date.day()) -// .and_hms( -// timeset[k].hour as u32, -// timeset[k].minute as u32, -// timeset[k].second as u32, -// ); -// if options.until.is_some() && res > options.until.unwrap() { -// return iter_result.get_value(); -// } -// if res >= options.dtstart { -// if !iter_result.accept(res) { -// return iter_result.get_value(); -// } - -// if count > 0 { -// count -= 1; -// if count == 0 { -// return iter_result.get_value(); -// } -// } -// } -// } -// } -// } - -// if options.interval == 0 { -// return iter_result.get_value(); -// } - -// // Handle frequency and interval -// counter_date = increment_counter_date(counter_date, options, filtered); - -// if counter_date.year() > 2200 { -// return iter_result.get_value(); -// } - -// if options.freq == Frequenzy::Hourly -// || options.freq == Frequenzy::Minutely -// || options.freq == Frequenzy::Secondly -// { -// timeset = ii.gettimeset( -// &options.freq, -// counter_date.hour() as usize, -// counter_date.minute() as usize, -// counter_date.second() as usize, -// 0, -// ); -// } - -// ii.rebuild(counter_date.year() as isize, counter_date.month() as usize); -// } -// } - pub fn increment_counter_date( counter_date: DTime, options: &ParsedOptions, |