datalogic-rs
A lightweight, high-performance Rust implementation of JSONLogic, optimized for rule-based decision-making and dynamic expressions.
✨ Why datalogic-rs?
- 🏆 Fully JSONLogic-compliant (100% test coverage)
- 🚀 Fast & lightweight: Zero-copy JSON parsing, minimal allocations
- 🔒 Thread-safe: Designed for parallel execution
- ⚡ Optimized for production: Static dispatch and rule optimization
- 🔌 Extensible: Support for custom operators
📦 Installation
Add datalogic-rs to your Cargo.toml:
[]
= "2.0.17"
🚀 Quick Start: Evaluating JSONLogic Rules
use ;
use json;
🛠️ Features
✅ Supported Operations
| Category | Operators |
|---|---|
| Comparison | ==, ===, !=, !==, >, >=, <, <= |
| Logic | and, or, !, !! |
| Arithmetic | +, -, *, /, %, min, max |
| Control Flow | if, ?:, ?? |
| Arrays | map, filter, reduce, merge, all, none, some |
| Strings | substr, cat, in |
| Data Access | var, val, exists, missing, missing_some |
| Special | preserve, throw, try |
| Custom | Support for user-defined operators |
💡 Advanced Features
- Static Optimization: Rules are optimized at compile-time
- Error Handling: Built-in error handling with
tryoperator - Memory Efficiency: Zero-copy JSON deserialization
- Type Coercion: JSONLogic-compliant type conversions
- Thread Safety: All operations are thread-safe
- Custom Operators: Extend with your own operators
🔌 Custom Operators
use ;
use ;
use Cow;
// Define a custom power operator
;
// Register the operator
global.add_operator?;
// Use in rules
let rule = from_value?;
let result = apply?;
assert_eq!;
🎯 Use Cases
datalogic-rs is ideal for rule-based decision engines in:
- Feature flagging (Enable features dynamically based on user attributes)
- Dynamic pricing (Apply discounts or surge pricing based on conditions)
- Fraud detection (Evaluate transaction risk using JSON-based rules)
- Form validation (Check field dependencies dynamically)
📊 Performance
Benchmark results show datalogic-rs is 2x faster than other JSONLogic implementations, thanks to:
- Static operator dispatch
- Zero-copy deserialization
- Optimized rule compilation
To run benchmarks:
🛠️ Contributing
We welcome contributions! See the CONTRIBUTING.md for details.
📜 License: Apache-2.0
🚀 Next Steps
✅ Try out datalogic-rs today!
📖 Check out the docs.rs documentation
⭐ Star the GitHub repository if you find it useful!