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
Overview
datalogic-rs provides a robust implementation of JSONLogic rules with arena-based memory management for optimal performance. The library provides both a parser for JSON-based rules and a fluent builder API for constructing rules in a type-safe manner.
Features
- Arena-based memory management for optimal performance
- Comprehensive JSONLogic operator support
- Fluent builder API for type-safe rule construction
- Factory methods for common rule patterns
- Optimizations for static rule components
- Zero copy rule creation and evaluation
- High test coverage and compatibility with standard JSONLogic
Installation
Add datalogic-rs to your Cargo.toml:
[]
= "3.0.6"
Usage Examples
1. Simple Comparison Rule
Builder API:
use DataLogic;
use json;
let logic = new;
let builder = logic.builder;
let rule = builder
.compare
.greater_than
.var
.value
.build;
let data = json!;
let result = logic.evaluate.unwrap;
assert!;
Raw JSON Evaluation:
use DataLogic;
use json;
let logic = new;
let result = logic.evaluate_str.unwrap;
assert!;
2. Complex Logical Rule (AND/OR)
Builder API:
let rule = builder
.control
.and
.add
.add
.build;
let data = json!;
let result = logic.evaluate.unwrap;
assert!;
Raw JSON Evaluation:
let result = logic.evaluate_str.unwrap;
assert!;
3. Array Operations
Builder API:
let adult_names = builder
.array
.map
.array
.mapper
.build;
let data = json!;
let result = logic.evaluate.unwrap;
assert_eq!;
Raw JSON Evaluation:
let result = logic.evaluate_str.unwrap;
assert_eq!;
4. Conditional Logic (IF)
Builder API:
let rule = builder
.control
.if_
.condition
.then
.else_
.build;
let data = json!;
let result = logic.evaluate.unwrap;
assert_eq!;
Raw JSON Evaluation:
let result = logic.evaluate_str.unwrap;
assert_eq!;
5. DateTime Operations
Builder API:
use DataLogic;
use json;
use Utc;
let logic = new;
let builder = logic.builder;
// Create a datetime expression
let date_expr = builder
.value
.build;
// Add 2 days to the date
let future_date = builder
.arithmetic
.add
.arg
.arg
.build;
// Compare dates
let is_future = builder
.compare
.greater_than
.arg
.arg
.build;
let result = logic.evaluate.unwrap;
assert!;
Raw JSON Evaluation:
let result = logic.evaluate_str.unwrap;
assert!;
Performance Benefits
The builder API leverages arena allocation for all rule components, providing several performance benefits:
- Zero-copy rule construction
- Reduced memory allocations
- Improved cache locality
- Optimization opportunities during construction
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 |
| DateTime | datetime, timestamp, now, parse_date, format_date, date_diff |
| Special | preserve, throw, try |
| Custom | Support for user-defined operators |
Custom Operators
use ;
use ;
use Cow;
;
// Using Builder API
let mut dl = new;
dl.register_custom_operator;
let builder = dl.builder;
let rule = builder
.custom
.args
.build;
let result = dl.evaluate.unwrap;
assert_eq!;
// Using Raw JSON
let result = dl.evaluate_str.unwrap;
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)
- Authorization rules (Implement complex access control policies)
- Business rule engines (Enforce business policies with configurable rules)
- Date-based processing (Event scheduling, time-based access control, date constraint validation)
Performance
Benchmark results show datalogic-rs is 30% faster than the next fastest JSONLogic implementations, thanks to:
- Arena-based memory management
- Static operator dispatch
- Zero-copy deserialization
- Optimized rule compilation
Contributing
We welcome contributions! See the CONTRIBUTING.md for details.
License
Licensed under Apache License, Version 2.0
Next Steps
✅ Try out datalogic-rs today!
📖 Check out the API documentation for detailed usage instructions
📚 See the docs.rs documentation for comprehensive reference
⭐ Star the GitHub repository if you find it useful!