#rule

rule

A rule engine written in rust

7 releases

✓ Uses Rust 2018 edition

0.1.6 Oct 8, 2019
0.1.5 Sep 9, 2019
0.1.4 Aug 31, 2019

#141 in Parser tooling

Download history 9/week @ 2019-07-28 9/week @ 2019-08-04 5/week @ 2019-08-11 63/week @ 2019-08-25 31/week @ 2019-09-01 54/week @ 2019-09-08 48/week @ 2019-09-15 44/week @ 2019-09-22 18/week @ 2019-09-29

94 downloads per month

MIT license

29KB
600 lines

Rule

circle-ci crates.io license docs.rs codecov

A rule engine written in rust. There's also a python fork.

The rule is a json string or rust object of a list expression. The expression is like [op, arg0, arg1, ..., argn], the op is the operator, and arg0..n is the arguments for the operator. Any argument can be another expression.

For writing convenience, the first argument will be tried to resolve as the context parameter. Or, you can just use the special var operator to indicate the context parameter.

Usage

#[macro_use]
extern crate rule;

use rule::{Rule, Result};

fn main() -> Result<()> {
    let context = json!({"a": 1, "world": "hello"});

    // match the context with rules
    assert!(Rule::new(json!(["=", "a", 1]))?.matches(&context)?);
    assert!(Rule::new(json!(["=", ["var", "a"], 1]))?.matches(&context)?);
    assert!(Rule::from_str(r#"["=", ["var", "a"], 1]"#)?.matches(&context)?);
    assert!(Rule::from_value(["=", "world", "hello"])?.matches(&context)?);

    // rule! macro
    assert!(rule!["=", "a", 1]?.matches(&context)?);

    // collection operators
    assert!(rule!["in", 1, 1, 2, 3]?.matches(&json!({}))?);
    assert!(rule!["startswith", "hello", "he"]?.matches(&json!({}))?);
    assert!(rule!["startswith", "arr", "foo", "bar"]?.matches(&json!({"arr": ["foo", "bar", "baz"]}))?);
    assert!(rule!["endswith", "arr", "bar", "baz"]?.matches(&json!({"arr": ["foo", "bar", "baz"]}))?);

    Ok(())
}

See rule::op for more supported operators.

ToDos

  • add more built-in Ops
  • support register custom Ops
  • support rule! macro

License

http://tclh123.mit-license.org/

Dependencies

~2MB
~58K SLoC