#json #rule #rules #engine

json-rules-engine

json rules engine

17 releases (8 breaking)

0.9.3 Feb 11, 2021
0.9.1 Dec 17, 2020
0.7.3 Nov 16, 2020

#3 in #rules

Download history 22/week @ 2021-04-07 36/week @ 2021-04-14 19/week @ 2021-04-21 19/week @ 2021-04-28 2/week @ 2021-05-05 17/week @ 2021-05-12 3/week @ 2021-06-02 17/week @ 2021-06-09 1/week @ 2021-06-30 18/week @ 2021-07-14 2/week @ 2021-07-21

87 downloads per month

MIT/Apache

57KB
1.5K SLoC

json-rules-engine

Installation

Add this package to Cargo.toml of your project. (Check https://crates.io/crates/json-rules-engine for right version)

[dependencies]
json-rules-engine = { version = "0.9", features = ["email", "eval"] }
tokio = { version = "0.3.3", features = ["macros"] }
serde_json = { version = "*" }
anyhow = { version = "*" }

Features

  • Built in operators
  • Full support for ALL, OR, Not, AtLeast boolean operators, including recursive nesting
  • Type Safe
  • Load rules from json
  • HTTP post to callback url
  • Built in Moustache render
  • Email notifications based on SendGrid
  • Safe script
  • Custom function
  • Coalescence Group

Get started

use json_rules_engine::{Engine, Rule, Map, from_dynamic};
use serde_json::json;
use serde::{Serialize, Deserialize};

#[derive(Deserialize, Serialize)]
struct Facts {
    name: String,
    age: u8,
    action: String
}

fn age_greater_than20_less_than_inclusive25(p: Map) -> bool {
    let facts: Facts = from_dynamic(&p.into()).unwrap();
    facts.age > 20 && facts.age <= 25
}

#[tokio::main]
async main() -> anyhow::Result<()> {
    let sendgrid_api_key = "kjsldkjslkjlwkjkjew";

    let rule_json = json!({
        "conditions": {
            "and": [
                {
                    "field": "name",
                    "operator": "string_equals",
                    "value": "Cheng JIANG"
                },
                {
                    "field": "age",
                    "operator": "int_in_range",
                    "value": [20, 25]
                },
                {
                    "and": [
                        {
                            "expr": "facts.age > 20 && facts.age <= 25",
                        },
                        {
                            "expr": "my_function(facts)",
                        },
                    ]
                },
                {
                    "field": "action",
                    "operator": "string_equals",
                    "value": "coding in rust"
                }
            ]
        },
        "events": [
            {
                "type": "post_to_callback_url",
                "params": {
                    "callback_url": "http://example.com/peoples/conding_in_rust",
                    "type": "info",
                    "title": "Another person is coding in rust",
                    "message": "Name: {{ name }}, Age: {{ age }}, Action: {{ action }}"
                }
            },
            {
                "type": "email_notification",
                "params": {
                    "from": "alex_cj96@foxmail.com",
                    "to": ["abc.def@gmail.com"],
                    "type": "info",
                    "title": "Another person is coding in rust",
                    "message": "Name: {{ name }}, Age: {{ age }}, Action: {{ action }},"
                }
            }
        ]
    });

    let rule: Rule = serde_json::from_str::<Rule>(&serde_json::to_string(&rule_json).unwrap()).unwrap();

    let mut engine = Engine::new(sendgrid_api_key);
    engine.add_rule(rule);
    engine.add_function("my_function", age_greater_than20_less_than_inclusive25);

    let facts = json!({
        "name": "Cheng JIANG",
        "age": 24,
        "action": "coding in rust",
    });

    let rule_results = engine.run(&facts).await?;

    println!("{:?}", rule_results);
}

Special Thanks

Dependencies

~4.5–8.5MB
~197K SLoC