From 6ede3e3c7e57e029026b2efd2aad216b0fa83062 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 19 Sep 2017 17:42:23 +0200 Subject: Add extensions for building iterators conveniently --- src/iter.rs | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) (limited to 'src/iter.rs') diff --git a/src/iter.rs b/src/iter.rs index 0faceec..94d1458 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -93,3 +93,169 @@ impl> Iterator for CalculatingIter { } +pub mod extensions { + use timetype::TimeType as TT; + use super::Iter; + use error::Result; + use error::KairosError as KE; + use error::KairosErrorKind as KEK; + + pub trait Minutely { + fn minutely(self, i: i64) -> Result; + } + + pub trait Hourly { + fn hourly(self, i: i64) -> Result; + } + + pub trait Daily { + fn daily(self, i: i64) -> Result; + } + + pub trait Weekly : Sized { + fn weekly(self, i: i64) -> Result; + } + + pub trait Monthly { + fn monthly(self, i: i64) -> Result; + } + + pub trait Yearly { + fn yearly(self, i: i64) -> Result; + } + + pub trait Every { + fn every(self, inc: TT) -> Result; + } + + impl Minutely for TT { + + fn minutely(self, i: i64) -> Result { + match self { + TT::Moment(mom) => { + let increment = TT::minutes(i); + assert!(increment.is_a_amount(), "This is a Bug, please report this!"); + Iter::build(mom, increment) + }, + _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + } + } + + } + + impl Hourly for TT { + + fn hourly(self, i: i64) -> Result { + match self { + TT::Moment(mom) => { + let increment = TT::hours(i); + assert!(increment.is_a_amount(), "This is a Bug, please report this!"); + Iter::build(mom, increment) + }, + _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + } + } + + } + + impl Daily for TT { + + fn daily(self, i: i64) -> Result { + match self { + TT::Moment(mom) => { + let increment = TT::days(i); + assert!(increment.is_a_amount(), "This is a Bug, please report this!"); + Iter::build(mom, increment) + }, + _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + } + } + + } + + impl Weekly for TT { + + /// Conveniance function over `Daily::daily( n * 7 )` + fn weekly(self, i: i64) -> Result { + match self { + TT::Moment(mom) => { + let increment = TT::days(i * 7); + assert!(increment.is_a_amount(), "This is a Bug, please report this!"); + Iter::build(mom, increment) + }, + _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + } + } + + } + + impl Monthly for TT { + + fn monthly(self, i: i64) -> Result { + match self { + TT::Moment(mom) => { + let increment = TT::months(i); + assert!(increment.is_a_amount(), "This is a Bug, please report this!"); + Iter::build(mom, increment) + }, + _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + } + } + + } + + impl Yearly for TT { + + fn yearly(self, i: i64) -> Result { + match self { + TT::Moment(mom) => { + let increment = TT::years(i); + assert!(increment.is_a_amount(), "This is a Bug, please report this!"); + Iter::build(mom, increment) + }, + _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + } + } + + } + + + impl Every for TT { + fn every(self, inc: TT) -> Result { + match self { + TT::Moment(mom) => Iter::build(mom, inc), + _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + } + } + } + + #[cfg(test)] + mod tests { + use super::*; + use timetype::TimeType as TT; + use chrono::NaiveDate as ND; + + fn ymd_hms(y: i32, m: u32, d: u32, h: u32, mi: u32, s: u32) -> TT { + TT::moment(ND::from_ymd(y, m, d).and_hms(h, mi, s)) + } + + + #[test] + fn test_simple() { + let minutes = ymd_hms(2000, 1, 1, 0, 0, 0) + .minutely(1) + .unwrap() + .calculate() + .take(5) + .collect::>(); + + assert_eq!(ymd_hms(2000, 1, 1, 0, 1, 0), *minutes[0].as_ref().unwrap()); + assert_eq!(ymd_hms(2000, 1, 1, 0, 2, 0), *minutes[1].as_ref().unwrap()); + assert_eq!(ymd_hms(2000, 1, 1, 0, 3, 0), *minutes[2].as_ref().unwrap()); + assert_eq!(ymd_hms(2000, 1, 1, 0, 4, 0), *minutes[3].as_ref().unwrap()); + assert_eq!(ymd_hms(2000, 1, 1, 0, 5, 0), *minutes[4].as_ref().unwrap()); + } + + } + +} -- cgit v1.2.3