summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFredrik Meringdal <fredrikmeringdal@Fredriks-MacBook-Pro.local>2021-02-03 21:21:41 +0100
committerFredrik Meringdal <fredrikmeringdal@Fredriks-MacBook-Pro.local>2021-02-03 21:21:41 +0100
commit2b3feb57c7881517ff943d92c320c74da6ef2829 (patch)
tree22e3dfa7c231ef7e7a14e0f606fd2c984c705549
parent11f9f1a7b2339898bcad43d001e9e211d33eb5f9 (diff)
downloadrust_rrule-2b3feb57c7881517ff943d92c320c74da6ef2829.zip
tests passing for rrule
-rw-r--r--src/iter/iter_v2.rs49
-rw-r--r--src/rruleset_iter.rs12
2 files changed, 42 insertions, 19 deletions
diff --git a/src/iter/iter_v2.rs b/src/iter/iter_v2.rs
index 2a7e361..47fee79 100644
--- a/src/iter/iter_v2.rs
+++ b/src/iter/iter_v2.rs
@@ -1,3 +1,5 @@
+use std::cmp::{max, min};
+
use crate::{datetime::Time, Frequenzy};
use chrono::prelude::*;
use chrono_tz::Tz;
@@ -16,21 +18,34 @@ impl Iterator for RRuleIter {
type Item = DateTime<Tz>;
fn next(&mut self) -> Option<Self::Item> {
- if !self.finished {
- generate(self);
+ if self.finished {
+ return None;
}
- if self.remain.is_empty() {
- return None;
+ if !self.remain.is_empty() {
+ return Some(self.remain.remove(0));
}
- 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;
- let mut count = options.count.unwrap_or(0);
+ 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(
@@ -66,11 +81,15 @@ pub fn generate(iter: &mut RRuleIter) {
}
if res >= options.dtstart {
+ println!("Addint here");
iter.remain.push(res);
- if count > 0 {
- count -= 1;
- if count == 0 {
+ 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;
}
}
@@ -99,6 +118,7 @@ pub fn generate(iter: &mut RRuleIter) {
return;
}
if res >= options.dtstart {
+ println!("Addint here 2 with count: {:?}", iter.ii.options.count);
iter.remain.push(res);
// let (add, continue_iterator) = iter_result.accept(&res);
// if add {
@@ -108,9 +128,12 @@ pub fn generate(iter: &mut RRuleIter) {
// return;
// }
- if count > 0 {
- count -= 1;
- if count == 0 {
+ 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;
}
}
diff --git a/src/rruleset_iter.rs b/src/rruleset_iter.rs
index 9fe4c56..8facf99 100644
--- a/src/rruleset_iter.rs
+++ b/src/rruleset_iter.rs
@@ -18,14 +18,14 @@ impl Iterator for RRuleIterSet {
fn next(&mut self) -> Option<Self::Item> {
let mut next_date: Option<(usize, DateTime<Tz>)> = None;
- for (i, rrule_iter) in self.rrule_iters.iter().enumerate() {
+ for (i, rrule_iter) in self.rrule_iters.iter_mut().enumerate() {
let rrule_queue = self.queue.remove(&i);
- let next_rrule_date = None;
+ let mut next_rrule_date = None;
match rrule_queue {
Some(d) => next_rrule_date = Some(d),
None => {
// should be generated
- next_rrule_date = generate(rrule_iter, &self.exrules, &mut self.exdates);
+ next_rrule_date = generate(rrule_iter, &mut self.exrules, &mut self.exdates);
}
}
@@ -54,8 +54,8 @@ impl Iterator for RRuleIterSet {
}
fn generate(
- rrule_iter: &RRuleIter,
- exrules: &Vec<RRule>,
+ rrule_iter: &mut RRuleIter,
+ exrules: &mut Vec<RRule>,
exdates: &mut HashMap<i64, ()>,
) -> Option<DateTime<Tz>> {
let mut date = rrule_iter.next();
@@ -68,7 +68,7 @@ fn generate(
fn accept_generated_date(
date: &Option<DateTime<Tz>>,
- exrules: &Vec<RRule>,
+ exrules: &mut Vec<RRule>,
exdates: &mut HashMap<i64, ()>,
) -> bool {
match date {