datalogic-rs
A high-performance, production-ready Rust implementation of JSONLogic.
Evaluate complex rules and dynamic expressions with a powerful, memory-efficient, and developer-friendly engine.
datalogic-rs is a Rust implementation of JSONLogic designed for performance, safety, and ease of use. Whether you're building a feature flagging system, a dynamic pricing engine, or a complex validation pipeline, datalogic-rs provides the power and flexibility you need.
Our secret sauce? An arena-based memory model that delivers zero-copy parsing and evaluation, making it up to 30% faster than other implementations.
🚀 What Makes datalogic-rs Awesome?
- Blazing Fast: Our arena-based allocator minimizes memory overhead and maximizes speed.
- 100% JSONLogic Compliant: Full compatibility with the official JSONLogic test suite.
- Extensible: Easily add your own custom operators with our simple or advanced APIs.
- Powerful Templating: Preserve object structures for dynamic, structured output.
- Ready for Production: Thread-safe, statically dispatched, and optimized for real-world workloads.
- Batteries Included: Over 50 built-in operators, including datetime and regex support.
- WASM Support: Compile to WebAssembly for use in browser environments.
🏗️ How It Works: The Arena Advantage
datalogic-rs uses an arena allocator for memory-efficient evaluation. This means we can parse a rule once and evaluate it many times with minimal overhead.
use DataLogic;
// Create an evaluator with a custom arena chunk size.
let dl = with_chunk_size;
// Parse your rule and data once.
let rule = dl.parse_logic.unwrap;
let data = dl.parse_data.unwrap;
// Evaluate efficiently.
let result = dl.evaluate.unwrap;
assert!;
// Reset the arena to reuse memory for the next batch of evaluations.
dl.reset_arena;
Structured Object Preservation
Generate powerful, structured outputs by preserving non-operator keys in your JSON.
use DataLogic;
// Enable structure preservation for templating.
let dl = with_preserve_structure;
let result = dl.evaluate_str.unwrap;
// The result is a structured object with evaluated fields.
// { "status": "pass", "grade": 95, "timestamp": "2024-01-15T10:30:00Z" }
🎯 Getting Started: Core API
1. evaluate - For Reusable Rules & Data
Ideal for scenarios where you need to run the same rule against different data.
use DataLogic;
let dl = new;
// Parse the rule and data once.
let rule = dl.parse_logic.unwrap;
let data = dl.parse_data.unwrap;
// Evaluate as many times as you need.
let result = dl.evaluate.unwrap;
assert!;
2. evaluate_str - For Quick, One-Shot Evaluations
Perfect for when your rules are dynamic or you only need a single evaluation.
use DataLogic;
let dl = new;
let result = dl.evaluate_str.unwrap;
assert_eq!;
3. evaluate_json - For Seamless JSON Integration
Works directly with serde_json::Value for easy integration into your existing JSON workflows.
use DataLogic;
use json;
let dl = new;
let logic = json!;
let data = json!;
let result = dl.evaluate_json.unwrap;
assert_eq!;
🔧 Installation
Add datalogic-rs to your Cargo.toml:
[]
= "3.0"
📖 Real-World Examples
Complex Business Rules
Check for multi-condition eligibility with nested logic.
use DataLogic;
let dl = new;
let result = dl.evaluate_str.unwrap;
assert!;
Array Processing & Filtering
Filter and map user data with ease.
use DataLogic;
let dl = new;
let result = dl.evaluate_str.unwrap;
// Returns ["Alice", "Charlie"]
assert_eq!;
DateTime Operations
Perform date arithmetic with timezone support.
use DataLogic;
let dl = new;
let result = dl.evaluate_str.unwrap;
assert!;
Regex Data Extraction
Extract structured data from strings using named capture groups.
use DataLogic;
let dl = new;
let result = dl.evaluate_str.unwrap;
// Returns: { "bank": "SBIN", "country": "IN", "location": "BB", "branch": "101" }
let obj = result.as_object.unwrap;
assert_eq!;
🔌 Custom Operators
Extend datalogic-rs with your own domain-specific logic.
use ;
use NumberValue;
// Define a custom operator that doubles a number.
let mut dl = new;
dl.register_simple_operator;
// Use your custom operator in a rule.
let result = dl.evaluate_str.unwrap;
assert_eq!;
📊 Performance
datalogic-rs is fast. Here's how it stacks up against other implementations on an Apple M2 Pro:
| Implementation | Execution Time | Relative Performance |
|---|---|---|
datalogic-rs |
380ms | 1.0x (baseline) |
json-logic-engine (pre-compiled) |
417ms | 1.1x slower |
json-logic-engine (interpreted) |
986ms | 2.6x slower |
json-logic-js |
5,755ms | 15.1x slower |
Benchmarks run on the standard JSONLogic test suite.
🎯 Use Cases
Feature Flagging
Dynamic Pricing
Fraud Detection
📋 Supported Operations
| Category | Operators |
|---|---|
| Comparison | ==, ===, !=, !==, >, >=, <, <= |
| Logic | and, or, !, !! |
| Arithmetic | +, -, *, /, %, min, max, abs, ceil, floor |
| Control Flow | if, ?:, ?? |
| Arrays | map, filter, reduce, all, some, none, merge, in, length, slice, sort |
| Strings | cat, substr, starts_with, ends_with, upper, lower, trim, replace, split |
| Data Access | var, val, exists, missing, missing_some |
| DateTime | datetime, timestamp, now, parse_date, format_date, date_diff |
| Error Handling | throw, try |
| Custom | User-defined operators |
🤝 Contributing
We welcome contributions! Please see CONTRIBUTING.md for details on how to get started.
🏢 About Plasmatic
datalogic-rs is developed by Plasmatic, an organization dedicated to building open-source tools for financial infrastructure and data processing.
Check out our other projects:
- SwiftMTMessage: A library for parsing SWIFT MT messages with CBPR+ compliance.
- Reframe: A transformation engine for converting SWIFT MT messages to ISO 20022.
- MXMessage: A library for parsing ISO 20022 (MX) messages.
📄 License
datalogic-rs is licensed under the Apache License, Version 2.0. See LICENSE for details.