pub struct CompiledLogic {
pub root: CompiledNode,
}Expand description
Compiled logic that can be evaluated multiple times across different data.
CompiledLogic represents a pre-processed JSONLogic expression that has been
optimized for repeated evaluation. It’s thread-safe and can be shared across
threads using Arc.
§Performance Benefits
- Parse once, evaluate many: Avoid repeated JSON parsing
- Static evaluation: Constant expressions are pre-computed
- OpCode dispatch: Built-in operators use fast enum dispatch
- Thread-safe sharing: Use
Arcto share across threads
§Example
use datalogic_rs::DataLogic;
use serde_json::json;
use std::sync::Arc;
let engine = DataLogic::new();
let logic = json!({">": [{"var": "score"}, 90]});
let compiled = engine.compile(&logic).unwrap(); // Returns Arc<CompiledLogic>
// Can be shared across threads
let compiled_clone = Arc::clone(&compiled);
std::thread::spawn(move || {
let data = json!({"score": 95});
let result = engine.evaluate_owned(&compiled_clone, data);
});Fields§
§root: CompiledNodeThe root node of the compiled logic tree
Implementations§
Source§impl CompiledLogic
impl CompiledLogic
Sourcepub fn compile(logic: &Value) -> Result<Self>
pub fn compile(logic: &Value) -> Result<Self>
Compiles a JSON value into a compiled logic structure.
This method performs basic compilation without static evaluation.
For optimal performance, use compile_with_static_eval instead.
§Arguments
logic- The JSON logic expression to compile
§Returns
A compiled logic structure, or an error if compilation fails.
Sourcepub fn compile_for_trace(
logic: &Value,
preserve_structure: bool,
) -> Result<Self>
pub fn compile_for_trace( logic: &Value, preserve_structure: bool, ) -> Result<Self>
Compiles for tracing without static evaluation.
This method compiles the logic without performing static evaluation, ensuring that all operators remain in the tree for step-by-step debugging. Use this when you need to trace execution through operators that would otherwise be pre-evaluated at compile time.
§Arguments
logic- The JSON logic expression to compilepreserve_structure- Whether to preserve unknown object structure
§Returns
A compiled logic structure without static optimizations.
Sourcepub fn compile_with_static_eval(
logic: &Value,
engine: &DataLogic,
) -> Result<Self>
pub fn compile_with_static_eval( logic: &Value, engine: &DataLogic, ) -> Result<Self>
Compiles with static evaluation using the provided engine.
This method performs optimizations including:
- Static evaluation of constant expressions
- OpCode assignment for built-in operators
- Structure preservation based on engine settings
§Arguments
logic- The JSON logic expression to compileengine- The DataLogic engine for static evaluation
§Returns
An optimized compiled logic structure, or an error if compilation fails.
Source§impl CompiledLogic
impl CompiledLogic
Trait Implementations§
Source§impl Clone for CompiledLogic
impl Clone for CompiledLogic
Source§fn clone(&self) -> CompiledLogic
fn clone(&self) -> CompiledLogic
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more