summaryrefslogtreecommitdiffstats
path: root/src/iter.rs
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-09-27 21:16:40 +0200
committerMatthias Beyer <mail@beyermatthias.de>2017-09-27 21:16:40 +0200
commitdc88c03f54bb176470b8803e89f0467d4bfb66ad (patch)
tree3f10aa78818a249b220f7780425cf2a45507bc29 /src/iter.rs
parentaae9b15675bb6ec10041ade66959f731a6ea56fe (diff)
Add "Until" iterator type
Diffstat (limited to 'src/iter.rs')
-rw-r--r--src/iter.rs73
1 files changed, 73 insertions, 0 deletions
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<I, M> WithoutFilter<M> for I
}
}
+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;
@@ -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::<Vec<_>>();
+
+ assert!(v.is_empty());
+ }
+
+}
+