#rules #json #engine #logic

datalogic-rs

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.

2 stable releases

1.0.2 Nov 20, 2024
1.0.1 Nov 19, 2024

#302 in Data structures

Download history 326/week @ 2024-11-18

326 downloads per month

Apache-2.0

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
~35K SLoC