Expand description
§datalogic-rs
A high-performance, thread-safe Rust implementation of JSONLogic.
§Overview
datalogic-rs provides a powerful rule evaluation engine that compiles JSONLogic
expressions into optimized, reusable structures that can be evaluated across
multiple threads with zero overhead.
§Key Features
- Compilation-based optimization: Parse once, evaluate many times
- Thread-safe by design: Share compiled logic across threads with
Arc - 50+ built-in operators: Complete JSONLogic compatibility plus extensions
- Arena-allocated evaluation: Results live in a
bumpalo::Bumparena and can borrow directly into caller input for zero-copy paths - Extensible: Add custom operators via the
CustomOperatortrait - Structured templates: Preserve object structure for dynamic outputs
§Quick Start (one-shot)
use datalogic_rs::Engine;
let engine = Engine::new();
let result = engine.eval_str(
r#"{"==": [{"var": "status"}, "active"]}"#,
r#"{"status": "active"}"#,
).unwrap();
assert_eq!(result, "true");§Reusing the arena across many evaluations
For high-throughput callers, open a Session handle. It owns a
bumpalo::Bump, resets it between calls, and returns owned results so
you don’t have to juggle arena lifetimes:
use datalogic_rs::Engine;
let engine = Engine::new();
let compiled = engine.compile(r#"{"+": [{"var": "x"}, 1]}"#).unwrap();
let mut session = engine.session();
for x in 0..3 {
let payload = format!(r#"{{"x": {}}}"#, x);
let result = session.eval_str(&compiled, &payload).unwrap();
assert_eq!(result, (x + 1).to_string());
// The session does not auto-reset; bound peak memory by
// resetting between iterations (constant-time, reuses chunks).
session.reset();
}§Power-user (compile once, evaluate many, zero-copy results)
When the result borrow can stay scoped to a caller-managed
bumpalo::Bump, skip the deep-clone and use Engine::evaluate
directly. evaluate accepts any input shape via EvalInput:
&str, &OwnedDataValue, &serde_json::Value, an owned DataValue<'a>,
or an existing &'a DataValue<'a>.
use bumpalo::Bump;
use datalogic_rs::Engine;
let engine = Engine::new();
let compiled = engine.compile(r#"{"==": [{"var": "status"}, "active"]}"#).unwrap();
let arena = Bump::new();
let result = engine.evaluate(&compiled, r#"{"status": "active"}"#, &arena).unwrap();
assert_eq!(result.as_bool(), Some(true));§Architecture
The library uses a two-phase approach:
- Compilation: JSON logic is parsed into
Logicwith OpCode dispatch - Evaluation: Compiled logic is evaluated through arena dispatch — results
are
&'a DataValue<'a>allocated in abumpalo::Bumpfor the duration of one evaluate call.
This design enables sharing compiled logic across threads, eliminates
repeated parsing overhead, and lets read-through operations like var
return zero-copy borrows into the caller’s input data.
Re-exports§
Modules§
- operator
- Public scaffolding for user-supplied operators.
Structs§
- Engine
- JSONLogic compile/evaluate engine.
- Engine
Builder - Builder for
Engine. Construct viaEngine::builder. - Error
- Error returned by every
crate::Engineoperation. - Evaluation
Config - Knobs that change how an
Enginetreats edge cases during evaluation — non-numeric arguments to arithmetic, division by zero, loose equality across types, truthiness rules, numeric coercion, and a recursion-depth cap. - Execution
Step trace - Captures state at each evaluation step.
- Expression
Node trace - Represents a node in the expression tree for flow diagram rendering.
- Logic
- Compiled logic that can be evaluated multiple times across different data.
- Numeric
Coercion Config - Knobs for the implicit value→number coercions arithmetic and comparison perform on non-numeric arguments.
- Path
Step - One node along the path from the root of a compiled rule down to the
failing sub-expression. Returned root-to-leaf by
crate::Logic::resolve_node_ids/crate::Error::resolve_path. - Session
- Reusable evaluation handle. Construct via
Engine::session. - Traced
Run trace - Result of a traced evaluation produced by
TracedSession. Always includes the trace data; the value-or-error split lives onSelf::result. - Traced
Session trace - Trace-enabled view over a
crate::Engineengine. Constructed viacrate::Engine::trace. Mirrorscrate::Session1:1 — everyeval*returns aTracedRun<R>carrying the trace alongside the result, whereRis the same shape thatSession::eval*would return. Owns its ownbumpalo::Bumpacross calls; reset is per-call (the trace path always allocates a fresh arena to keep the borrowed-result lifetime tied to the run).
Enums§
- Data
Value - Arena-allocated JSON value tree. Mirrors
serde_json::Valuein shape and access surface, but every composite payload lives in aBump. - Division
ByZero Handling - Defines how to handle division by zero
- Error
Kind - Discriminant for
crate::Error. Stable variant tags are exposed viacrate::Error::tagfor matching across releases. - NanHandling
- Defines how to handle NaN (Not a Number) scenarios in arithmetic operations
- Truthy
Evaluator - Defines how to evaluate truthiness of values
Traits§
- Arena
Ext - Allocate borrowed
DataValues in aBumpwithout typingarena.alloc(DataValue::...)at every return. - Custom
Operator - Custom operator hook for the
Engine. - Eval
Input - Adapter trait that converts a value into a
&'a DataValue<'a>borrowed from the caller-supplied arena. Sealed — the supported input shapes are listed in this file; external crates cannot add new ones. - From
Data Value - Convert a borrowed arena
DataValueinto a concrete result type. - Into
Logic - Convert
selfinto anOwnedDataValuesuitable for compilation. - Owned
Input - Adapter trait for
crate::Engine::eval/crate::Engine::eval_str/crate::Engine::eval_intoand the module-leveldatalogic::eval*helpers, where the engine creates and owns the arena per call.
Functions§
- compile
- Compile a rule using the default engine. Equivalent to
Engine::default().compile(rule). - eval
- One-shot evaluation returning
datavalue::OwnedDataValue. - eval_
into serde_json - One-shot evaluation deserialised into a typed
T: DeserializeOwned. - eval_
str - One-shot evaluation returning a JSON
String.
Type Aliases§
- Custom
Error Source - Trait-object alias for the source carried by
ErrorKind::Custom. Reference-counted soErrorKindstays cheap to clone, and bounded so a singlecrate::Errorvalue can be sent across threads. - Result
- Result type for Engine operations