summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-09-02 15:40:07 +0200
committerMatthias Beyer <mail@beyermatthias.de>2017-09-02 15:40:15 +0200
commit3fd1cb16b0bb8bc1924479df3a2dc024640b4569 (patch)
treef52364e337d1ecc5538cb4ade8e0afe378dd65d3
parent3d5211cf485cd21a277c9ae525174adf49b25c98 (diff)
Implement subtraction
-rw-r--r--src/timetype.rs19
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 {