2 stable releases
new 1.0.2 | Nov 20, 2024 |
---|---|
1.0.1 | Nov 19, 2024 |
#319 in Data structures
115 downloads per month
43KB
872 lines
datalogic-rs
A Rust implementation of JSONLogic that provides a way to write portable logic rules as JSON.
Overview
datalogic-rs
implements the complete JSONLogic specification, allowing you to create, share, and evaluate rules across different platforms while staying true to Rust's safety and performance principles.
Installation
Add to your Cargo.toml
:
[dependencies]
datalogic-rs = "0.1.0"
Quick Example
use datalogic_rs::JsonLogic;
use serde_json::json;
fn main() {
let logic = JsonLogic::new();
// Complex discount rule:
// - 20% off if cart total > $100 AND user is premium member
// - OR 10% off if cart has more than 5 items
let discount_rule = json!({
"or": [
{"and": [
{">": [{"var": "cart.total"}, 100]},
{"==": [{"var": "user.membership"}, "premium"]}
]},
{">": [{"var": "cart.item_count"}, 5]}
]
});
let customer_data = json!({
"cart": {
"total": 120.00,
"item_count": 3
},
"user": {
"membership": "premium"
}
});
let applies_for_discount = logic.apply(&discount_rule, &customer_data).unwrap();
assert_eq!(applies_for_discount, json!(true));
}
Supported Operations
This implementation supports all standard JSONLogic operations including:
- Basic operators (
==
,===
,!=
,!==
,>
,>=
,<
,<=
) - Logic operators (
!
,!!
,or
,and
,if
) - Numeric operations (
+
,-
,*
,/
,%
) - Array operations (
merge
) - String operations (
cat
,substr
) - Data access (
var
)
For detailed documentation of operations and examples, visit jsonlogic.com.
Features
- ✅ Complete implementation of JSONLogic specification
- 🚀 Zero-copy JSON deserialization
- 🛡️ Type-safe rule evaluation
- 🧪 Comprehensive test suite matching official JSONLogic tests
Testing
The library includes a comprehensive test suite that verifies compatibility with the official JSONLogic test cases:
cargo test
License
Licensed under Apache-2.0
References
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Dependencies
~0.7–1.6MB
~34K SLoC