[][src]Crate json_rules_engine

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() method is called. map is a field: value mapping of facts 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 map.

To construct a tree, see the following methods.

Example

extern crate ruuls;
use serde_json::json;

let tree = ruuls::and(vec![
    ruuls::string_equals("name", "John Doe"),
    ruuls::or(vec![
        ruuls::int_equals("fav_number", 5),
        ruuls::int_in_range("thinking_of", 5, 10)
    ])
]);
let mut facts = json!({
    "name": "John Doe",
    "fav_number": 5
});
let result = tree.check_value(&facts);
println!("{:?}", result);
assert!(result.status == ruuls::Status::Met);
// result = RuleResult { name: "And", status: Met, children: [RuleResult { name: "Name is John Doe", status: Met, children: [] }, RuleResult { name: "Or", status: Met, children: [RuleResult { name: "Favorite number is 5", status: Met, children: [] }, RuleResult { 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

ConditionResult

Result of checking a rules tree.

Enums

Condition

Representation of a node in the rules tree

Constraint
Status

The status of a rule check

Functions

and

Creates a Rule where all child Rules must be Met

at_least

Creates a Rule where n child Rules must be Met

bool_equals

Creates a rule for boolean comparison.

int_equals

Creates a rule for int comparison.

int_in_range

Creates a rule for int range comparison with the interval [start, end].

or

Creates a Rule where any child Rule must be Met

string_equals

Creates a rule for string comparison