juspay_jsonlogic 0.5.5

A JsonLogic implementation in Rust
Documentation
use serde_json::Value;

use super::{Data, Expression, PartialResult};

pub fn compute(args: &[Expression], data: &Data) -> Value {
    let mut result: Vec<Value> = vec![];

    for arg in args {
        let arg = arg.compute(data);
        match arg {
            Value::Array(arr) => result.extend(arr.iter().cloned()),
            _ => result.push(arg),
        };
    }

    Value::Array(result)
}

// early returns on finding any Ambiguous arg
pub fn partial_compute(args: &[Expression], data: &Data) -> PartialResult {
    let mut result: Vec<Value> = vec![];

    for arg in args {
        let arg = arg.partial_compute(data)?;
        match arg {
            Value::Array(arr) => result.extend(arr.iter().cloned()),
            _ => result.push(arg),
        };
    }

    Ok(Value::Array(result))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compute_const;
    use serde_json::json;

    #[test]
    fn merge() {
        assert_eq!(compute_const!(), json!([]));
        assert_eq!(
            compute_const!(json!([1, 2]), json!([3, 4])),
            json!([1, 2, 3, 4])
        );
        assert_eq!(
            compute_const!(json!(1), json!(2), json!([3, 4])),
            json!([1, 2, 3, 4])
        );
    }
}