Skip to content

Commit

Permalink
cleanup evaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Nov 1, 2024
1 parent 83b448b commit 603888c
Showing 1 changed file with 39 additions and 51 deletions.
90 changes: 39 additions & 51 deletions expression-core/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExpressionValue> {
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<ExpressionValue> {
let evaluated_attribute = match self {
Expand Down

0 comments on commit 603888c

Please sign in to comment.