Add this package to Cargo.toml
of your project. (Check https://crates.io/crates/json-rules-engine for right version)
toml
[dependencies]
json-rules-engine = { version = "0.3.0" }
tokio = { version = "0.3.3", features = ["macros"] }
serde_json = { version = "*" }
ALL
, ANY
, AtLeast
boolean operators, including recursive nesting```rust use jsonrulesengine::{Engine, Rule}; use serde_json::json;
async main() -> anyhow::Result<()> { let rulejson = json!({ "conditions": { "and": [ { "field": "name", "operator": "stringequals", "value": "Cheng JIANG" }, { "field": "age", "operator": "intinrange", "value": [20, 25] }, { "field": "action", "operator": "stringequals", "value": "coding in rust" } ] }, "event": { "type": "posttocallbackurl", "params": { "callbackurl": "http://example.com/people/condingin_rust" "type": "info", "title": "Another person is coding in rust", "message": "Name: {{ name }}, Age: {{ age }}, Action: {{ action }}," } } });
let rule: Rule = serde_json::from_str(serde_json::to_string(&rules_json).unwrap()).unwrap();
let mut engine = Engine::new();
engine.add_rule(rule);
let facts = json!({
"name": "Cheng JIANG",
"age": 24,
"action": "coding in rust",
});
let rule_results = engine.run(&facts).await?;
println!("{:?}", rule_results);
} ```