diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-02 15:40:07 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-02 15:40:15 +0200 |
commit | 3fd1cb16b0bb8bc1924479df3a2dc024640b4569 (patch) | |
tree | f52364e337d1ecc5538cb4ade8e0afe378dd65d3 | |
parent | 3d5211cf485cd21a277c9ae525174adf49b25c98 (diff) |
Implement subtraction
-rw-r--r-- | src/timetype.rs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/timetype.rs b/src/timetype.rs index 44b1308..e064f35 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -48,7 +48,7 @@ impl TimeType { match self { TT::Addition(a, b) => add(a, b), - TT::Subtraction(a, b) => unimplemented!(), + TT::Subtraction(a, b) => sub(a, b), x => Ok(x) } } @@ -72,6 +72,23 @@ fn add(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> { } } +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 { |