1 unstable release

0.1.0 Aug 18, 2023

#243 in Parser tooling

MIT license

40KB
932 lines

Rust

coolrule

Boolean expression evaluation engine (a port of boolrule to Rust).

// Without context
let expr = coolrule::new("1 in (1, 2, 3) or 2 > 3")?;
let test = expr.test()?; // true

// With context
let expr = coolrule::new("x ∉ (5, 6, 7)")?;
let test = expr.test_with_context(
    HashMap::from([(vec!["x"], Value::Number(8.0))])
)?; // true

The boolrule test suite has also been ported (and passes) see lib.rs.

Expressions are parsed via PEG parser combinators (powered by pom).

It's around 3x faster than the Python version (before any kind of optimization work).

I'm still learning how to write idiomatic Rust so if you see anything strange please let me know!

Tests

cargo test


lib.rs:

A Rust port of boolrule.

This library evaluates boolean expressions.

Usage

To use this library, you need to create a CoolRule instance by parsing a boolean expression string. The library supports evaluating boolean expressions containing various operations such as and, or, not, comparisons, and set membership checks. Expressions can be evaluated with or without a context, where the context provides values for variables used in the expression.

Examples

Creating and testing an expression without context:

let expr = coolrule::new("1 > 2 and 3 <= 5").unwrap();
let result = expr.test().unwrap(); // false

Creating and testing an expression with context:

use coolrule::{Value};
use std::collections::HashMap;

let expr = coolrule::new("x == 5").unwrap();
let mut context = HashMap::new();
context.insert(vec!["x"], Value::Number(5.0));
let result = expr.test_with_context(&context).unwrap(); // true

Dependencies

~795KB
~10K SLoC