diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-02 15:40:30 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-02 15:40:30 +0200 |
commit | 6ed09b627e62cf719d119dd85064188605976369 (patch) | |
tree | f52364e337d1ecc5538cb4ade8e0afe378dd65d3 | |
parent | 7282611edefaa1ca014de2a41e8417ad8aec057b (diff) | |
parent | 3fd1cb16b0bb8bc1924479df3a2dc024640b4569 (diff) |
Merge branch 'impl-calculate'
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/error.rs | 21 | ||||
-rw-r--r-- | src/lib.rs | 3 | ||||
-rw-r--r-- | src/result.rs | 5 | ||||
-rw-r--r-- | src/timetype.rs | 115 |
5 files changed, 144 insertions, 1 deletions
@@ -14,3 +14,4 @@ repository = "https://github.com/matthiasbeyer/kairos" [dependencies] chrono = "0.4" +error-chain = "0.3" diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..116a8eb --- /dev/null +++ b/src/error.rs @@ -0,0 +1,21 @@ +error_chain! { + types { + KairosError, KairosErrorKind, ResultExt, Result; + } + + links { + } + + foreign_links { + } + + errors { + + UnknownError { + description("Unknown Error") + display("Unknown Error") + } + + } + +} @@ -1,4 +1,5 @@ - +#[macro_use] +extern crate error_chain; extern crate chrono; pub mod timetype; diff --git a/src/result.rs b/src/result.rs new file mode 100644 index 0000000..835cb7d --- /dev/null +++ b/src/result.rs @@ -0,0 +1,5 @@ +use std::result::Result as RResult; + +use error::KairosError; + +pub type Result<T> = RResult<T, KairosError>; diff --git a/src/timetype.rs b/src/timetype.rs index 4e2575e..e064f35 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -6,6 +6,8 @@ use chrono::NaiveDateTime; use std::ops::Add; use std::ops::Sub; +use result::Result; + /// A Type of Time, currently based on chrono::NaiveDateTime #[derive(Debug)] pub enum TimeType { @@ -39,6 +41,54 @@ impl Sub for TimeType { } } +impl TimeType { + + fn calculate(self) -> Result<TimeType> { + use timetype::TimeType as TT; + + match self { + TT::Addition(a, b) => add(a, b), + TT::Subtraction(a, b) => sub(a, b), + x => Ok(x) + } + } +} + +fn add(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> { + use timetype::TimeType as TT; + + match (*a, *b) { + (TT::Seconds(a), TT::Seconds(b)) => Ok(TT::Seconds(a + b)), + (TT::Minutes(a), TT::Minutes(b)) => unimplemented!(), + (TT::Hours(a), TT::Hours(b)) => unimplemented!(), + (TT::Days(a), TT::Days(b)) => unimplemented!(), + (TT::Weeks(a), TT::Weeks(b)) => unimplemented!(), + (TT::Months(a), TT::Months(b)) => unimplemented!(), + (TT::Years(a), TT::Years(b)) => unimplemented!(), + (TT::Addition(a, b), other) => add(a, b) + .map(Box::new) + .and_then(|bx| add(bx, Box::new(other))), + others => unimplemented!(), + } +} + +fn sub(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> { + use timetype::TimeType as TT; + + match (*a, *b) { + (TT::Seconds(a), TT::Seconds(b)) => Ok(TT::Seconds(a - b)), + (TT::Minutes(a), TT::Minutes(b)) => unimplemented!(), + (TT::Hours(a), TT::Hours(b)) => unimplemented!(), + (TT::Days(a), TT::Days(b)) => unimplemented!(), + (TT::Weeks(a), TT::Weeks(b)) => unimplemented!(), + (TT::Months(a), TT::Months(b)) => unimplemented!(), + (TT::Years(a), TT::Years(b)) => unimplemented!(), + (TT::Subtraction(a, b), other) => sub(a, b) + .map(Box::new) + .and_then(|bx| sub(bx, Box::new(other))), + others => unimplemented!(), + } +} #[cfg(test)] mod tests { @@ -124,5 +174,70 @@ mod tests { } } + #[test] + fn test_addition_of_seconds_calculate() { + let a = TT::Seconds(0); + let b = TT::Seconds(1); + + let c = (a + b).calculate(); + + assert!(c.is_ok()); + let c = c.unwrap(); + + match c { + TT::Seconds(1) => assert!(true), + _ => assert!(false, "Addition failed"), + } + } + + #[test] + fn test_addition_of_seconds_multiple_calculate() { + let a = TT::Seconds(0); + let b = TT::Seconds(1); + let c = TT::Seconds(2); + + let d = (a + b + c).calculate(); + + assert!(d.is_ok()); + let d = d.unwrap(); + + match d { + TT::Seconds(3) => assert!(true), + _ => assert!(false, "Addition failed"), + } + } + + #[test] + fn test_subtraction_of_seconds_calculate() { + let a = TT::Seconds(5); + let b = TT::Seconds(3); + + let c = (a - b).calculate(); + + assert!(c.is_ok()); + let c = c.unwrap(); + + match c { + TT::Seconds(2) => assert!(true), + _ => assert!(false, "Subtraction failed"), + } + } + + #[test] + fn test_subtraction_of_seconds_multiple_calculate() { + let a = TT::Seconds(3); + let b = TT::Seconds(2); + let c = TT::Seconds(1); + + let d = (a - b - c).calculate(); + + assert!(d.is_ok()); + let d = d.unwrap(); + + match d { + TT::Seconds(0) => assert!(true), + _ => assert!(false, "Subtraction failed"), + } + } } |