#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.

30 stable releases

new 2.0.20 Feb 16, 2025
2.0.17 Feb 15, 2025
2.0.12 Jan 26, 2025
2.0.5 Dec 20, 2024
1.0.2 Nov 20, 2024

#229 in Data structures

Download history 326/week @ 2024-11-18 165/week @ 2024-11-25 302/week @ 2024-12-02 769/week @ 2024-12-09 495/week @ 2024-12-16 23/week @ 2024-12-23 223/week @ 2025-01-06 299/week @ 2025-01-13 176/week @ 2025-01-20 96/week @ 2025-01-27 295/week @ 2025-02-03 448/week @ 2025-02-10

1,051 downloads per month

Apache-2.0

89KB
2K SLoC

datalogic-rs

CI Status Documentation crates.io Downloads

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, caching, and rule optimization

📦 Installation

Add datalogic-rs to your Cargo.toml:

[dependencies]
datalogic-rs = "2.0.17"

🚀 Quick Start: Evaluating JSONLogic Rules

use datalogic_rs::{JsonLogic, Rule};
use serde_json::json;

fn main() {
    let rule = Rule::from_value(&json!({
        "if": [
            {">": [{"var": "cart.total"}, 100]},
            "Eligible for discount",
            "No discount"
        ]
    })).unwrap();

    let data = json!({"cart": {"total": 120}});
    let result = JsonLogic::apply(&rule, &data).unwrap();
    
    assert_eq!(result, json!("Eligible for discount"));
}

🛠️ Features

✅ Supported Operations

Category Operators
Comparisons ==, ===, !=, !==, >, >=, <, <=
Logic and, or, if, !, !!
Math +, -, *, /, %, min, max
Arrays map, filter, reduce, all, none, some, merge
Strings substr, cat, in
Data Handling var, missing, missing_some

💡 Advanced Features

  • Static Optimization: Rules are optimized at compile-time for faster execution.
  • Error Handling: The try operator prevents rule evaluation failures.
  • Memory Efficiency: Zero-copy JSON deserialization with SmallVec.
  • Type Coercion: JSONLogic-compliant automatic type conversions.

🎯 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
  • Optimized rule execution
  • Smart caching for reusable expressions

To run benchmarks:

cargo bench

🛠️ 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!

Dependencies

~0.7–1.6MB
~34K SLoC