From aae9b15675bb6ec10041ade66959f731a6ea56fe Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 26 Sep 2017 18:19:55 +0200 Subject: Add WithoutFilter iterator --- src/iter.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/iter.rs b/src/iter.rs index 4f178ed..0b3688f 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -127,6 +127,53 @@ impl EveryFilter for I } } +pub struct WithoutIter(I, M) + where I: Iterator>, + M: Matcher; + +impl WithoutIter + where I: Iterator>, + M: Matcher +{ + fn new(i: I, m: M) -> WithoutIter { + WithoutIter(i, m) + } +} + +impl Iterator for WithoutIter + where I: Iterator>, + M: Matcher +{ + type Item = Result; + + fn next(&mut self) -> Option { + loop { + match self.0.next() { + None => return None, + Some(Err(e)) => return Some(Err(e)), + Some(Ok(tt)) => match self.1.matches(&tt) { + Ok(false) => return Some(Ok(tt)), + Ok(true) => continue, + Err(e) => return Some(Err(e)), + } + } + } + } +} + +pub trait WithoutFilter : Iterator> + Sized { + fn without(self, M) -> WithoutIter; +} + +impl WithoutFilter for I + where I: Iterator>, + M: Matcher +{ + fn without(self, matcher: M) -> WithoutIter { + WithoutIter::new(self, matcher) + } +} + pub mod extensions { use timetype::TimeType as TT; use super::Iter; @@ -394,5 +441,19 @@ mod type_tests_filter_interface { .take(12) .collect::>(); } + + #[test] + fn test_compile_skip() { + // This test is solely to check whether this compiles and the API is nice + let v = TimeType::today() + .daily(1) + .unwrap() + .take(20) + .every(::indicator::Day::Monday) + .without(::indicator::Day::Monday) + .collect::>(); + + assert_eq!(0, v.len()); + } } -- cgit v1.2.3 From dc88c03f54bb176470b8803e89f0467d4bfb66ad Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 27 Sep 2017 21:16:40 +0200 Subject: Add "Until" iterator type --- src/iter.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/iter.rs b/src/iter.rs index 0b3688f..64c6ba1 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -174,6 +174,54 @@ impl WithoutFilter for I } } +pub struct UntilIter(I, NaiveDateTime) + where I: Iterator>; + +impl UntilIter + where I: Iterator> +{ + fn new(i: I, date: NaiveDateTime) -> UntilIter { + UntilIter(i, date) + } +} + +impl Iterator for UntilIter + where I: Iterator> +{ + type Item = Result; + + fn next(&mut self) -> Option { + match self.0.next() { + None => None, + Some(Err(e)) => Some(Err(e)), + Some(Ok(tt)) => match tt.calculate() { + Err(e) => Some(Err(e)), + Ok(tt) => if tt.is_moment() { + if tt.get_moment().unwrap() < &self.1 { + Some(Ok(tt)) + } else { + None + } + } else { + Some(Err(KE::from_kind(KEK::ArgumentErrorNotAMoment(tt.name())))) + } + } + } + } +} + +pub trait Until : Iterator> + Sized { + fn until(self, NaiveDateTime) -> UntilIter; +} + +impl Until for I + where I: Iterator> +{ + fn until(self, ending: NaiveDateTime) -> UntilIter { + UntilIter::new(self, ending) + } +} + pub mod extensions { use timetype::TimeType as TT; use super::Iter; @@ -457,3 +505,28 @@ mod type_tests_filter_interface { } } +#[cfg(test)] +mod test_until { + use super::*; + use super::extensions::*; + + #[test] + fn test_until() { + let yesterday = (TimeType::today() - TimeType::days(1)) + .calculate() + .unwrap() + .get_moment() + .unwrap() + .clone(); + + let v = TimeType::today() + .daily(1) + .unwrap() + .until(yesterday) + .collect::>(); + + assert!(v.is_empty()); + } + +} + -- cgit v1.2.3