Crates.io | datalogic-rs |
lib.rs | datalogic-rs |
version | |
source | src |
created_at | 2024-11-19 02:20:35.614503 |
updated_at | 2024-12-20 02:03:53.661923 |
description | A fast, type-safe Rust implementation of JSONLogic for evaluating logical rules as JSON. Perfect for business rules engines and dynamic filtering in Rust applications. |
homepage | https://github.com/Open-Payments/datalogic-rs |
repository | https://github.com/Open-Payments/datalogic-rs |
max_upload_size | |
id | 1452835 |
Cargo.toml error: | TOML parse error at line 26, column 1 | 26 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A high-performance Rust implementation of JSONLogic that provides a way to write portable logic rules as JSON. Fully compliant with the JSONLogic specification and optimized for production use.
datalogic-rs
offers a complete, thread-safe implementation of the JSONLogic specification with:
Add to your Cargo.toml
:
[dependencies]
datalogic-rs = "2.0.0"
use datalogic_rs::JsonLogic;
use serde_json::json;
fn main() {
// Complex discount rule example
let discount_rule = json!({
"if": [
{"and": [
{">": [{"var": "cart.total"}, 100]},
{"==": [{"var": "user.membership"}, "premium"]}
]},
{"*": [{"var": "cart.total"}, 0.75]}, // 25% discount
{"*": [{"var": "cart.total"}, 1.0]} // no discount
]
});
let data = json!({
"cart": {
"total": 120.00
},
"user": {
"membership": "premium"
}
});
let rule = Rule::from_value(&discount_rule).unwrap();
let price = JsonLogic::apply(&rule, &data).unwrap();
assert_eq!(price, json!(90.0)); // 25% off 120
}
All JSONLogic operations are supported:
==
, ===
, !=
, !==
, >
, >=
, <
, <=
!
, !!
, or
, and
, if
, ?:
+
, -
, *
, /
, %
, min
, max
map
, filter
, reduce
, all
, none
, some
, merge
substr
, cat
, in
var
, missing
, missing_some
preserve
for data preservationThe library is optimized for production use with:
100% compatibility with official JSONLogic tests:
cargo test # Run unit tests
cargo bench # Run performance benchmarks
Licensed under Apache-2.0
Contributions are welcome! The codebase has extensive documentation and test coverage.