diff options
Diffstat (limited to 'src/timetype.rs')
-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!(), } |