diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-27 21:20:07 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-27 21:20:07 +0200 |
commit | 0057e488da0b0f2f33fdb88ab1517d1d47a846b4 (patch) | |
tree | 3f10aa78818a249b220f7780425cf2a45507bc29 | |
parent | 211fa5a91930fcdeec11da5055e53b645139b432 (diff) | |
parent | dc88c03f54bb176470b8803e89f0467d4bfb66ad (diff) |
Merge branch 'iter'
-rw-r--r-- | src/iter.rs | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/iter.rs b/src/iter.rs index 4f178ed..64c6ba1 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -127,6 +127,101 @@ impl<I, M> EveryFilter<M> for I } } +pub struct WithoutIter<I, M>(I, M) + where I: Iterator<Item = Result<TimeType>>, + M: Matcher; + +impl<I, M> WithoutIter<I, M> + where I: Iterator<Item = Result<TimeType>>, + M: Matcher +{ + fn new(i: I, m: M) -> WithoutIter<I, M> { + WithoutIter(i, m) + } +} + +impl<I, M> Iterator for WithoutIter<I, M> + where I: Iterator<Item = Result<TimeType>>, + M: Matcher +{ + type Item = Result<TimeType>; + + fn next(&mut self) -> Option<Self::Item> { + 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<M: Matcher> : Iterator<Item = Result<TimeType>> + Sized { + fn without(self, M) -> WithoutIter<Self, M>; +} + +impl<I, M> WithoutFilter<M> for I + where I: Iterator<Item = Result<TimeType>>, + M: Matcher +{ + fn without(self, matcher: M) -> WithoutIter<Self, M> { + WithoutIter::new(self, matcher) + } +} + +pub struct UntilIter<I>(I, NaiveDateTime) + where I: Iterator<Item = Result<TimeType>>; + +impl<I> UntilIter<I> + where I: Iterator<Item = Result<TimeType>> +{ + fn new(i: I, date: NaiveDateTime) -> UntilIter<I> { + UntilIter(i, date) + } +} + +impl<I> Iterator for UntilIter<I> + where I: Iterator<Item = Result<TimeType>> +{ + type Item = Result<TimeType>; + + fn next(&mut self) -> Option<Self::Item> { + 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<Item = Result<TimeType>> + Sized { + fn until(self, NaiveDateTime) -> UntilIter<Self>; +} + +impl<I> Until for I + where I: Iterator<Item = Result<TimeType>> +{ + fn until(self, ending: NaiveDateTime) -> UntilIter<Self> { + UntilIter::new(self, ending) + } +} + pub mod extensions { use timetype::TimeType as TT; use super::Iter; @@ -394,5 +489,44 @@ mod type_tests_filter_interface { .take(12) .collect::<Vec<_>>(); } + + #[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::<Vec<_>>(); + + assert_eq!(0, v.len()); + } +} + +#[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::<Vec<_>>(); + + assert!(v.is_empty()); + } + } |