datalogic-rs 5.0.0

A fast, type-safe Rust implementation of JSONLogic for evaluating logical rules as JSON. Perfect for business rules engines and dynamic filtering in Rust applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! `+`, `-`, `*` — basic arithmetic with overflow promotion to `f64` and
//! optional datetime/duration support.

use crate::arena::{ContextStack, DataValue, coerce_to_number_cfg, try_coerce_to_integer_cfg};
use crate::{CompiledNode, Engine, Result};
use bumpalo::Bump;
use datavalue::NumberValue;

use super::helpers::{
    ArithOp, FoldState, FoldStepOutcome, NanAction, VariadicFoldSpec, alloc_number,
    coerce_pair_f64, coerce_pair_int, handle_nan, try_int_op, variadic_fold,
};

/// Arena-mode `+`. Handles 0-arg (identity), 1-arg array (sum elements),
/// 1-arg single value (coerce + return), 2-arg (numeric or datetime native),
/// and variadic (sum all args).
#[inline]
pub(crate) fn evaluate_add<'a>(
    args: &'a [CompiledNode],
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
) -> Result<&'a DataValue<'a>> {
    if args.is_empty() {
        return Ok(alloc_number(arena, NumberValue::from_i64(0)));
    }
    if args.len() == 1 {
        return one_arg_arith(&args[0], ctx, engine, arena, ArithOp::Add);
    }
    if args.len() == 2 {
        return add_two_arg(&args[0], &args[1], ctx, engine, arena);
    }
    variadic_fold(
        args,
        ctx,
        engine,
        arena,
        VariadicFoldSpec {
            int_init: 0,
            float_init: 0.0,
            i_combine: i64::checked_add,
            f_combine: |a, b| a + b,
        },
    )
}

#[inline]
fn add_two_arg<'a>(
    a: &'a CompiledNode,
    b: &'a CompiledNode,
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
) -> Result<&'a DataValue<'a>> {
    let a_av = engine.dispatch_node(a, ctx, arena)?;
    let b_av = engine.dispatch_node(b, ctx, arena)?;

    // Integer-preserving fast path (both native Number with i64 values).
    if let (Some(ia), Some(ib)) = (a_av.as_i64(), b_av.as_i64()) {
        return Ok(alloc_number(
            arena,
            try_int_op(ia, ib, i64::checked_add, |x, y| x + y),
        ));
    }

    // Config-aware arena-native coercion (covers bool/null/string operands).
    if let Some((i1, i2)) = coerce_pair_int(a_av, b_av, engine) {
        return Ok(alloc_number(
            arena,
            try_int_op(i1, i2, i64::checked_add, |x, y| x + y),
        ));
    }
    if let Some((f1, f2)) = coerce_pair_f64(a_av, b_av, engine) {
        return Ok(alloc_number(arena, NumberValue::from_f64(f1 + f2)));
    }

    // Datetime / duration arithmetic.
    #[cfg(feature = "datetime")]
    {
        if let Some(av) = crate::operators::datetime::arith::datetime_add(a_av, b_av, arena) {
            return Ok(av);
        }
    }

    // Non-numeric, non-datetime — handle NaN per config.
    let mut sum = 0.0f64;
    for av in [a_av, b_av] {
        if let Some(f) = coerce_to_number_cfg(av, engine) {
            sum += f;
        } else {
            match handle_nan(engine)? {
                NanAction::Skip => {}
                NanAction::ReturnNull => return Ok(crate::arena::singletons::singleton_null()),
            }
        }
    }
    Ok(alloc_number(arena, NumberValue::from_f64(sum)))
}

/// Arena-mode `*`. 0-arg (1), 1-arg array (product), 1-arg scalar,
/// 2-arg (numeric or duration*scalar native), variadic.
#[inline]
pub(crate) fn evaluate_multiply<'a>(
    args: &'a [CompiledNode],
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
) -> Result<&'a DataValue<'a>> {
    if args.is_empty() {
        return Ok(alloc_number(arena, NumberValue::from_i64(1)));
    }
    if args.len() == 1 {
        return one_arg_arith(&args[0], ctx, engine, arena, ArithOp::Multiply);
    }
    if args.len() == 2 {
        return multiply_two_arg(&args[0], &args[1], ctx, engine, arena);
    }
    variadic_fold(
        args,
        ctx,
        engine,
        arena,
        VariadicFoldSpec {
            int_init: 1,
            float_init: 1.0,
            i_combine: i64::checked_mul,
            f_combine: |a, b| a * b,
        },
    )
}

#[inline]
fn multiply_two_arg<'a>(
    a: &'a CompiledNode,
    b: &'a CompiledNode,
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
) -> Result<&'a DataValue<'a>> {
    let a_av = engine.dispatch_node(a, ctx, arena)?;
    let b_av = engine.dispatch_node(b, ctx, arena)?;

    // Integer-preserving fast path.
    if let (Some(ia), Some(ib)) = (a_av.as_i64(), b_av.as_i64()) {
        return Ok(alloc_number(
            arena,
            try_int_op(ia, ib, i64::checked_mul, |x, y| x * y),
        ));
    }

    // Duration * scalar — checked before generic coercion so duration object
    // inputs aren't coerced to None and lost.
    #[cfg(feature = "datetime")]
    {
        if let Some(av) = crate::operators::datetime::arith::datetime_multiply(a_av, b_av, arena) {
            return Ok(av);
        }
    }

    if let Some((i1, i2)) = coerce_pair_int(a_av, b_av, engine) {
        return Ok(alloc_number(
            arena,
            try_int_op(i1, i2, i64::checked_mul, |x, y| x * y),
        ));
    }
    if let Some((f1, f2)) = coerce_pair_f64(a_av, b_av, engine) {
        return Ok(alloc_number(arena, NumberValue::from_f64(f1 * f2)));
    }

    // Non-numeric — handle NaN per config (multiplicative identity is 1).
    let mut product = 1.0f64;
    for av in [a_av, b_av] {
        if let Some(f) = coerce_to_number_cfg(av, engine) {
            product *= f;
        } else {
            match handle_nan(engine)? {
                NanAction::Skip => {}
                NanAction::ReturnNull => return Ok(crate::arena::singletons::singleton_null()),
            }
        }
    }
    Ok(alloc_number(arena, NumberValue::from_f64(product)))
}

/// Arena-mode `-`. Handles 1-arg (negate / array fold), 2-arg primary
/// (numeric or datetime), and variadic (left-fold subtractive).
#[inline]
pub(crate) fn evaluate_subtract<'a>(
    args: &'a [CompiledNode],
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
) -> Result<&'a DataValue<'a>> {
    if args.is_empty() {
        return Err(crate::Error::invalid_args());
    }
    if args.len() == 1 {
        return subtract_one_arg(&args[0], ctx, engine, arena);
    }
    if args.len() == 2 {
        return subtract_two_arg(&args[0], &args[1], ctx, engine, arena);
    }
    subtract_variadic(args, ctx, engine, arena)
}

#[inline]
fn subtract_one_arg<'a>(
    arg: &'a CompiledNode,
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
) -> Result<&'a DataValue<'a>> {
    let av = engine.dispatch_node(arg, ctx, arena)?;

    // Array fold case: (first - second - ...).
    if let DataValue::Array(items) = av {
        if items.is_empty() {
            return Err(crate::Error::invalid_args());
        }
        let mut result = coerce_to_number_cfg(&items[0], engine).ok_or_else(crate::Error::nan)?;
        for elem in &items[1..] {
            let n = coerce_to_number_cfg(elem, engine).ok_or_else(crate::Error::nan)?;
            result -= n;
        }
        return Ok(alloc_number(arena, NumberValue::from_f64(result)));
    }
    // Negate single value (preserve integer typing when possible).
    if let Some(i) = av.as_i64() {
        return Ok(alloc_number(
            arena,
            i.checked_neg()
                .map(NumberValue::from_i64)
                .unwrap_or_else(|| NumberValue::from_f64(-(i as f64))),
        ));
    }
    if let Some(f) = coerce_to_number_cfg(av, engine) {
        return Ok(alloc_number(arena, NumberValue::from_f64(-f)));
    }
    Err(crate::Error::nan())
}

#[inline]
fn subtract_two_arg<'a>(
    a: &'a CompiledNode,
    b: &'a CompiledNode,
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
) -> Result<&'a DataValue<'a>> {
    let a_av = engine.dispatch_node(a, ctx, arena)?;
    let b_av = engine.dispatch_node(b, ctx, arena)?;

    // Integer-preserving fast path.
    if let (Some(ia), Some(ib)) = (a_av.as_i64(), b_av.as_i64()) {
        return Ok(alloc_number(
            arena,
            try_int_op(ia, ib, i64::checked_sub, |x, y| x - y),
        ));
    }

    if let Some((i1, i2)) = coerce_pair_int(a_av, b_av, engine) {
        return Ok(alloc_number(
            arena,
            try_int_op(i1, i2, i64::checked_sub, |x, y| x - y),
        ));
    }
    if let Some((f1, f2)) = coerce_pair_f64(a_av, b_av, engine) {
        return Ok(alloc_number(arena, NumberValue::from_f64(f1 - f2)));
    }

    // Datetime / duration arithmetic.
    #[cfg(feature = "datetime")]
    {
        if let Some(av) = crate::operators::datetime::arith::datetime_subtract(a_av, b_av, arena) {
            return Ok(av);
        }
    }

    Err(crate::Error::nan())
}

/// Variadic (>2) subtract: integer fast path with overflow promotion.
///
/// Coercion strategy: `try_coerce_to_integer_cfg` (permissive) for the int
/// path so numeric strings stay on the int track; `coerce_to_number_cfg` for
/// the float path. The first arg seeds the accumulator and *must* coerce —
/// non-numeric first arg raises `NaN` immediately. Remaining args use the
/// usual NaN-handling config.
#[inline]
fn subtract_variadic<'a>(
    args: &'a [CompiledNode],
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
) -> Result<&'a DataValue<'a>> {
    let first_av = engine.dispatch_node(&args[0], ctx, arena)?;
    let int_init = first_av
        .as_i64()
        .or_else(|| try_coerce_to_integer_cfg(first_av, engine));
    let float_init = match coerce_to_number_cfg(first_av, engine) {
        Some(f) => f,
        None => return Err(crate::Error::nan()),
    };
    let mut state = FoldState::new(int_init.unwrap_or_default(), float_init);
    state.all_int = int_init.is_some();

    for arg in args.iter().skip(1) {
        let av = engine.dispatch_node(arg, ctx, arena)?;
        let int_opt = av
            .as_i64()
            .or_else(|| try_coerce_to_integer_cfg(av, engine));
        let float_opt = if int_opt.is_some() {
            None
        } else {
            coerce_to_number_cfg(av, engine)
        };
        if let FoldStepOutcome::ReturnNull = state.step(
            int_opt,
            float_opt,
            i64::checked_sub,
            std::ops::Sub::sub,
            engine,
        )? {
            return Ok(crate::arena::singletons::singleton_null());
        }
    }
    Ok(state.finalize(arena))
}

/// 1-arg `+` / `*`: literal-array reject, then either array-fold the elements
/// or treat as a single-value sum/product.
fn one_arg_arith<'a>(
    arg: &'a CompiledNode,
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
    op: ArithOp,
) -> Result<&'a DataValue<'a>> {
    // Literal array argument is invalid for + / *. Apply NaN config (default
    // ThrowError → propagates the error up).
    let is_literal_array = matches!(arg, CompiledNode::Array { .. })
        || matches!(
            arg,
            CompiledNode::Value {
                value: datavalue::OwnedDataValue::Array(_),
                ..
            }
        );
    if is_literal_array {
        return match handle_nan(engine)? {
            NanAction::Skip => Ok(alloc_number(
                arena,
                NumberValue::from_i64(op.identity_int()),
            )),
            NanAction::ReturnNull => Ok(crate::arena::singletons::singleton_null()),
        };
    }

    let av = engine.dispatch_node(arg, ctx, arena)?;

    // Array result (e.g. from `var "items"`): fold all elements.
    if let DataValue::Array(items) = av {
        return one_arg_array_fold(items, engine, arena, op);
    }

    // Non-array single value: coerce and return (op identity * coerced).
    if let Some(i) = try_coerce_to_integer_cfg(av, engine) {
        return match op.combine_int(op.identity_int(), i) {
            Some(r) => Ok(alloc_number(arena, NumberValue::from_i64(r))),
            None => Ok(alloc_number(
                arena,
                NumberValue::from_f64(op.combine_f(op.identity_int() as f64, i as f64)),
            )),
        };
    }
    if let Some(f) = coerce_to_number_cfg(av, engine) {
        return Ok(alloc_number(
            arena,
            NumberValue::from_f64(op.combine_f(op.identity_int() as f64, f)),
        ));
    }
    match handle_nan(engine)? {
        NanAction::Skip => Ok(alloc_number(
            arena,
            NumberValue::from_i64(op.identity_int()),
        )),
        NanAction::ReturnNull => Ok(crate::arena::singletons::singleton_null()),
    }
}

/// Fold an arena-resident array under `op` (`+` or `*`) with integer fast
/// path and overflow-to-f64.
///
/// Coercion strategy: `try_coerce_to_integer_cfg` for the int path (so
/// numeric-string elements stay on the int track), `coerce_to_number_cfg`
/// for the float fallback. Identical to `subtract_variadic`'s strategy
/// except the accumulator starts at `op.identity_int()` rather than
/// arg[0].
#[inline]
fn one_arg_array_fold<'a>(
    items: &[DataValue<'a>],
    engine: &Engine,
    arena: &'a Bump,
    op: ArithOp,
) -> Result<&'a DataValue<'a>> {
    if items.is_empty() {
        return Ok(alloc_number(
            arena,
            NumberValue::from_i64(op.identity_int()),
        ));
    }
    let init = op.identity_int();
    let mut state = FoldState::new(init, init as f64);
    for item in items.iter() {
        let int_opt = try_coerce_to_integer_cfg(item, engine);
        let float_opt = if int_opt.is_some() {
            None
        } else {
            coerce_to_number_cfg(item, engine)
        };
        if let FoldStepOutcome::ReturnNull = state.step(
            int_opt,
            float_opt,
            |a, b| op.combine_int(a, b),
            |a, b| op.combine_f(a, b),
            engine,
        )? {
            return Ok(crate::arena::singletons::singleton_null());
        }
    }
    Ok(state.finalize(arena))
}