diff options
Diffstat (limited to 'src/timetype.rs')
-rw-r--r-- | src/timetype.rs | 115 |
1 files changed, 115 insertions, 0 deletions
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"), + } + } } |