From 431b34dac7e59f1251cdb151e44ee6e604afe2d0 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 13 Jun 2020 22:52:07 -0400 Subject: [PATCH] Wired up the OwnedLiterals to the math functions. --- src/renderer/math.rs | 6 +-- src/renderer/parameters_context.rs | 68 +++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/renderer/math.rs b/src/renderer/math.rs index 9a74f24..8161019 100644 --- a/src/renderer/math.rs +++ b/src/renderer/math.rs @@ -138,21 +138,21 @@ impl Rem for MathNumber { } impl MathNumber { - fn math_abs(&self) -> Option { + pub fn math_abs(&self) -> Option { match self { MathNumber::Failure => None, MathNumber::Integer(num) => num.abs().try_into().ok(), MathNumber::Decimal(num) => Some(OwnedLiteral::LFloat(num.abs())), } } - fn math_floor(&self) -> Option { + pub fn math_floor(&self) -> Option { match self { MathNumber::Failure => None, MathNumber::Integer(num) => (*num).try_into().ok(), MathNumber::Decimal(num) => Some(OwnedLiteral::LFloat(num.floor())), } } - fn math_ceil(&self) -> Option { + pub fn math_ceil(&self) -> Option { match self { MathNumber::Failure => None, MathNumber::Integer(num) => (*num).try_into().ok(), diff --git a/src/renderer/parameters_context.rs b/src/renderer/parameters_context.rs index fa747f9..897f404 100644 --- a/src/renderer/parameters_context.rs +++ b/src/renderer/parameters_context.rs @@ -375,30 +375,84 @@ impl CompareContextElement for OwnedLiteral { } fn math_subtract<'a>(&self, other: &dyn ContextElement) -> Option> { - todo!() + // If its an OwnedLiteral then subtract them directly, + // otherwise defer to the other type's implementation of + // CompareContextElement since the end user could add any + // type. + match other.to_any().downcast_ref::() { + None => other.math_subtract(self), + Some(other_literal) => match (self, other_literal) { + (OwnedLiteral::LString(_), _) | (_, OwnedLiteral::LString(_)) => None, + (_, _) => (std::convert::Into::::into(self) + - std::convert::Into::::into(other_literal)) + .map(IceResult::from_owned), + }, + } } fn math_multiply<'a>(&self, other: &dyn ContextElement) -> Option> { - todo!() + // If its an OwnedLiteral then multiply them directly, + // otherwise defer to the other type's implementation of + // CompareContextElement since the end user could add any + // type. + match other.to_any().downcast_ref::() { + None => other.math_multiply(self), + Some(other_literal) => match (self, other_literal) { + (OwnedLiteral::LString(_), _) | (_, OwnedLiteral::LString(_)) => None, + (_, _) => (std::convert::Into::::into(self) + * std::convert::Into::::into(other_literal)) + .map(IceResult::from_owned), + }, + } } fn math_divide<'a>(&self, other: &dyn ContextElement) -> Option> { - todo!() + // If its an OwnedLiteral then divide them directly, otherwise + // defer to the other type's implementation of + // CompareContextElement since the end user could add any + // type. + match other.to_any().downcast_ref::() { + None => other.math_divide(self), + Some(other_literal) => match (self, other_literal) { + (OwnedLiteral::LString(_), _) | (_, OwnedLiteral::LString(_)) => None, + (_, _) => (std::convert::Into::::into(self) + / std::convert::Into::::into(other_literal)) + .map(IceResult::from_owned), + }, + } } fn math_modulus<'a>(&self, other: &dyn ContextElement) -> Option> { - todo!() + // If its an OwnedLiteral then modulus them directly, + // otherwise defer to the other type's implementation of + // CompareContextElement since the end user could add any + // type. + match other.to_any().downcast_ref::() { + None => other.math_modulus(self), + Some(other_literal) => match (self, other_literal) { + (OwnedLiteral::LString(_), _) | (_, OwnedLiteral::LString(_)) => None, + (_, _) => (std::convert::Into::::into(self) + % std::convert::Into::::into(other_literal)) + .map(IceResult::from_owned), + }, + } } fn math_abs<'a>(&self) -> Option> { - todo!() + std::convert::Into::::into(self) + .math_abs() + .map(IceResult::from_owned) } fn math_floor<'a>(&self) -> Option> { - todo!() + std::convert::Into::::into(self) + .math_floor() + .map(IceResult::from_owned) } fn math_ceil<'a>(&self) -> Option> { - todo!() + std::convert::Into::::into(self) + .math_ceil() + .map(IceResult::from_owned) } }