summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFredrik Meringdal <fredrikmeringdal@Fredriks-MacBook-Pro.local>2021-02-04 10:41:06 +0100
committerFredrik Meringdal <fredrikmeringdal@Fredriks-MacBook-Pro.local>2021-02-04 10:41:06 +0100
commitec44420555eea3655adfa1db9d8b9846a29be176 (patch)
tree233041e0add921dbe5e1a440fed5c89ddd4a1317
parentdaf5d106580ef0112ef9e1b3094248959a2c17fc (diff)
downloadrust_rrule-ec44420555eea3655adfa1db9d8b9846a29be176.zip
refactoring
-rw-r--r--src/rrule_iter.rs238
-rw-r--r--src/rrulestr.rs4
-rw-r--r--tests/rrule.rs2
3 files changed, 122 insertions, 122 deletions
diff --git a/src/rrule_iter.rs b/src/rrule_iter.rs
index 375fe00..d88d034 100644
--- a/src/rrule_iter.rs
+++ b/src/rrule_iter.rs
@@ -18,114 +18,58 @@ pub struct RRuleIter {
pub finished: bool,
}
-impl Iterator for RRuleIter {
- type Item = DateTime<Tz>;
+impl RRuleIter {
+ pub fn generate(&mut self) {
+ let options = self.ii.options.clone();
- fn next(&mut self) -> Option<Self::Item> {
- if self.finished {
- return None;
- }
+ match options.count {
+ Some(count) if count == 0 => return,
+ _ => (),
+ };
- if !self.buffer.is_empty() {
- return self.buffer.pop_front();
- }
-
- generate(self);
-
- if self.buffer.is_empty() {
- self.finished = true;
+ if self.counter_date.year() > MAX_YEAR {
+ return;
}
- self.buffer.pop_front()
- }
-}
-pub fn generate(iter: &mut RRuleIter) {
- let options = iter.ii.options.clone();
-
- match options.count {
- Some(count) if count == 0 => return,
- _ => (),
- };
-
- if iter.counter_date.year() > MAX_YEAR {
- return;
- }
-
- while iter.buffer.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,
+ while self.buffer.is_empty() {
+ let (dayset, start, end) = self.ii.getdayset(
+ &self.ii.options.freq,
+ self.counter_date.year() as isize,
+ self.counter_date.month() as usize,
+ self.counter_date.day() as usize,
);
- 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.buffer.push_back(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,
- );
+ 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, &self.ii);
+
+ if options.bysetpos.len() > 0 {
+ let poslist = build_poslist(
+ &options.bysetpos,
+ &self.timeset,
+ start,
+ end,
+ &self.ii,
+ &dayset,
+ &options.tzid,
+ );
+
+ for j in 0..poslist.len() {
+ let res = poslist[j];
if options.until.is_some() && res > options.until.unwrap() {
- return;
+ // return iter_result.get_value();
+ continue; // or break ?
}
+
if res >= options.dtstart {
- iter.buffer.push_back(res);
+ self.buffer.push_back(res);
- if let Some(count) = iter.ii.options.count {
+ if let Some(count) = self.ii.options.count {
if count > 0 {
- iter.ii.options.count = Some(count - 1);
+ self.ii.options.count = Some(count - 1);
}
// This means that the real count is 0, because of the decrement above
if count == 1 {
@@ -134,37 +78,95 @@ pub fn generate(iter: &mut RRuleIter) {
}
}
}
+ } 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(self.ii.yearordinal().unwrap() + current_day, &options.tzid);
+ for k in 0..self.timeset.len() {
+ let res = options
+ .tzid
+ .ymd(date.year(), date.month(), date.day())
+ .and_hms(
+ self.timeset[k].hour as u32,
+ self.timeset[k].minute as u32,
+ self.timeset[k].second as u32,
+ );
+ if options.until.is_some() && res > options.until.unwrap() {
+ return;
+ }
+ if res >= options.dtstart {
+ self.buffer.push_back(res);
+
+ if let Some(count) = self.ii.options.count {
+ if count > 0 {
+ self.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;
+ if options.interval == 0 {
+ return;
+ }
+
+ // Handle frequency and interval
+ self.counter_date = increment_counter_date(self.counter_date, &options, filtered);
+
+ if self.counter_date.year() > MAX_YEAR {
+ return;
+ }
+
+ if options.freq == Frequenzy::Hourly
+ || options.freq == Frequenzy::Minutely
+ || options.freq == Frequenzy::Secondly
+ {
+ self.timeset = self.ii.gettimeset(
+ &options.freq,
+ self.counter_date.hour() as usize,
+ self.counter_date.minute() as usize,
+ self.counter_date.second() as usize,
+ 0,
+ );
+ }
+
+ let year = self.counter_date.year();
+ let month = self.counter_date.month();
+
+ self.ii.rebuild(year as isize, month as usize);
}
+ }
+}
- // Handle frequency and interval
- iter.counter_date = increment_counter_date(iter.counter_date, &options, filtered);
+impl Iterator for RRuleIter {
+ type Item = DateTime<Tz>;
- if iter.counter_date.year() > MAX_YEAR {
- return;
+ fn next(&mut self) -> Option<Self::Item> {
+ if self.finished {
+ return None;
}
- 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,
- );
+ if !self.buffer.is_empty() {
+ return self.buffer.pop_front();
}
- let year = iter.counter_date.year();
- let month = iter.counter_date.month();
+ self.generate();
- iter.ii.rebuild(year as isize, month as usize);
+ if self.buffer.is_empty() {
+ self.finished = true;
+ }
+ self.buffer.pop_front()
}
}
diff --git a/src/rrulestr.rs b/src/rrulestr.rs
index 232f5a4..62d1faa 100644
--- a/src/rrulestr.rs
+++ b/src/rrulestr.rs
@@ -573,9 +573,7 @@ pub fn build_rrule(s: &str) -> Result<RRule, RRuleParseError> {
#[cfg(test)]
mod test {
- use super::NWeekdayIdentifier;
use super::*;
- use chrono_tz::{Tz, UTC};
#[test]
fn it_works_1() {
@@ -724,7 +722,7 @@ mod test {
fn bench() {
let now = std::time::SystemTime::now();
for _ in 0..10000 {
- let mut res = build_rruleset("RRULE:UNTIL=19990404T110000Z;DTSTART;TZID=America/New_York:19990104T110000Z;FREQ=WEEKLY;BYDAY=TU,WE").unwrap();
+ let res = build_rruleset("RRULE:UNTIL=19990404T110000Z;DTSTART;TZID=America/New_York:19990104T110000Z;FREQ=WEEKLY;BYDAY=TU,WE").unwrap();
// println!("Parsing took: {:?}", now.elapsed().unwrap().as_millis());
let tmp_now = std::time::SystemTime::now();
diff --git a/tests/rrule.rs b/tests/rrule.rs
index bfb6f7c..e6d3376 100644
--- a/tests/rrule.rs
+++ b/tests/rrule.rs
@@ -22,7 +22,7 @@ mod test {
}
fn test_recurring(options: ParsedOptions, expected_dates: &Vec<DateTime<Tz>>) {
- let mut rrule = RRule::new(options);
+ let rrule = RRule::new(options);
let res = rrule.all();
assert_eq!(