Skip to main content

compare_values

Function compare_values 

Source
pub fn compare_values(left: &Value, right: &Value) -> Option<Ordering>
Expand description

Compare two values and return their ordering, if comparable

Returns Some(Ordering) for same-type comparisons (integers, strings, bytes) and cross-type integer comparisons (via i128 coercion). Returns None for incomparable type combinations.

ยงExamples

use std::cmp::Ordering;
use libmagic_rs::parser::ast::Value;
use libmagic_rs::evaluator::operators::compare_values;

assert_eq!(compare_values(&Value::Uint(5), &Value::Uint(10)), Some(Ordering::Less));
assert_eq!(compare_values(&Value::Int(-1), &Value::Uint(0)), Some(Ordering::Less));
assert_eq!(compare_values(&Value::Uint(42), &Value::Int(42)), Some(Ordering::Equal));
assert_eq!(compare_values(&Value::Uint(1), &Value::String("1".to_string())), None);