From 4be0e3b361615a84ecf1402b827a81ca62e95bc5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 26 Sep 2017 17:41:20 +0200 Subject: Implement filter interface for matchers --- Cargo.toml | 7 +++++++ src/indicator.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/iter.rs | 17 +++++++++++++++++ src/lib.rs | 3 +++ src/matcher.rs | 11 +++++++++++ 5 files changed, 80 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 17b7e02..4995361 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,10 @@ repository = "https://github.com/matthiasbeyer/kairos" [dependencies] chrono = "0.4" error-chain = "0.10" + +filters = { version = "0.1.1", optional = true } + +[features] +default = [] +with-filters = [ "filters" ] + diff --git a/src/indicator.rs b/src/indicator.rs index 7a57b7e..72e1136 100644 --- a/src/indicator.rs +++ b/src/indicator.rs @@ -57,3 +57,45 @@ impl Into for Month { } } } + +#[cfg(feature = "with-filters")] +pub struct DayFilter(Day); + +#[cfg(feature = "with-filters")] +impl Filter for DayFilter { + fn filter(&self, tt: &TimeType) -> bool { + tt.get_moment(|mom| mom.weekday() == self.0.into()).unwrap_or(false) + } +} + +#[cfg(feature = "with-filters")] +impl IntoFilter for Day { + type IntoFilt = DayFilter; + + fn into_filter(self) -> Self::IntoFilt { + DayFilter(self) + } + +} + + +#[cfg(feature = "with-filters")] +pub struct MonthFilter(Month); + +#[cfg(feature = "with-filters")] +impl Filter for MonthFilter { + fn filter(&self, tt: &TimeType) -> bool { + tt.get_moment(|mom| mom.month() == self.0.into()).unwrap_or(false) + } +} + +#[cfg(feature = "with-filters")] +impl IntoFilter for Month { + type IntoFilt = MonthFilter; + + fn into_filter(self) -> Self::IntoFilt { + MonthFilter(self) + } + +} + diff --git a/src/iter.rs b/src/iter.rs index 7e53da4..0b8c6a0 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -412,3 +412,20 @@ mod type_tests { } } +#[cfg(all(feature = "with-filters", test))] +mod type_tests_filter_interface { + use super::*; + use super::IntoCalculatingIter; + use super::extensions::*; + + #[test] + fn test_compile() { + // This test is solely to check whether this compiles and the API is nice + let _ = TimeType::today() + .yearly(1) + .unwrap() + .calculate() + .every(::indicator::Day::Monday.or(::indicator::Month::January)); + } +} + diff --git a/src/lib.rs b/src/lib.rs index e861ccd..b537b14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,9 @@ extern crate error_chain; extern crate chrono; +#[cfg(feature = "with-filters")] +extern crate filters; + pub mod error; pub mod iter; pub mod result; diff --git a/src/matcher.rs b/src/matcher.rs index 1482b68..d574090 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -34,4 +34,15 @@ impl Matcher for Month { } +#[cfg(feature = "with-filters")] +use filters::filter::*; + +#[cfg(feature = "with-filters")] +impl Matcher for F + where F: Filter +{ + fn matches(&self, tt: &TimeType) -> Result { + Ok(self.filter(tt)) + } +} -- cgit v1.2.3