summaryrefslogtreecommitdiffstats
path: root/src/iter.rs
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-09-19 17:42:23 +0200
committerMatthias Beyer <mail@beyermatthias.de>2017-09-19 17:42:23 +0200
commit6ede3e3c7e57e029026b2efd2aad216b0fa83062 (patch)
treec005465f93d92fd68019bd7e0306bef30ab82534 /src/iter.rs
parentae868ddf8259599ac733734e7372c3eff4a7c7fd (diff)
Add extensions for building iterators conveniently
Diffstat (limited to 'src/iter.rs')
-rw-r--r--src/iter.rs166
1 files changed, 166 insertions, 0 deletions
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<I: Iterator<Item = TimeType>> Iterator for CalculatingIter<I> {
}
+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<Iter>;
+ }
+
+ pub trait Hourly {
+ fn hourly(self, i: i64) -> Result<Iter>;
+ }
+
+ pub trait Daily {
+ fn daily(self, i: i64) -> Result<Iter>;
+ }
+
+ pub trait Weekly : Sized {
+ fn weekly(self, i: i64) -> Result<Iter>;
+ }
+
+ pub trait Monthly {
+ fn monthly(self, i: i64) -> Result<Iter>;
+ }
+
+ pub trait Yearly {
+ fn yearly(self, i: i64) -> Result<Iter>;
+ }
+
+ pub trait Every {
+ fn every(self, inc: TT) -> Result<Iter>;
+ }
+
+ impl Minutely for TT {
+
+ fn minutely(self, i: i64) -> Result<Iter> {
+ 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<Iter> {
+ 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<Iter> {
+ 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<Iter> {
+ 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<Iter> {
+ 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<Iter> {
+ 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<Iter> {
+ 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::<Vec<_>>();
+
+ 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());
+ }
+
+ }
+
+}