summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-09-16 16:00:27 +0200
committerMatthias Beyer <mail@beyermatthias.de>2017-09-16 16:00:32 +0200
commit6b9d1d0d37159720f23e4916e29f6f17841ff231 (patch)
tree09e99ac5bbd7c0cf69ef4349b71543167f9d482e
parent93418a6a5b70d5c0dcf1b509fa54d675558196da (diff)
Add types to check whether TT::Moment is weekday / in month
-rw-r--r--src/error.rs10
-rw-r--r--src/indicator.rs59
-rw-r--r--src/lib.rs1
-rw-r--r--src/timetype.rs45
4 files changed, 115 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
index 4d4a0af..d450146 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -43,6 +43,16 @@ error_chain! {
display("Argument Error: Cannot calculate end-of-month on a {:?}", tt)
}
+ CannotCompareDayTo(tt_rep: &'static str) {
+ description("Cannot compare Day to non-Moment TimeType")
+ display("Cannot compare Day to non-Moment TimeType: {:?}", tt_rep)
+ }
+
+ CannotCompareMonthTo(tt_rep: &'static str) {
+ description("Cannot compare Month to non-Moment TimeType")
+ display("Cannot compare Month to non-Moment TimeType: {:?}", tt_rep)
+ }
+
}
}
diff --git a/src/indicator.rs b/src/indicator.rs
new file mode 100644
index 0000000..7a57b7e
--- /dev/null
+++ b/src/indicator.rs
@@ -0,0 +1,59 @@
+#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
+pub enum Day {
+ Monday,
+ Tuesday,
+ Wednesday,
+ Thursday,
+ Friday,
+ Saturday,
+ Sunday,
+}
+
+impl Into<::chrono::Weekday> for Day {
+ fn into(self) -> ::chrono::Weekday {
+ match self {
+ Day::Monday => ::chrono::Weekday::Mon,
+ Day::Tuesday => ::chrono::Weekday::Tue,
+ Day::Wednesday => ::chrono::Weekday::Wed,
+ Day::Thursday => ::chrono::Weekday::Thu,
+ Day::Friday => ::chrono::Weekday::Fri,
+ Day::Saturday => ::chrono::Weekday::Sat,
+ Day::Sunday => ::chrono::Weekday::Sun,
+ }
+ }
+}
+
+#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
+pub enum Month {
+ January,
+ February,
+ March,
+ April,
+ May,
+ June,
+ July,
+ August,
+ September,
+ October,
+ November,
+ December,
+}
+
+impl Into<u32> for Month {
+ fn into(self) -> u32 {
+ match self {
+ Month::January => 1,
+ Month::February => 2,
+ Month::March => 3,
+ Month::April => 4,
+ Month::May => 5,
+ Month::June => 6,
+ Month::July => 7,
+ Month::August => 8,
+ Month::September => 9,
+ Month::October => 10,
+ Month::November => 11,
+ Month::December => 12,
+ }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 624ce64..60bcfc1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,5 +6,6 @@ pub mod error;
pub mod iter;
pub mod result;
pub mod timetype;
+pub mod indicator;
mod util;
diff --git a/src/timetype.rs b/src/timetype.rs
index 91302fc..9ab3c46 100644
--- a/src/timetype.rs
+++ b/src/timetype.rs
@@ -15,6 +15,7 @@ use result::Result;
use error::KairosErrorKind as KEK;
use error::KairosError as KE;
use error_chain::ChainedError;
+use indicator::{Day, Month};
use util::*;
/// A Type of Time, currently based on chrono::NaiveDateTime
@@ -388,6 +389,50 @@ impl TimeType {
}
}
+ /// Check whether a `TimeType::Moment` is a certain weekday. Returns an error if TimeType is
+ /// not a `TimeType::Moment`.
+ pub fn is_a(&self, d: Day) -> Result<bool> {
+ use self::TimeType as TT;
+
+ match *self {
+ TT::Moment(m) => Ok(m.weekday() == d.into()),
+ _ => Err(KE::from_kind(KEK::CannotCompareDayTo(self.name()))),
+ }
+ }
+
+ /// Check whether a `TimeType::Moment` is in a certain month. Returns an error if the TimeType
+ /// is not a `TimeType::Moment`.
+ pub fn is_in(&self, month: Month) -> Result<bool> {
+ use self::TimeType as TT;
+
+ match *self {
+ TT::Moment(m) => Ok(m.month() == month.into()),
+ _ => Err(KE::from_kind(KEK::CannotCompareMonthTo(self.name()))),
+ }
+ }
+
+ /// Get a string representation of the variant of the `TimeType` instance.
+ pub fn name(&self) -> &'static str {
+ use self::TimeType as TT;
+
+ match *self {
+ TT::Addition(..) => "Addition",
+ TT::Days(..) => "Days",
+ TT::EndOfDay(..) => "EndOfDay",
+ TT::EndOfHour(..) => "EndOfHour",
+ TT::EndOfMinute(..) => "EndOfMinute",
+ TT::EndOfMonth(..) => "EndOfMonth",
+ TT::EndOfYear(..) => "EndOfYear",
+ TT::Hours(..) => "Hours",
+ TT::Minutes(..) => "Minutes",
+ TT::Moment(..) => "Moment",
+ TT::Months(..) => "Months",
+ TT::Seconds(..) => "Seconds",
+ TT::Subtraction(..) => "Subtraction",
+ TT::Years(..) => "Years",
+ }
+ }
+
pub fn calculate(self) -> Result<TimeType> {
do_calculate(self)
}