Skip to main content

apply_operator

Function apply_operator 

Source
pub fn apply_operator(operator: &Operator, left: &Value, right: &Value) -> bool
Expand description

Apply operator to two values using the specified operator type

This is the main operator application interface that dispatches to the appropriate operator function based on the Operator enum variant. This function serves as the primary entry point for operator evaluation in magic rule processing.

§Arguments

  • operator - The operator to apply (Equal, NotEqual, LessThan, GreaterThan, LessEqual, GreaterEqual, BitwiseAnd, BitwiseAndMask, BitwiseXor, BitwiseNot, or AnyValue)
  • left - The left-hand side value (typically from file data)
  • right - The right-hand side value (typically from magic rule)

§Returns

true if the operator condition is satisfied, false otherwise

§Examples

use libmagic_rs::parser::ast::{Operator, Value};
use libmagic_rs::evaluator::operators::apply_operator;

// Equality comparison
assert!(apply_operator(
    &Operator::Equal,
    &Value::Uint(42),
    &Value::Uint(42)
));

// Inequality comparison
assert!(apply_operator(
    &Operator::NotEqual,
    &Value::Uint(42),
    &Value::Uint(24)
));

// Less-than comparison
assert!(apply_operator(
    &Operator::LessThan,
    &Value::Uint(5),
    &Value::Uint(10)
));

// Greater-than comparison
assert!(apply_operator(
    &Operator::GreaterThan,
    &Value::Uint(10),
    &Value::Uint(5)
));

// Less-than-or-equal comparison
assert!(apply_operator(
    &Operator::LessEqual,
    &Value::Uint(10),
    &Value::Uint(10)
));

// Greater-than-or-equal comparison
assert!(apply_operator(
    &Operator::GreaterEqual,
    &Value::Uint(10),
    &Value::Uint(10)
));

// Bitwise AND operation
assert!(apply_operator(
    &Operator::BitwiseAnd,
    &Value::Uint(0xFF),
    &Value::Uint(0x0F)
));

// Cross-type integer coercion
assert!(apply_operator(
    &Operator::Equal,
    &Value::Uint(42),
    &Value::Int(42)
));

// Bitwise XOR, NOT, and any-value
assert!(apply_operator(
    &Operator::BitwiseXor,
    &Value::Uint(0xFF),
    &Value::Uint(0x0F)
));
assert!(apply_operator(
    &Operator::AnyValue,
    &Value::Uint(0),
    &Value::Uint(0)
));