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§
- Coalescence
Event - Condition
Result - Result of checking a rules tree.
- Engine
- Event
- Rule
- Rule
Result
Enums§
- Condition
- Constraint
- Error
- Status
- The status of a rule check
Traits§
Functions§
- and
- Creates a
Rule
where all childRule
s must beMet
- at_
least - Creates a
Rule
wheren
childRule
s must beMet
- bool_
equals - Creates a rule for boolean comparison.
- float_
contains - float_
does_ not_ contain - float_
equals - Creates a rule for float comparison.
- float_
greater_ than - float_
greater_ than_ inclusive - float_
in - float_
in_ range - float_
less_ than - float_
less_ than_ inclusive - float_
not_ equals - float_
not_ in - float_
not_ in_ range - int_
contains - int_
contains_ all - int_
contains_ any - int_
does_ not_ contain - int_
does_ not_ contain_ any - int_
equals - Creates a rule for int comparison.
- int_
greater_ than - int_
greater_ than_ inclusive - int_in
- int_
in_ range - int_
less_ than - int_
less_ than_ inclusive - int_
not_ equals - int_
not_ in - int_
not_ in_ range - or
- Creates a
Rule
where any childRule
must beMet
- string_
contains - string_
contains_ all - string_
contains_ any - string_
does_ not_ contain - string_
does_ not_ contain_ any - string_
equals - Creates a rule for string comparison
- string_
in - string_
not_ equals - string_
not_ in