From 694a9a4ecb4f743ab7958a66a042381a812ad156 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 26 Sep 2017 17:18:03 +0200 Subject: Add Matcher trait for indicators --- src/error.rs | 5 +++++ src/lib.rs | 1 + src/matcher.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 src/matcher.rs diff --git a/src/error.rs b/src/error.rs index d450146..39d482f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -33,6 +33,11 @@ error_chain! { display("The passed argument is not an amount: {:?}", tt) } + ArgumentErrorNotAMoment(name: &'static str) { + description("Argument Error: Not a moment TimeType object") + display("The passed argument is not a moment, but a {}", name) + } + CannotCalculateEndOfYearOn(tt: TimeType) { description("Argument Error: Cannot calculate end-of-year") display("Argument Error: Cannot calculate end-of-year on a {:?}", tt) diff --git a/src/lib.rs b/src/lib.rs index 60bcfc1..e861ccd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,5 +7,6 @@ pub mod iter; pub mod result; pub mod timetype; pub mod indicator; +pub mod matcher; mod util; diff --git a/src/matcher.rs b/src/matcher.rs new file mode 100644 index 0000000..1482b68 --- /dev/null +++ b/src/matcher.rs @@ -0,0 +1,37 @@ + +use chrono::Datelike; + +use error::KairosError as KE; +use error::KairosErrorKind as KEK; +use error::Result; +use indicator::Day; +use indicator::Month; +use timetype::TimeType; + +/// A trait to extend indicator::* to be able to match them with a TimeType object +pub trait Matcher { + fn matches(&self, tt: &TimeType) -> Result; +} + +impl Matcher for Day { + + fn matches(&self, tt: &TimeType) -> Result { + let this : ::chrono::Weekday = self.clone().into(); + tt.get_moment() + .map(|mom| this == mom.weekday()) + .ok_or(KE::from_kind(KEK::ArgumentErrorNotAMoment(tt.name()))) + } +} + +impl Matcher for Month { + + fn matches(&self, tt: &TimeType) -> Result { + let this : u32 = self.clone().into(); + tt.get_moment() + .map(|mom| this == mom.month()) + .ok_or(KE::from_kind(KEK::ArgumentErrorNotAMoment(tt.name()))) + } + +} + + -- cgit v1.2.3