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.6.0", features = ["email"] }
tokio = { version = "0.3.3", features = ["macros"] }
serde_json = { version = "*" }
anyhow = { version = "*" }
ALL
, ANY
, AtLeast
boolean operators, including recursive nestingSendGrid
```rust use jsonrulesengine::{Engine, Rule, Map, fromdynamic}; use serdejson::json; use serde::{Serialize, Deserialize};
struct Facts { name: String, age: u8, action: String }
fn agegreaterthan20lessthaninclusive25(p: Map) -> bool { let facts: Facts = fromdynamic(&p.into()).unwrap(); facts.age > 20 && facts.age <= 25 }
async main() -> anyhow::Result<()> { let rulejson = json!({ "conditions": { "and": [ { "field": "name", "operator": "stringequals", "value": "Cheng JIANG" }, { "field": "age", "operator": "intinrange", "value": [20, 25] }, { "script": "facts.age > 20 && facts.age <= 25", }, { "script": "myfunction(facts)", }, { "field": "action", "operator": "stringequals", "value": "coding in rust" } ] }, "event": { "type": "posttocallbackurl", "params": { "callbackurl": "http://example.com/people/condinginrust", "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();
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);
} ```