From 6c25067ba3218abbd9c4e60467559edd4d923deb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 30 Oct 2018 13:25:33 +0100 Subject: Move code to failure as error handling library --- Cargo.toml | 2 +- src/error.rs | 89 +++++---------- src/iter.rs | 30 ++--- src/lib.rs | 2 +- src/matcher.rs | 10 +- src/parser/iterator.rs | 29 ++--- src/parser/mod.rs | 4 +- src/parser/timetype.rs | 4 +- src/timetype.rs | 304 +++++++++++++++++++++++++------------------------ 9 files changed, 226 insertions(+), 248 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 78e4d68..e76c984 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,9 +14,9 @@ repository = "https://github.com/matthiasbeyer/kairos" [dependencies] chrono = "0.4" -error-chain = "0.12" nom = "3.2" iso8601 = "0.2" +failure = "0.1" filters = { version = "0.3", optional = true } diff --git a/src/error.rs b/src/error.rs index 43413d7..c095000 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,79 +1,42 @@ use timetype::TimeType; -error_chain! { - types { - KairosError, KairosErrorKind, ResultExt, Result; - } +#[derive(Debug, Clone, Eq, PartialEq, Fail)] +pub enum ErrorKind { - links { - } + #[fail(display = "Unknown Error")] + UnknownError, - foreign_links { - NomError(::nom::simple_errors::Err); - } + #[fail(display = "Cannot add: {:?} + {:?}", _0, _1)] + CannotAdd(TimeType, TimeType), - errors { + #[fail(display = "Cannot subtract: {:?} - {:?}", _0, _1)] + CannotSub(TimeType, TimeType), - UnknownError { - description("Unknown Error") - display("Unknown Error") - } + #[fail(display = "The passed argument is not an amount: {:?}", _0)] + ArgumentErrorNotAnAmount(TimeType), - CannotAdd(a: TimeType, b: TimeType) { - description("Cannot add") - display("Cannot add: {:?} + {:?}", a, b) - } + #[fail(display = "The passed argument is not a moment, but a {}", _0)] + ArgumentErrorNotAMoment(&'static str), - CannotSub(a: TimeType, b: TimeType) { - description("Cannot subtract") - display("Cannot subtract: {:?} - {:?}", a, b) - } + #[fail(display = "Argument Error: Cannot calculate end-of-year on a {:?}", _0)] + CannotCalculateEndOfYearOn(TimeType), - ArgumentErrorNotAnAmount(tt: TimeType) { - description("Argument Error: Not an amount TimeType object") - display("The passed argument is not an amount: {:?}", tt) - } + #[fail(display = "Argument Error: Cannot calculate end-of-month on a {:?}", _0)] + CannotCalculateEndOfMonthOn(TimeType), - ArgumentErrorNotAMoment(name: &'static str) { - description("Argument Error: Not a moment TimeType object") - display("The passed argument is not a moment, but a {}", name) - } + #[fail(display = "Cannot compare Day to non-Moment TimeType: {:?}", _0)] + CannotCompareDayTo(&'static str), - CannotCalculateEndOfYearOn(tt: TimeType) { - description("Argument Error: Cannot calculate end-of-year") - display("Argument Error: Cannot calculate end-of-year on a {:?}", tt) - } + #[fail(display = "Cannot compare Month to non-Moment TimeType: {:?}", _0)] + CannotCompareMonthTo(&'static str), - CannotCalculateEndOfMonthOn(tt: TimeType) { - description("Argument Error: Cannot calculate end-of-month") - display("Argument Error: Cannot calculate end-of-month on a {:?}", tt) - } + #[fail(display = "Out of bounds: {}-{}-{}T{}:{}:{}", _0, _1, _2, _3, _4, _5)] + OutOfBounds(i32, u32, u32, u32, u32, u32), - CannotCompareDayTo(tt_rep: &'static str) { - description("Cannot compare Day to non-Moment TimeType") - display("Cannot compare Day to non-Moment TimeType: {:?}", tt_rep) - } + #[fail(display = "Cannot calculate date for iterator")] + NotADateInsideIterator, - CannotCompareMonthTo(tt_rep: &'static str) { - description("Cannot compare Month to non-Moment TimeType") - display("Cannot compare Month to non-Moment TimeType: {:?}", tt_rep) - } - - OutOfBounds(y: i32, mo: u32, d: u32, hr: u32, mi: u32, s: u32) { - description("Out of bounds error") - display("Out of bounds: {}-{}-{}T{}:{}:{}", y, mo, d, hr, mi, s) - } - - NotADateInsideIterator { - description("Cannot calculate date for iterator") - display("Cannot calculate date for iterator") - } - - UnknownParserError { - description("Unknown parser error") - display("Unknown parser error") - } - - } + #[fail(display = "Unknown parser error")] + UnknownParserError, } diff --git a/src/iter.rs b/src/iter.rs index 85463c8..d3d8978 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -2,10 +2,10 @@ //! use chrono::NaiveDateTime; +use failure::Fallible as Result; +use failure::Error; -use error::KairosError as KE; -use error::KairosErrorKind as KEK; -use error::Result; +use error::ErrorKind as KEK; use timetype::TimeType; use matcher::Matcher; @@ -27,7 +27,7 @@ impl Iter { pub fn build(base: NaiveDateTime, inc: TimeType) -> Result { if !inc.is_a_amount() { - Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(inc))) + Err(Error::from(KEK::ArgumentErrorNotAnAmount(inc))) } else { Ok(Iter { base: TimeType::moment(base), @@ -214,7 +214,7 @@ impl Iterator for UntilIter None } } else { - Some(Err(KE::from_kind(KEK::ArgumentErrorNotAMoment(tt.name())))) + Some(Err(Error::from(KEK::ArgumentErrorNotAMoment(tt.name())))) } } } @@ -284,9 +284,9 @@ impl Times for 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; + use failure::Fallible as Result; + use failure::Error; + use error::ErrorKind as KEK; pub trait Minutely { fn minutely(self, i: i64) -> Result; @@ -325,7 +325,7 @@ pub mod extensions { assert!(increment.is_a_amount(), "This is a Bug, please report this!"); Iter::build(mom, increment) }, - _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + _ => Err(Error::from(KEK::ArgumentErrorNotAnAmount(self))), } } @@ -340,7 +340,7 @@ pub mod extensions { assert!(increment.is_a_amount(), "This is a Bug, please report this!"); Iter::build(mom, increment) }, - _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + _ => Err(Error::from(KEK::ArgumentErrorNotAnAmount(self))), } } @@ -355,7 +355,7 @@ pub mod extensions { assert!(increment.is_a_amount(), "This is a Bug, please report this!"); Iter::build(mom, increment) }, - _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + _ => Err(Error::from(KEK::ArgumentErrorNotAnAmount(self))), } } @@ -371,7 +371,7 @@ pub mod extensions { assert!(increment.is_a_amount(), "This is a Bug, please report this!"); Iter::build(mom, increment) }, - _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + _ => Err(Error::from(KEK::ArgumentErrorNotAnAmount(self))), } } @@ -386,7 +386,7 @@ pub mod extensions { assert!(increment.is_a_amount(), "This is a Bug, please report this!"); Iter::build(mom, increment) }, - _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + _ => Err(Error::from(KEK::ArgumentErrorNotAnAmount(self))), } } @@ -401,7 +401,7 @@ pub mod extensions { assert!(increment.is_a_amount(), "This is a Bug, please report this!"); Iter::build(mom, increment) }, - _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + _ => Err(Error::from(KEK::ArgumentErrorNotAnAmount(self))), } } @@ -412,7 +412,7 @@ pub mod extensions { fn every(self, inc: TT) -> Result { match self { TT::Moment(mom) => Iter::build(mom, inc), - _ => Err(KE::from_kind(KEK::ArgumentErrorNotAnAmount(self))), + _ => Err(Error::from(KEK::ArgumentErrorNotAnAmount(self))), } } } diff --git a/src/lib.rs b/src/lib.rs index d8e1d7c..ae1b9c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ #![recursion_limit="256"] #[macro_use] -extern crate error_chain; +extern crate failure; extern crate chrono; #[macro_use] diff --git a/src/matcher.rs b/src/matcher.rs index d574090..ca4fb8d 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -1,9 +1,9 @@ use chrono::Datelike; -use error::KairosError as KE; -use error::KairosErrorKind as KEK; -use error::Result; +use error::ErrorKind as KEK; +use failure::Fallible as Result; +use failure::Error; use indicator::Day; use indicator::Month; use timetype::TimeType; @@ -19,7 +19,7 @@ impl Matcher for Day { let this : ::chrono::Weekday = self.clone().into(); tt.get_moment() .map(|mom| this == mom.weekday()) - .ok_or(KE::from_kind(KEK::ArgumentErrorNotAMoment(tt.name()))) + .ok_or(Error::from(KEK::ArgumentErrorNotAMoment(tt.name()))) } } @@ -29,7 +29,7 @@ impl Matcher for Month { let this : u32 = self.clone().into(); tt.get_moment() .map(|mom| this == mom.month()) - .ok_or(KE::from_kind(KEK::ArgumentErrorNotAMoment(tt.name()))) + .ok_or(Error::from(KEK::ArgumentErrorNotAMoment(tt.name()))) } } diff --git a/src/parser/iterator.rs b/src/parser/iterator.rs index f06edd0..a60892e 100644 --- a/src/parser/iterator.rs +++ b/src/parser/iterator.rs @@ -1,5 +1,7 @@ use nom::whitespace::sp; +use failure::Fallible as Result; +use failure::Error; use parser::timetype::*; use timetype::IntoTimeType; use timetype; @@ -64,7 +66,7 @@ named!(pub iterator, do_parse!( pub struct Iterator(Date, Iterspec, Option); impl Iterator { - pub fn into_user_iterator(self) -> error::Result> { + pub fn into_user_iterator(self) -> Result> { use iter::Times; use iter::Until; @@ -91,7 +93,8 @@ impl Iterator { let into_ndt = |e: timetype::TimeType| try!(e.calculate()) .get_moment() - .ok_or(error::KairosErrorKind::NotADateInsideIterator) + .ok_or(error::ErrorKind::NotADateInsideIterator) + .map_err(Error::from) .map(Clone::clone); match self.2 { @@ -122,7 +125,7 @@ impl Iterator { // names are hard #[derive(Debug)] pub enum UserIterator - where I: ::std::iter::Iterator> + where I: ::std::iter::Iterator> { Iterator(iter::Iter), TimesIter(iter::TimesIter), @@ -130,9 +133,9 @@ pub enum UserIterator } impl ::std::iter::Iterator for UserIterator - where I: ::std::iter::Iterator> + where I: ::std::iter::Iterator> { - type Item = error::Result; + type Item = Result; fn next(&mut self) -> Option { match *self { @@ -171,7 +174,7 @@ mod tests { let (_, i) = res.unwrap(); println!("{:#?}", i); - let ui : Result, _> = i.into_user_iterator(); + let ui : Result> = i.into_user_iterator(); assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); @@ -195,7 +198,7 @@ mod tests { let (_, i) = res.unwrap(); println!("{:#?}", i); - let ui : Result, _> = i.into_user_iterator(); + let ui : Result> = i.into_user_iterator(); assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); @@ -219,7 +222,7 @@ mod tests { let (_, i) = res.unwrap(); println!("{:#?}", i); - let ui : Result, _> = i.into_user_iterator(); + let ui : Result> = i.into_user_iterator(); assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); @@ -243,7 +246,7 @@ mod tests { let (_, i) = res.unwrap(); println!("{:#?}", i); - let ui : Result, _> = i.into_user_iterator(); + let ui : Result> = i.into_user_iterator(); assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); @@ -275,7 +278,7 @@ mod tests { let (_, i) = res.unwrap(); println!("{:#?}", i); - let ui : Result, _> = i.into_user_iterator(); + let ui : Result> = i.into_user_iterator(); assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); println!("Okay: {:#?}", ui); @@ -306,7 +309,7 @@ mod tests { let (_, i) = res.unwrap(); println!("{:#?}", i); - let ui : Result, _> = i.into_user_iterator(); + let ui : Result> = i.into_user_iterator(); assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); @@ -336,7 +339,7 @@ mod tests { let (_, i) = res.unwrap(); println!("{:#?}", i); - let ui : Result, _> = i.into_user_iterator(); + let ui : Result> = i.into_user_iterator(); assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); @@ -366,7 +369,7 @@ mod tests { let (_, i) = res.unwrap(); println!("{:#?}", i); - let ui : Result, _> = i.into_user_iterator(); + let ui : Result> = i.into_user_iterator(); assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f67a3cb..c6483b4 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -49,8 +49,8 @@ use nom::IResult; mod timetype; mod iterator; -use error::Result; -use error::KairosErrorKind as KEK; +use failure::Fallible as Result; +use error::ErrorKind as KEK; use iter::Iter; use timetype::IntoTimeType; use parser::timetype::timetype; diff --git a/src/parser/timetype.rs b/src/parser/timetype.rs index 83516fe..13f111b 100644 --- a/src/parser/timetype.rs +++ b/src/parser/timetype.rs @@ -4,11 +4,11 @@ use std::str::FromStr; use nom::digit; use nom::whitespace::sp; use chrono::NaiveDate; +use failure::Fallible as Result; use timetype::IntoTimeType; use timetype; -use error::Result; -use error::KairosErrorKind as KEK; +use error::ErrorKind as KEK; named!(pub integer, alt!( map_res!( diff --git a/src/timetype.rs b/src/timetype.rs index 8d12226..3d02c57 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -5,15 +5,15 @@ use chrono::NaiveDateTime; use chrono::NaiveDate; use chrono::Datelike; use chrono::Timelike; +use failure::Fallible as Result; +use failure::Error; use std::ops::Add; use std::ops::AddAssign; use std::ops::Sub; use std::ops::SubAssign; -use error::Result; -use error::KairosErrorKind as KEK; -use error::KairosError as KE; +use error::ErrorKind as KEK; use indicator::{Day, Month}; use util::*; @@ -395,7 +395,7 @@ impl TimeType { match *self { TT::Moment(m) => Ok(m.weekday() == d.into()), - _ => Err(KE::from_kind(KEK::CannotCompareDayTo(self.name()))), + _ => Err(Error::from(KEK::CannotCompareDayTo(self.name()))), } } @@ -406,7 +406,7 @@ impl TimeType { match *self { TT::Moment(m) => Ok(m.month() == month.into()), - _ => Err(KE::from_kind(KEK::CannotCompareMonthTo(self.name()))), + _ => Err(Error::from(KEK::CannotCompareMonthTo(self.name()))), } } @@ -481,11 +481,12 @@ fn end_of_year(tt: TimeType) -> Result { els @ TT::Months(_) | els @ TT::Years(_) | els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(KE::from_kind(KEK::CannotCalculateEndOfYearOn(els))), + els @ TT::Subtraction(_, _) => Err(Error::from(KEK::CannotCalculateEndOfYearOn(els))), TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), 12, 31) .map(|nd| nd.and_hms(0, 0, 0)) .map(TT::moment) - .ok_or(KEK::OutOfBounds(m.year() as i32, 12, 31, 0, 0, 0).into()), + .ok_or(KEK::OutOfBounds(m.year() as i32, 12, 31, 0, 0, 0)) + .map_err(Error::from), TT::EndOfYear(e) => do_calculate(*e), TT::EndOfMonth(e) => do_calculate(*e), @@ -510,13 +511,14 @@ fn end_of_month(tt: TimeType) -> Result { els @ TT::Months(_) | els @ TT::Years(_) | els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(KE::from_kind(KEK::CannotCalculateEndOfMonthOn(els))), + els @ TT::Subtraction(_, _) => Err(Error::from(KEK::CannotCalculateEndOfMonthOn(els))), TT::Moment(m) => { let last_day = get_num_of_days_in_month(m.year() as i64, m.month() as i64) as u32; NaiveDate::from_ymd_opt(m.year(), m.month(), last_day) .map(|nd| nd.and_hms(0, 0, 0)) .map(TT::moment) - .ok_or(KEK::OutOfBounds(m.year() as i32, m.month() as u32, last_day, 0, 0, 0).into()) + .ok_or(KEK::OutOfBounds(m.year() as i32, m.month() as u32, last_day, 0, 0, 0)) + .map_err(Error::from) }, TT::EndOfYear(e) => do_calculate(*e), TT::EndOfMonth(e) => do_calculate(*e), @@ -541,11 +543,12 @@ fn end_of_day(tt: TimeType) -> Result { els @ TT::Months(_) | els @ TT::Years(_) | els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(KE::from_kind(KEK::CannotCalculateEndOfMonthOn(els))), + els @ TT::Subtraction(_, _) => Err(Error::from(KEK::CannotCalculateEndOfMonthOn(els))), TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) .map(|nd| nd.and_hms(23, 59, 59)) .map(TT::moment) - .ok_or(KEK::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, 23, 59, 59).into()), + .ok_or(KEK::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, 23, 59, 59)) + .map_err(Error::from), TT::EndOfYear(e) => do_calculate(*e), TT::EndOfMonth(e) => do_calculate(*e), TT::EndOfDay(e) => do_calculate(*e), @@ -569,11 +572,12 @@ fn end_of_hour(tt: TimeType) -> Result { els @ TT::Months(_) | els @ TT::Years(_) | els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(KE::from_kind(KEK::CannotCalculateEndOfMonthOn(els))), + els @ TT::Subtraction(_, _) => Err(Error::from(KEK::CannotCalculateEndOfMonthOn(els))), TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) .and_then(|nd| nd.and_hms_opt(m.hour(), 59, 59)) .map(TT::moment) - .ok_or(KEK::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, m.hour() as u32, 59, 59).into()), + .ok_or(KEK::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, m.hour() as u32, 59, 59)) + .map_err(Error::from), TT::EndOfYear(e) => do_calculate(*e), TT::EndOfMonth(e) => do_calculate(*e), TT::EndOfDay(e) => do_calculate(*e), @@ -597,11 +601,12 @@ fn end_of_minute(tt: TimeType) -> Result { els @ TT::Months(_) | els @ TT::Years(_) | els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(KE::from_kind(KEK::CannotCalculateEndOfMonthOn(els))), + els @ TT::Subtraction(_, _) => Err(Error::from(KEK::CannotCalculateEndOfMonthOn(els))), TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) .and_then(|nd| nd.and_hms_opt(m.hour(), m.minute(), 59)) .map(TT::moment) - .ok_or(KEK::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, m.hour() as u32, m.minute() as u32, 59 as u32).into()), + .ok_or(KEK::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, m.hour() as u32, m.minute() as u32, 59 as u32)) + .map_err(Error::from), TT::EndOfYear(e) => do_calculate(*e), TT::EndOfMonth(e) => do_calculate(*e), TT::EndOfDay(e) => do_calculate(*e), @@ -615,7 +620,7 @@ fn add(a: Box, b: Box) -> Result { match (*a, *b) { (TT::Moment(mom), thing) => add_to_moment(mom, thing), - (thing, TT::Moment(mom)) => Err(KE::from_kind(KEK::CannotAdd(thing, TT::Moment(mom)))), + (thing, TT::Moment(mom)) => Err(Error::from(KEK::CannotAdd(thing, TT::Moment(mom)))), (TT::Seconds(a), other) => add_to_seconds(a, other), (TT::Minutes(a), other) => add_to_minutes(a, other), @@ -638,21 +643,21 @@ fn add(a: Box, b: Box) -> Result { .and_then(|bx| add(Box::new(other), bx)) .and_then(|rx| sub(Box::new(rx), b)), - (TT::EndOfYear(e), other) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfYear(e)))), - (other, TT::EndOfYear(e)) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfYear(e)))), + (TT::EndOfYear(e), other) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfYear(e)))), + (other, TT::EndOfYear(e)) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfYear(e)))), - (TT::EndOfMonth(e), other) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfMonth(e)))), - (other, TT::EndOfMonth(e)) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfMonth(e)))), + (TT::EndOfMonth(e), other) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfMonth(e)))), + (other, TT::EndOfMonth(e)) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfMonth(e)))), - (TT::EndOfDay(e), other) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfDay(e)))), - (other, TT::EndOfDay(e)) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfDay(e)))), + (TT::EndOfDay(e), other) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfDay(e)))), + (other, TT::EndOfDay(e)) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfDay(e)))), - (TT::EndOfHour(e), other) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfHour(e)))), - (other, TT::EndOfHour(e)) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfHour(e)))), + (TT::EndOfHour(e), other) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfHour(e)))), + (other, TT::EndOfHour(e)) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfHour(e)))), - (TT::EndOfMinute(e), other) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfMinute(e)))), + (TT::EndOfMinute(e), other) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfMinute(e)))), // unreachable, just for completeness: - //(other, TT::EndOfMinute(e)) => Err(KE::from_kind(KEK::CannotAdd(other, TT::EndOfMinute(e)))), + //(other, TT::EndOfMinute(e)) => Err(Error::from(KEK::CannotAdd(other, TT::EndOfMinute(e)))), } } @@ -666,12 +671,12 @@ fn add_to_seconds(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Seconds(a * 60 * 60 * 24 + amount)), TT::Months(a) => Ok(TT::Seconds(a * 60 * 60 * 24 * 30 + amount)), TT::Years(a) => Ok(TT::Seconds(a * 60 * 60 * 24 * 30 * 12 + amount)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotAdd(TT::Seconds(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotAdd(TT::Seconds(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotAdd(TT::Seconds(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => add_to_seconds(amount, try!(add(b, c))), TT::Subtraction(b, c) => add_to_seconds(amount, try!(sub(b, c))), } @@ -687,12 +692,12 @@ fn add_to_minutes(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Minutes(a * 60 * 24 + amount)), TT::Months(a) => Ok(TT::Minutes(a * 60 * 24 * 30 + amount)), TT::Years(a) => Ok(TT::Minutes(a * 60 * 24 * 30 * 12 + amount)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotAdd(TT::Minutes(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotAdd(TT::Minutes(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotAdd(TT::Minutes(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => add_to_minutes(amount, try!(add(b, c))), TT::Subtraction(b, c) => add_to_minutes(amount, try!(sub(b, c))), } @@ -708,12 +713,12 @@ fn add_to_hours(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Hours( a * 24 + amount)), TT::Months(a) => Ok(TT::Hours( a * 24 * 30 + amount)), TT::Years(a) => Ok(TT::Hours( a * 24 * 30 * 12 + amount)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotAdd(TT::Hours(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Hours(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Hours(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Hours(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Hours(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Hours(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotAdd(TT::Hours(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotAdd(TT::Hours(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotAdd(TT::Hours(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotAdd(TT::Hours(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotAdd(TT::Hours(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotAdd(TT::Hours(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => add_to_hours(amount, try!(add(b, c))), TT::Subtraction(b, c) => add_to_hours(amount, try!(sub(b, c))), } @@ -729,12 +734,12 @@ fn add_to_days(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Days( a + amount)), TT::Months(a) => Ok(TT::Days( a * 30 + amount)), TT::Years(a) => Ok(TT::Days( a * 30 * 12 + amount)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotAdd(TT::Days(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Days(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Days(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Days(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Days(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Days(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotAdd(TT::Days(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotAdd(TT::Days(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotAdd(TT::Days(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotAdd(TT::Days(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotAdd(TT::Days(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotAdd(TT::Days(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => add_to_days(amount, try!(add(b, c))), TT::Subtraction(b, c) => add_to_days(amount, try!(sub(b, c))), } @@ -750,12 +755,12 @@ fn add_to_months(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Days( a + amount * 30)), TT::Months(a) => Ok(TT::Months( a + amount)), TT::Years(a) => Ok(TT::Months( a * 12 + amount)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotAdd(TT::Months(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Months(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Months(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Months(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Months(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Months(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotAdd(TT::Months(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotAdd(TT::Months(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotAdd(TT::Months(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotAdd(TT::Months(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotAdd(TT::Months(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotAdd(TT::Months(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => add_to_months(amount, try!(add(b, c))), TT::Subtraction(b, c) => add_to_months(amount, try!(sub(b, c))), } @@ -771,12 +776,12 @@ fn add_to_years(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Days( a + amount * 12 * 30)), TT::Months(a) => Ok(TT::Months( a + amount * 12)), TT::Years(a) => Ok(TT::Years( a + amount)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotAdd(TT::Years(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Years(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Years(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Years(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Years(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Years(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotAdd(TT::Years(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotAdd(TT::Years(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotAdd(TT::Years(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotAdd(TT::Years(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotAdd(TT::Years(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotAdd(TT::Years(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => add_to_years(amount, try!(add(b, c))), TT::Subtraction(b, c) => add_to_years(amount, try!(sub(b, c))), } @@ -798,8 +803,8 @@ fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Minutes(a) => { @@ -814,8 +819,8 @@ fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Hours(a) => { @@ -830,8 +835,8 @@ fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Days(a) => { @@ -846,8 +851,8 @@ fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Months(a) => { @@ -862,8 +867,8 @@ fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Years(a) => { @@ -878,16 +883,16 @@ fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, - TT::Moment(m) => Err(KE::from_kind(KEK::CannotAdd(TT::Moment(mom), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Moment(mom), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Moment(mom), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Moment(mom), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Moment(mom), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotAdd(TT::Moment(mom), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotAdd(TT::Moment(mom), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotAdd(TT::Moment(mom), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotAdd(TT::Moment(mom), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotAdd(TT::Moment(mom), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotAdd(TT::Moment(mom), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotAdd(TT::Moment(mom), TT::EndOfMinute(e)))), TT::Addition(a, b) => add_to_moment(mom, try!(add(a, b))), TT::Subtraction(a, b) => add_to_moment(mom, try!(sub(a, b))), } @@ -919,21 +924,21 @@ fn sub(a: Box, b: Box) -> Result { .and_then(|bx| sub(Box::new(other), bx)) .and_then(|rx| add(Box::new(rx), b)), - (TT::EndOfYear(e), other) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfYear(e)))), - (other, TT::EndOfYear(e)) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfYear(e)))), + (TT::EndOfYear(e), other) => Err(Error::from(KEK::CannotSub(other, TT::EndOfYear(e)))), + (other, TT::EndOfYear(e)) => Err(Error::from(KEK::CannotSub(other, TT::EndOfYear(e)))), - (TT::EndOfMonth(e), other) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfMonth(e)))), - (other, TT::EndOfMonth(e)) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfMonth(e)))), + (TT::EndOfMonth(e), other) => Err(Error::from(KEK::CannotSub(other, TT::EndOfMonth(e)))), + (other, TT::EndOfMonth(e)) => Err(Error::from(KEK::CannotSub(other, TT::EndOfMonth(e)))), - (TT::EndOfDay(e), other) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfDay(e)))), - (other, TT::EndOfDay(e)) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfDay(e)))), + (TT::EndOfDay(e), other) => Err(Error::from(KEK::CannotSub(other, TT::EndOfDay(e)))), + (other, TT::EndOfDay(e)) => Err(Error::from(KEK::CannotSub(other, TT::EndOfDay(e)))), - (TT::EndOfHour(e), other) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfHour(e)))), - (other, TT::EndOfHour(e)) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfHour(e)))), + (TT::EndOfHour(e), other) => Err(Error::from(KEK::CannotSub(other, TT::EndOfHour(e)))), + (other, TT::EndOfHour(e)) => Err(Error::from(KEK::CannotSub(other, TT::EndOfHour(e)))), - (TT::EndOfMinute(e), other) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfMinute(e)))), + (TT::EndOfMinute(e), other) => Err(Error::from(KEK::CannotSub(other, TT::EndOfMinute(e)))), // unreachable, but for completeness - //(other, TT::EndOfMinute(e)) => Err(KE::from_kind(KEK::CannotSub(other, TT::EndOfMinute(e)))), + //(other, TT::EndOfMinute(e)) => Err(Error::from(KEK::CannotSub(other, TT::EndOfMinute(e)))), } } @@ -947,12 +952,12 @@ fn sub_from_seconds(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Seconds(amount - a * 60 * 60 * 24)), TT::Months(a) => Ok(TT::Seconds(amount - a * 60 * 60 * 24 * 30)), TT::Years(a) => Ok(TT::Seconds(amount - a * 60 * 60 * 24 * 30 * 12)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotSub(TT::Seconds(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotSub(TT::Seconds(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotSub(TT::Seconds(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotSub(TT::Seconds(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotSub(TT::Seconds(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotSub(TT::Seconds(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotSub(TT::Seconds(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotSub(TT::Seconds(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotSub(TT::Seconds(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotSub(TT::Seconds(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotSub(TT::Seconds(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotSub(TT::Seconds(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => sub_from_seconds(amount, try!(add(b, c))), TT::Subtraction(b, c) => sub_from_seconds(amount, try!(sub(b, c))), } @@ -968,12 +973,12 @@ fn sub_from_minutes(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Minutes(amount - a * 60 * 24)), TT::Months(a) => Ok(TT::Minutes(amount - a * 60 * 24 * 30)), TT::Years(a) => Ok(TT::Minutes(amount - a * 60 * 24 * 30 * 12)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotSub(TT::Minutes(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotSub(TT::Minutes(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotSub(TT::Minutes(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotSub(TT::Minutes(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotSub(TT::Minutes(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotSub(TT::Minutes(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotSub(TT::Minutes(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotSub(TT::Minutes(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotSub(TT::Minutes(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotSub(TT::Minutes(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotSub(TT::Minutes(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotSub(TT::Minutes(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => sub_from_minutes(amount, try!(add(b, c))), TT::Subtraction(b, c) => sub_from_minutes(amount, try!(sub(b, c))), } @@ -989,12 +994,12 @@ fn sub_from_hours(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Hours(amount - a * 24)), TT::Months(a) => Ok(TT::Hours(amount - a * 24 * 30)), TT::Years(a) => Ok(TT::Hours(amount - a * 24 * 30 * 12)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotSub(TT::Hours(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotSub(TT::Hours(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotSub(TT::Hours(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotSub(TT::Hours(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotSub(TT::Hours(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotSub(TT::Hours(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotSub(TT::Hours(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotSub(TT::Hours(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotSub(TT::Hours(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotSub(TT::Hours(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotSub(TT::Hours(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotSub(TT::Hours(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => sub_from_hours(amount, try!(add(b, c))), TT::Subtraction(b, c) => sub_from_hours(amount, try!(sub(b, c))), } @@ -1010,12 +1015,12 @@ fn sub_from_days(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Days(amount - a)), TT::Months(a) => Ok(TT::Days(amount - a * 30)), TT::Years(a) => Ok(TT::Days(amount - a * 30 * 12)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotSub(TT::Days(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotSub(TT::Days(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotSub(TT::Days(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotSub(TT::Days(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotSub(TT::Days(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotSub(TT::Days(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotSub(TT::Days(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotSub(TT::Days(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotSub(TT::Days(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotSub(TT::Days(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotSub(TT::Days(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotSub(TT::Days(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => sub_from_days(amount, try!(add(b, c))), TT::Subtraction(b, c) => sub_from_days(amount, try!(sub(b, c))), } @@ -1031,12 +1036,12 @@ fn sub_from_months(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Days(amount * 30 - a)), TT::Months(a) => Ok(TT::Months(amount - a)), TT::Years(a) => Ok(TT::Months(amount - a * 12)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotSub(TT::Months(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotSub(TT::Months(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotSub(TT::Months(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotSub(TT::Months(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotSub(TT::Months(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotSub(TT::Months(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotSub(TT::Months(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotSub(TT::Months(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotSub(TT::Months(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotSub(TT::Months(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotSub(TT::Months(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotSub(TT::Months(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => sub_from_months(amount, try!(add(b, c))), TT::Subtraction(b, c) => sub_from_months(amount, try!(sub(b, c))), } @@ -1052,12 +1057,12 @@ fn sub_from_years(amount: i64, tt: TimeType) -> Result { TT::Days(a) => Ok(TT::Days(amount * 12 * 30 - a)), TT::Months(a) => Ok(TT::Months(amount * 12 - a)), TT::Years(a) => Ok(TT::Years(amount - a)), - TT::Moment(m) => Err(KE::from_kind(KEK::CannotSub(TT::Years(amount), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotSub(TT::Years(amount), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotSub(TT::Years(amount), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotSub(TT::Years(amount), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotSub(TT::Years(amount), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotSub(TT::Years(amount), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotSub(TT::Years(amount), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotSub(TT::Years(amount), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotSub(TT::Years(amount), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotSub(TT::Years(amount), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotSub(TT::Years(amount), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotSub(TT::Years(amount), TT::EndOfMinute(e)))), TT::Addition(b, c) => sub_from_years(amount, try!(add(b, c))), TT::Subtraction(b, c) => sub_from_years(amount, try!(sub(b, c))), } @@ -1079,8 +1084,8 @@ fn sub_from_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, d as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Minutes(a) => { @@ -1095,8 +1100,8 @@ fn sub_from_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, d as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Hours(a) => { @@ -1111,8 +1116,8 @@ fn sub_from_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, d as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Days(a) => { @@ -1127,8 +1132,8 @@ fn sub_from_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, d as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Months(a) => { @@ -1143,8 +1148,8 @@ fn sub_from_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, d as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, TT::Years(a) => { @@ -1159,16 +1164,16 @@ fn sub_from_moment(mom: NaiveDateTime, tt: TimeType) -> Result { let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(KEK::OutOfBounds(y as i32, mo as u32, d as u32, h as u32, mi as u32, s as u32).into()) - .map_err(KE::from_kind)?; + .ok_or(KEK::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, - TT::Moment(m) => Err(KE::from_kind(KEK::CannotSub(TT::Moment(mom), TT::Moment(m)))), - TT::EndOfYear(e) => Err(KE::from_kind(KEK::CannotSub(TT::Moment(mom), TT::EndOfYear(e)))), - TT::EndOfMonth(e) => Err(KE::from_kind(KEK::CannotSub(TT::Moment(mom), TT::EndOfMonth(e)))), - TT::EndOfDay(e) => Err(KE::from_kind(KEK::CannotSub(TT::Moment(mom), TT::EndOfDay(e)))), - TT::EndOfHour(e) => Err(KE::from_kind(KEK::CannotSub(TT::Moment(mom), TT::EndOfHour(e)))), - TT::EndOfMinute(e) => Err(KE::from_kind(KEK::CannotSub(TT::Moment(mom), TT::EndOfMinute(e)))), + TT::Moment(m) => Err(Error::from(KEK::CannotSub(TT::Moment(mom), TT::Moment(m)))), + TT::EndOfYear(e) => Err(Error::from(KEK::CannotSub(TT::Moment(mom), TT::EndOfYear(e)))), + TT::EndOfMonth(e) => Err(Error::from(KEK::CannotSub(TT::Moment(mom), TT::EndOfMonth(e)))), + TT::EndOfDay(e) => Err(Error::from(KEK::CannotSub(TT::Moment(mom), TT::EndOfDay(e)))), + TT::EndOfHour(e) => Err(Error::from(KEK::CannotSub(TT::Moment(mom), TT::EndOfHour(e)))), + TT::EndOfMinute(e) => Err(Error::from(KEK::CannotSub(TT::Moment(mom), TT::EndOfMinute(e)))), TT::Addition(a, b) => sub_from_moment(mom, try!(add(a, b))), TT::Subtraction(a, b) => sub_from_moment(mom, try!(sub(a, b))), } @@ -1178,6 +1183,7 @@ fn sub_from_moment(mom: NaiveDateTime, tt: TimeType) -> Result { mod tests { use chrono::NaiveDate; use super::TimeType as TT; + use error::ErrorKind; #[test] fn test_addition_of_seconds() { @@ -1892,7 +1898,10 @@ mod tests { assert!(res.is_err()); let res = res.unwrap_err(); - assert_eq!("Cannot add", res.kind().description()); + assert!(match res.downcast_ref::() { + Some(&ErrorKind::CannotAdd(..)) => true, + _ => false, + }); } #[test] @@ -1905,7 +1914,10 @@ mod tests { assert!(res.is_err()); let res = res.unwrap_err(); - assert_eq!("Cannot subtract", res.kind().description()); + assert!(match res.downcast_ref::() { + Some(&ErrorKind::CannotSub(..)) => true, + _ => false, + }); } } -- cgit v1.2.3