Expand description
Simple rules engine that represents requirements as a tree, with each node having one or more requirements in order to be “Met”.
A tree of rules is constructed, and then the .check_json()
method is called.
json
is a nested field: value
that will be given to each node in the tree for testing.
Status output can be either Met
, NotMet
, or Unknown
if the tested field is not present in the json.
To construct a tree, see the following methods.
§Example
extern crate json_rules_engine;
use serde_json::json;
let tree = json_rules_engine::and(vec![
json_rules_engine::string_equals("name", "John Doe"),
json_rules_engine::or(vec![
json_rules_engine::int_equals("fav_number", 5),
json_rules_engine::int_in_range("thinking_of", 5, 10)
])
]);
let mut facts = json!({
"name": "John Doe",
"fav_number": 5
});
#[cfg(not(feature = "eval"))]
{
let result = tree.check_value(&facts);
println!("{:?}", result);
assert!(result.status == json_rules_engine::Status::Met);
}
// result = ConditionResult { name: "And", status: Met, children: [ConditionResult { name: "Name is John Doe", status: Met, children: [] }, ConditionResult { name: "Or", status: Met, children: [ConditionResult { name: "Favorite number is 5", status: Met, children: [] }, ConditionResult { name: "Thinking of a number between 5 and 10", status: Unknown, children: [] }] }] }
This creates a tree like the following:
+---------+
| AND |
+---------+
_____________________/\_______________
| |
V V
+-------------------+ +--------+
| Name is John Doe | | OR |
+-------------------+ +--------+
| field: "name" | ______________/\___________
| value: "John Doe" | | |
+-------------------+ V V
+----------------------+ +-------------------------+
| Favorite number is 5 | | Number between 5 and 10 |
+----------------------+ +-------------------------+
| field: "fav_number" | | field: "thinking_of" |
| value: 5 | | start: 5 |
+----------------------+ | end: 10 |
+-------------------------+
Structs§
- Result of checking a rules tree.
Enums§
- The status of a rule check
Traits§
Functions§
- Creates a
Rule
where all childRule
s must beMet
- Creates a
Rule
wheren
childRule
s must beMet
- Creates a rule for boolean comparison.
- Creates a rule for float comparison.
- Creates a rule for int comparison.
- Creates a
Rule
where any childRule
must beMet
- Creates a rule for string comparison