1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
extern crate serde_json;
mod data;
mod expression;
mod operators;
use serde_json::Value;
use std::collections::HashSet;
use data::Data;
pub fn apply(json_logic: &Value, data: &Value) -> Result<Value, String> {
let ast = expression::Expression::from_json(json_logic)?;
let data = Data::from_json(data);
Ok(ast.compute(&data))
}
#[allow(dead_code)]
fn get_variable_names(json_logic: &Value) -> Result<HashSet<String>, String> {
let ast = expression::Expression::from_json(json_logic)?;
ast.get_variable_names()
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use std::collections::HashSet;
#[test]
fn var_names() {
let json_logic = json!({ "!==": [{ "var": "foo" }, { "var": ["bar", 5] }] });
let names: HashSet<_> = [String::from("foo"), String::from("bar")]
.iter()
.cloned()
.collect();
assert_eq!(get_variable_names(&json_logic).unwrap(), names);
}
}