datalogic-rs is a high-performance Rust implementation of JSONLogic for evaluating logical rules expressed as JSON. It provides a fast, memory-efficient, and thread-safe way to evaluate complex business rules, feature flags, dynamic pricing logic, and more.
Why datalogic-rs?
- Fast: Uses OpCode-based dispatch with compile-time optimization for maximum performance
- Thread-Safe: Compile once, evaluate anywhere with zero-copy
Arcsharing - Intuitive: Works seamlessly with
serde_json::Value - Extensible: Add custom operators with a simple trait
- Feature-Rich: 59 built-in operators including datetime, regex, and error handling
- Fully Compliant: Passes the official JSONLogic test suite
How It Works
datalogic-rs uses a two-phase approach:
-
Compilation: Your JSON logic is parsed and compiled into an optimized
CompiledLogicstructure. This phase:- Assigns OpCodes to built-in operators for fast dispatch
- Pre-evaluates constant expressions
- Analyzes structure for templating mode
-
Evaluation: The compiled logic is evaluated against your data with:
- Direct OpCode dispatch (no string lookups at runtime)
- Context stack for nested operations (map, filter, reduce)
- Efficient value passing with minimal allocations
Quick Example
#![allow(unused)]
fn main() {
use datalogic_rs::DataLogic;
use serde_json::json;
let engine = DataLogic::new();
// Define a rule: is the user's age greater than 18?
let rule = json!({ ">": [{ "var": "age" }, 18] });
// Compile once
let compiled = engine.compile(&rule).unwrap();
// Evaluate against different data
let result = engine.evaluate_owned(&compiled, json!({ "age": 21 })).unwrap();
assert_eq!(result, json!(true));
let result = engine.evaluate_owned(&compiled, json!({ "age": 16 })).unwrap();
assert_eq!(result, json!(false));
}
What is JSONLogic?
JSONLogic is a standard for expressing logic rules as JSON. This makes it:
- Portable: Rules can be stored in databases, sent over APIs, or embedded in configuration
- Language-agnostic: The same rules work across different implementations
- Human-readable: Rules are easier to understand than arbitrary code
- Safe: Rules can be evaluated without arbitrary code execution
A JSONLogic rule is a JSON object where:
- The key is the operator name
- The value is an array of arguments
{"operator": [arg1, arg2, ...]}
For example:
{"and": [
{">": [{"var": "age"}, 18]},
{"==": [{"var": "country"}, "US"]}
]}
This rule checks if age > 18 AND country == "US".
Next Steps
- Installation - Add datalogic-rs to your project
- Quick Start - Get up and running in minutes
- Operators - Explore all 59 built-in operators