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
433
434
435
436
437
438
439
440
441
442
443
#![cfg(feature = "serde_json")]

use datalogic_rs::{Engine, Error};
use serde_json::{Value, json};

fn to_json(err: &Error) -> Value {
    serde_json::to_value(err).expect("Error must serialize")
}

#[test]
fn serialize_invalid_operator() {
    let err = Error::invalid_operator("foo");
    assert_eq!(
        to_json(&err),
        json!({"type": "InvalidOperator", "message": "Invalid operator: foo"})
    );
}

#[test]
fn serialize_invalid_arguments() {
    let err = Error::invalid_arguments("need 2");
    assert_eq!(
        to_json(&err),
        json!({"type": "InvalidArguments", "message": "Invalid arguments: need 2"})
    );
}

#[test]
fn serialize_variable_not_found() {
    let err = Error::variable_not_found("user.name");
    assert_eq!(
        to_json(&err),
        json!({
            "type": "VariableNotFound",
            "message": "Variable not found: user.name",
            "variable": "user.name",
        })
    );
}

#[test]
fn serialize_invalid_context_level() {
    let err = Error::invalid_context_level(-3);
    assert_eq!(
        to_json(&err),
        json!({
            "type": "InvalidContextLevel",
            "message": "Invalid context level: -3",
            "level": -3,
        })
    );
}

#[test]
fn serialize_type_error() {
    let err = Error::type_error("cannot compare");
    assert_eq!(
        to_json(&err),
        json!({"type": "TypeError", "message": "Type error: cannot compare"})
    );
}

#[test]
fn serialize_arithmetic_error() {
    let err = Error::arithmetic_error("divide by zero");
    assert_eq!(
        to_json(&err),
        json!({"type": "ArithmeticError", "message": "Arithmetic error: divide by zero"})
    );
}

#[test]
fn serialize_custom() {
    let err = Error::custom_message("user-defined");
    assert_eq!(
        to_json(&err),
        json!({"type": "Custom", "message": "user-defined"})
    );
}

#[test]
fn serialize_parse_error() {
    let err = Error::parse_error("expected value");
    assert_eq!(
        to_json(&err),
        json!({"type": "ParseError", "message": "Parse error: expected value"})
    );
}

#[test]
fn serialize_thrown() {
    use datavalue::{NumberValue, OwnedDataValue};
    let owned = OwnedDataValue::Object(vec![
        (
            "code".to_string(),
            OwnedDataValue::Number(NumberValue::Integer(42)),
        ),
        (
            "reason".to_string(),
            OwnedDataValue::String("boom".to_string()),
        ),
    ]);
    let err = Error::thrown(owned);
    let v = to_json(&err);
    assert_eq!(v["type"], json!("Thrown"));
    assert_eq!(v["thrown"], json!({"code": 42, "reason": "boom"}));
    // message is the Display output, which prints the JSON payload
    assert!(v["message"].as_str().unwrap().starts_with("Thrown: "));
}

#[test]
fn serialize_format_error() {
    let err = Error::format_error("bad pattern");
    assert_eq!(
        to_json(&err),
        json!({"type": "FormatError", "message": "Format error: bad pattern"})
    );
}

#[test]
fn serialize_index_out_of_bounds() {
    let err = Error::index_out_of_bounds(5, 3);
    assert_eq!(
        to_json(&err),
        json!({
            "type": "IndexOutOfBounds",
            "message": "Index 5 out of bounds for array of length 3",
            "index": 5,
            "length": 3,
        })
    );
}

#[test]
fn serialize_configuration_error() {
    let err = Error::configuration_error("invalid setting");
    assert_eq!(
        to_json(&err),
        json!({"type": "ConfigurationError", "message": "Configuration error: invalid setting"})
    );
}

#[test]
fn structured_error_adds_operator_field() {
    let se = Error::arithmetic_error("divide by zero").with_operator("/");
    let v = serde_json::to_value(&se).unwrap();
    assert_eq!(
        v,
        json!({
            "type": "ArithmeticError",
            "message": "Arithmetic error: divide by zero",
            "operator": "/",
        })
    );
}

#[test]
fn structured_error_omits_operator_when_none() {
    let se = Error::type_error("x");
    let v = serde_json::to_value(&se).unwrap();
    assert_eq!(v, json!({"type": "TypeError", "message": "Type error: x"}));
}

#[test]
fn structured_error_flattens_variant_extras() {
    let se = Error::index_out_of_bounds(10, 2).with_operator("substr");
    let v = serde_json::to_value(&se).unwrap();
    assert_eq!(
        v,
        json!({
            "type": "IndexOutOfBounds",
            "message": "Index 10 out of bounds for array of length 2",
            "index": 10,
            "length": 2,
            "operator": "substr",
        })
    );
}

#[cfg(feature = "error-handling")]
#[test]
fn evaluate_json_structured_reports_outer_operator() {
    let engine = Engine::new();
    let err = engine
        .eval_str(r#"{"throw": {"type": "Boom"}}"#, r#"{}"#)
        .expect_err("throw must produce a structured error");
    let v = serde_json::to_value(&err).unwrap();
    assert_eq!(v["type"], json!("Thrown"));
    assert!(v["thrown"].is_object(), "thrown payload must be preserved");
    assert_eq!(v["operator"], json!("throw"));
}

#[test]
fn evaluate_json_structured_success_passes_through() {
    let engine = Engine::new();
    let result: serde_json::Value = engine
        .eval_into::<serde_json::Value, _, _>(r#"{"+": [1, 2]}"#, r#"{}"#)
        .unwrap();
    // Accept either integer or float encoding — datalogic returns either based on inputs.
    assert_eq!(result.as_f64(), Some(3.0));
}

#[test]
fn evaluate_json_structured_parse_error_has_no_operator() {
    let engine = Engine::new();
    let err = engine
        .eval_str("not json", r#"{}"#)
        .expect_err("invalid JSON should error");
    let v = serde_json::to_value(&err).unwrap();
    assert_eq!(v["type"], json!("ParseError"));
    assert!(v.get("operator").is_none());
}

#[test]
fn evaluate_json_structured_captures_arithmetic_operator() {
    let engine = Engine::new();
    let err = engine
        .eval_str(r#"{"/": [1, 0]}"#, "{}")
        .expect_err("division by zero must error");
    let v = serde_json::to_value(&err).unwrap();
    assert_eq!(v["operator"], json!("/"));
    assert_eq!(v["type"], json!("Thrown"));
}

#[test]
fn evaluate_json_structured_captures_unknown_operator() {
    let engine = Engine::new();
    let err = engine
        .eval_str(r#"{"not_a_real_op_123": [1]}"#, "{}")
        .expect_err("unknown op must error");
    let v = serde_json::to_value(&err).unwrap();
    assert_eq!(v["type"], json!("InvalidOperator"));
    // The outermost node IS an InvalidOperator, so operator field mirrors the name.
    assert_eq!(v["operator"], json!("not_a_real_op_123"));
}

#[test]
fn evaluate_json_structured_captures_type_coercion_op() {
    let engine = Engine::new();
    let err = engine
        .eval_str(r#"{"+": ["abc", 1]}"#, "{}")
        .expect_err("non-numeric addition must error");
    let v = serde_json::to_value(&err).unwrap();
    assert_eq!(v["operator"], json!("+"));
}

#[cfg(feature = "trace")]
#[test]
fn evaluate_json_with_trace_structured_populates_error_fields() {
    let engine = Engine::new();
    let traced = engine
        .trace()
        .eval_str(r#"{"throw": {"type": "Boom"}}"#, r#"{}"#);
    let structured = traced.result.expect_err("structured must populate");
    let v = serde_json::to_value(&structured).unwrap();
    assert_eq!(v["type"], json!("Thrown"));
    assert_eq!(v["operator"], json!("throw"));
}

/// Regression gate: Display output must stay byte-identical to v4.0.21, since
/// the JSONLogic test suite and downstream TS consumers depend on it.
#[test]
fn display_output_snapshot() {
    let cases: &[(Error, &str)] = &[
        (Error::invalid_operator("foo"), "Invalid operator: foo"),
        (Error::invalid_arguments("x"), "Invalid arguments: x"),
        (
            Error::variable_not_found("user.name"),
            "Variable not found: user.name",
        ),
        (
            Error::invalid_context_level(-2),
            "Invalid context level: -2",
        ),
        (Error::type_error("x"), "Type error: x"),
        (Error::arithmetic_error("x"), "Arithmetic error: x"),
        (Error::custom_message("raw"), "raw"),
        (Error::parse_error("x"), "Parse error: x"),
        (
            Error::thrown(datavalue::OwnedDataValue::Object(vec![(
                "k".to_string(),
                datavalue::OwnedDataValue::Number(datavalue::NumberValue::Integer(1)),
            )])),
            "Thrown: {\"k\":1}",
        ),
        (Error::format_error("x"), "Format error: x"),
        (
            Error::index_out_of_bounds(1, 0),
            "Index 1 out of bounds for array of length 0",
        ),
        (Error::configuration_error("x"), "Configuration error: x"),
    ];
    for (err, expected) in cases {
        assert_eq!(err.to_string(), *expected, "Display changed for {:?}", err);
    }
}

// =====================================================================
// Error breadcrumb path tests (plan item #4)
// =====================================================================

#[test]
fn structured_error_has_nonempty_path_on_runtime_error() {
    let engine = Engine::new();
    // Nested variable access that will fail: outer wraps an unknown-var read.
    // The path should contain at least one node id (the failing node), and
    // more if the error unwinds through additional operators.
    // `throw` wrapped in an `if` with a DYNAMIC condition (reads a var)
    // so the optimiser can't fold the if away and the error unwinds
    // through multiple operator frames.
    let err = engine
        .eval_str(
            r#"{"if": [{"var": "go"}, {"throw": "oops"}, "ok"]}"#,
            r#"{"go": true}"#,
        )
        .expect_err("throw should fail");

    // Breadcrumb should be populated, leaf-first (deepest failure first).
    assert!(
        !err.node_ids().is_empty(),
        "expected breadcrumb path, got empty"
    );
    // All ids should be nonzero (SYNTHETIC_ID=0 is reserved).
    for id in err.node_ids() {
        assert!(
            *id > 0,
            "synthetic id leaked into breadcrumb: {:?}",
            err.node_ids()
        );
    }
    // Should have at least 2 ids — the throw itself plus the wrapping if,
    // since the if's dynamic condition prevents dead-code elimination.
    assert!(
        err.node_ids().len() >= 2,
        "expected at least 2 ids in path, got {:?}",
        err.node_ids()
    );
}

#[test]
fn structured_error_empty_path_on_success() {
    let engine = Engine::new();
    let result = engine.eval_str(r#"{"==": [1, 1]}"#, r#"{}"#);
    // Successful eval — no error, so there's nothing to assert about path.
    assert!(result.is_ok());
}

#[cfg(feature = "error-handling")]
#[test]
fn try_catches_and_discards_inner_path() {
    let engine = Engine::new();
    // Outer `try` swallows a failing inner branch; the caller should see Ok,
    // and no breadcrumb should leak into a subsequent failing evaluation
    // because try truncates on catch.
    // try swallows the throw; result should be the fallback, not an error.
    let result: serde_json::Value = engine
        .eval_into::<serde_json::Value, _, _>(
            r#"{"try": [{"throw": "ignored"}, "fallback"]}"#,
            r#"{}"#,
        )
        .unwrap();
    assert_eq!(result, json!("fallback"));
}

#[test]
fn structured_error_path_serializes_to_json() {
    let engine = Engine::new();
    let err = engine
        .eval_str(r#"{"if": [true, {"throw": "boom"}, "ok"]}"#, r#"{}"#)
        .unwrap_err();

    let json: Value = serde_json::to_value(&err).expect("must serialize");
    // `node_ids` should be present as an array of numbers.
    let node_ids = json
        .get("node_ids")
        .expect("serialized error should include `node_ids` field")
        .as_array()
        .expect("`node_ids` should be an array");
    assert!(!node_ids.is_empty());
    for id in node_ids {
        assert!(id.is_u64());
    }
    // No PathStep cache field is serialized — wire format is the
    // baseline `{type, message, ?operator, ?path}` shape.
    assert!(json.get("path_steps").is_none());
    assert!(json.get("resolved").is_none());
}

/// Compile a rule that throws at runtime (NaN from string arithmetic) and
/// return the resulting Error along with its compiled Logic.
fn nan_error(engine: &Engine) -> (datalogic_rs::Logic, Error) {
    let compiled = engine.compile(r#"{"+": ["x", 1]}"#).unwrap();
    let mut session = engine.session();
    let err = session
        .eval_into::<serde_json::Value, _>(&compiled, &json!(null))
        .expect_err("arithmetic on string must fail");
    (compiled, err)
}

#[test]
fn engine_errors_carry_raw_path_for_on_demand_resolution() {
    // Engine evaluation attaches raw compiled-node ids only — resolving
    // those into PathSteps is paid at the catch site via
    // `error.resolve_path(&compiled)`. Doing the walk on every
    // boundary crossing inflates error-heavy workloads ~17×; the
    // resolve-on-demand contract puts the cost where the caller
    // actually needs the data.
    let engine = Engine::new();
    let (compiled, err) = nan_error(&engine);

    assert!(
        !err.node_ids().is_empty(),
        "engine errors must arrive with raw breadcrumb ids, got {:?}",
        err
    );
    // Resolve on demand against the original Logic.
    let steps = err.resolve_path(&compiled);
    assert!(!steps.is_empty(), "resolve_path must produce steps");
    assert_eq!(steps[0].operator.as_deref(), Some("+"));
}

#[test]
fn with_path_replaces_prior_path() {
    // `with_path` is a plain setter — replacing the inline `Vec<u32>`
    // wholesale.
    let err = Error::invalid_arguments("nope")
        .with_node_ids(vec![1, 2, 3])
        .with_node_ids(vec![999]);
    assert_eq!(err.node_ids(), &[999]);
}

#[test]
fn wrap_preserves_node_ids_metadata() {
    // `Error::wrap(some_error)` is a no-op when given an existing Error;
    // the raw node_ids breadcrumb round-trips alongside operator metadata.
    let engine = Engine::new();
    let (_compiled, err) = nan_error(&engine);
    let original_node_ids = err.node_ids().to_vec();
    assert!(!original_node_ids.is_empty());

    let wrapped = Error::wrap(err);
    assert_eq!(wrapped.node_ids(), original_node_ids.as_slice());
}