diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-07 16:28:26 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-07 16:28:26 +0200 |
commit | fdfcfad129fb69b60e9b7a1e12a616b05a8976d7 (patch) | |
tree | 83d2ffc407261bcab463538336a9713b5b3903db | |
parent | 6d075aa0d87d2ae474c035b6c9d6d7490ee214e6 (diff) |
Impl mixed subtraction/addition evaluation
-rw-r--r-- | src/timetype.rs | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/src/timetype.rs b/src/timetype.rs index 80a6c57..17e173b 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -129,13 +129,17 @@ impl TimeType { } fn calculate(self) -> Result<TimeType> { - use timetype::TimeType as TT; + do_calculate(self) + } +} - match self { - TT::Addition(a, b) => add(a, b), - TT::Subtraction(a, b) => sub(a, b), - x => Ok(x) - } +fn do_calculate(tt: TimeType) -> Result<TimeType> { + use timetype::TimeType as TT; + + match tt { + TT::Addition(a, b) => add(a, b), + TT::Subtraction(a, b) => sub(a, b), + x => Ok(x) } } @@ -150,6 +154,13 @@ fn add(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> { (other, TT::Addition(a, b)) => add(a, b) .map(Box::new) .and_then(|bx| add(Box::new(other), bx)), + (TT::Subtraction(a, b), other) => sub(a, b) + .map(Box::new) + .and_then(|bx| add(Box::new(other), bx)), + (other, TT::Subtraction(a, b)) => do_calculate(*a) + .map(Box::new) + .and_then(|bx| add(Box::new(other), bx)) + .and_then(|rx| sub(Box::new(rx), b)), (thing, TT::Moment(mom)) => Err(KE::from_kind(KEK::CannotAdd(thing, TT::Moment(mom)))), others => unimplemented!(), } @@ -166,6 +177,13 @@ fn sub(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> { (other, TT::Subtraction(a, b)) => sub(a, b) .map(Box::new) .and_then(|bx| sub(Box::new(other), bx)), + (TT::Addition(a, b), other) => add(a, b) + .map(Box::new) + .and_then(|bx| sub(bx, Box::new(other))), + (other, TT::Addition(a, b)) => do_calculate(*a) + .map(Box::new) + .and_then(|bx| sub(Box::new(other), bx)) + .and_then(|rx| add(Box::new(rx), b)), (thing, TT::Moment(mom)) => Err(KE::from_kind(KEK::CannotSub(thing, TT::Moment(mom)))), others => unimplemented!(), } |