From 603888c20041cda29e11b652e617f58157d80682 Mon Sep 17 00:00:00 2001 From: Toon Willems Date: Fri, 1 Nov 2024 13:42:05 +0100 Subject: [PATCH] cleanup evaluate --- expression-core/src/evaluate.rs | 90 ++++++++++++++------------------- 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/expression-core/src/evaluate.rs b/expression-core/src/evaluate.rs index fefa8db..79eaef5 100644 --- a/expression-core/src/evaluate.rs +++ b/expression-core/src/evaluate.rs @@ -87,61 +87,49 @@ impl Function { Ok(ExpressionValue::String(evaluated_args.concat())) } - Function::Ceil(expr, None) => { - let evaluated_decimal = expr.evaluate(event)?.to_decimal()?; - Ok(ExpressionValue::Number( - evaluated_decimal.with_scale_round(0, RoundingMode::Ceiling), - )) - } - Function::Floor(expr, None) => { - let evaluated_decimal = expr.evaluate(event)?.to_decimal()?; - Ok(ExpressionValue::Number( - evaluated_decimal.with_scale_round(0, RoundingMode::Floor), - )) - } - Function::Round(expr, None) => { - let evaluated_decimal = expr.evaluate(event)?.to_decimal()?; - Ok(ExpressionValue::Number( - evaluated_decimal.with_scale_round(0, RoundingMode::HalfUp), - )) - } - Function::Round(expr, Some(digit_expr)) => { - let evaluated_decimal = expr.evaluate(event)?.to_decimal()?; - let round_digits = digit_expr - .evaluate(event)? - .to_decimal()? - .to_i64() - .ok_or(ExpressionError::ExpectedDecimal)?; - Ok(ExpressionValue::Number( - evaluated_decimal.with_scale_round(round_digits, RoundingMode::HalfUp), - )) - } - Function::Ceil(expr, Some(digit_expr)) => { - let evaluated_decimal = expr.evaluate(event)?.to_decimal()?; - let round_digits = digit_expr - .evaluate(event)? - .to_decimal()? - .to_i64() - .ok_or(ExpressionError::ExpectedDecimal)?; - Ok(ExpressionValue::Number( - evaluated_decimal.with_scale_round(round_digits, RoundingMode::Ceiling), - )) - } - Function::Floor(expr, Some(digit_expr)) => { - let evaluated_decimal = expr.evaluate(event)?.to_decimal()?; - let round_digits = digit_expr - .evaluate(event)? - .to_decimal()? - .to_i64() - .ok_or(ExpressionError::ExpectedDecimal)?; - Ok(ExpressionValue::Number( - evaluated_decimal.with_scale_round(round_digits, RoundingMode::Floor), - )) - } + Function::Round(expr, digit_expr) => evaluate_with_rounding_mode( + expr.as_ref(), + digit_expr.as_ref().map(AsRef::as_ref), + event, + RoundingMode::HalfUp, + ), + Function::Ceil(expr, digit_expr) => evaluate_with_rounding_mode( + expr.as_ref(), + digit_expr.as_ref().map(AsRef::as_ref), + event, + RoundingMode::Ceiling, + ), + Function::Floor(expr, digit_expr) => evaluate_with_rounding_mode( + expr.as_ref(), + digit_expr.as_ref().map(AsRef::as_ref), + event, + RoundingMode::Floor, + ), } } } +fn evaluate_with_rounding_mode( + expr: &Expression, + digits: Option<&Expression>, + event: &Event, + rounding_mode: RoundingMode, +) -> EvaluationResult { + let evaluated_decimal = expr.evaluate(event)?.to_decimal()?; + let round_digits = match digits { + Some(digit_expr) => digit_expr + .evaluate(event)? + .to_decimal()? + .to_i64() + .ok_or(ExpressionError::ExpectedDecimal)?, + None => 0, + }; + + Ok(ExpressionValue::Number( + evaluated_decimal.with_scale_round(round_digits, rounding_mode), + )) +} + impl EventAttribute { pub fn evaluate(&self, event: &Event) -> EvaluationResult { let evaluated_attribute = match self {