Function apply

Source
pub fn apply(json_logic: &Value, data: &Value) -> Result<Value, String>
Expand description

Applies the given JsonLogic rule to the specified data. If the rule does not use any variables, you may pass &Value::Null as the second argument.

ยงExample

use serde_json::{json, Value};

let rule = json!({
    "===": [
        2,
        { "var": "foo" }
    ]
});

let data = json!({ "foo": 2 });
assert_eq!(jsonlogic::apply(&rule, &data), Ok(Value::Bool(true)));

let data = json!({ "foo": 3 });
assert_eq!(jsonlogic::apply(&rule, &data), Ok(Value::Bool(false)));
Examples found in repository?
examples/fizzbuzz.rs (line 22)
8fn main() -> Result<(), Box<dyn Error>> {
9    let rule = json!({
10        "if": [
11            {"==": [{ "%": [{ "var": "i" }, 15] }, 0]},
12            "fizzbuzz",
13            {"==": [{ "%": [{ "var": "i" }, 3] }, 0]},
14            "fizz",
15            {"==": [{ "%": [{ "var": "i" }, 5] }, 0]},
16            "buzz",
17            { "var": "i" }
18        ]
19    });
20
21    for i in 1..=30 {
22        println!("{}", jsonlogic::apply(&rule, &json!({ "i": i }))?);
23    }
24
25    Ok(())
26}