MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / eval_binary_op

Function eval_binary_op

nodedb-query/src/expr/binary.rs:44–113  ·  view source on GitHub ↗
(left: &Value, op: BinaryOp, right: &Value)

Source from the content-addressed store, hash-verified

42}
43
44pub(super) fn eval_binary_op(left: &Value, op: BinaryOp, right: &Value) -> Value {
45 match op {
46 // Arithmetic: prefer Decimal when either operand is Decimal to avoid f64 drift.
47 BinaryOp::Add | BinaryOp::Sub | BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => {
48 let left_is_decimal = matches!(left, Value::Decimal(_));
49 let right_is_decimal = matches!(right, Value::Decimal(_));
50 if left_is_decimal || right_is_decimal {
51 match (value_to_decimal(left), value_to_decimal(right)) {
52 (Some(a), Some(b)) => return decimal_arith(a, op, b),
53 _ => return Value::Null,
54 }
55 }
56 // Both operands are non-Decimal: use f64 path.
57 match op {
58 BinaryOp::Add => match (value_to_f64(left, true), value_to_f64(right, true)) {
59 (Some(a), Some(b)) => to_value_number(a + b),
60 _ => Value::Null,
61 },
62 BinaryOp::Sub => match (value_to_f64(left, true), value_to_f64(right, true)) {
63 (Some(a), Some(b)) => to_value_number(a - b),
64 _ => Value::Null,
65 },
66 BinaryOp::Mul => match (value_to_f64(left, true), value_to_f64(right, true)) {
67 (Some(a), Some(b)) => to_value_number(a * b),
68 _ => Value::Null,
69 },
70 BinaryOp::Div => match (value_to_f64(left, true), value_to_f64(right, true)) {
71 (Some(a), Some(b)) => {
72 if b == 0.0 {
73 Value::Null
74 } else {
75 to_value_number(a / b)
76 }
77 }
78 _ => Value::Null,
79 },
80 BinaryOp::Mod => match (value_to_f64(left, true), value_to_f64(right, true)) {
81 (Some(a), Some(b)) => {
82 if b == 0.0 {
83 Value::Null
84 } else {
85 to_value_number(a % b)
86 }
87 }
88 _ => Value::Null,
89 },
90 _ => Value::Null,
91 }
92 }
93 BinaryOp::Concat => {
94 let ls = value_to_display_string(left);
95 let rs = value_to_display_string(right);
96 Value::String(format!("{ls}{rs}"))
97 }
98 BinaryOp::Eq => Value::Bool(coerced_eq(left, right)),
99 BinaryOp::NotEq => Value::Bool(!coerced_eq(left, right)),
100 BinaryOp::Gt => Value::Bool(compare_values(left, right) == std::cmp::Ordering::Greater),
101 BinaryOp::GtEq => {

Callers 1

eval_scopeMethod · 0.70

Calls 8

value_to_decimalFunction · 0.85
decimal_arithFunction · 0.85
value_to_f64Function · 0.85
to_value_numberFunction · 0.85
value_to_display_stringFunction · 0.85
coerced_eqFunction · 0.50
compare_valuesFunction · 0.50
is_truthyFunction · 0.50

Tested by

no test coverage detected