Skip to content
gdt050579Public

About

A fast, expressive rule-based filtering engine for Rust — evaluate boolean expressions over any data structure

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Repository files navigation

whereexpr

A Rust library for evaluating boolean filter expressions over typed records — like a WHERE clause, but for your own types.


What it does

whereexpr lets you take a string like:

cond_1 && (cond_2 || cond_3)

where each named condition is a field-level test like:

age > 30
surname is-one-of [Doe, Smith, Williams] {ignore-case}

...and evaluate the whole thing against any instance of your struct — at runtime, with no macros.

It separates two concerns cleanly:

  • Conditions — per-field predicates (age > 30, name starts-with Jo, status is active)
  • Expressions — boolean combinators over named conditions (cond_a && !cond_b || (cond_c && cond_d))

The same filter can also be written as one rule and compiled with Expression::from_rule_str, or kept with other rules in a text file and read one rule at a time with RuleParser.


Quick example

use whereexpr::*;

struct Person {
    name: String,
    surname: String,
    age: u32,
}

impl Person {
    const NAME: AttributeIndex = AttributeIndex::new(0);
    const SURNAME: AttributeIndex = AttributeIndex::new(1);
    const AGE: AttributeIndex = AttributeIndex::new(2);
}

impl Attributes for Person {
    const TYPE_ID: u64 = 1;
    const TYPE_NAME: &'static str = "Person";    

    fn get(&self, idx: AttributeIndex) -> Option<Value<'_>> {
        match idx {
            Self::NAME    => Some(Value::String(&self.name)),
            Self::SURNAME => Some(Value::String(&self.surname)),
            Self::AGE     => Some(Value::U32(self.age)),
            _             => None,
        }
    }

    fn kind(idx: AttributeIndex) -> Option<ValueKind> {
        match idx {
            Self::NAME    => Some(ValueKind::String),
            Self::SURNAME => Some(ValueKind::String),
            Self::AGE     => Some(ValueKind::U32),
            _             => None,
        }
    }

    fn index(name: &str) -> Option<AttributeIndex> {
        match name {
            "name"    => Some(Self::NAME),
            "surname" => Some(Self::SURNAME),
            "age"     => Some(Self::AGE),
            _         => None,
        }
    }
}

fn main() {
    let expr = ExpressionBuilder::<Person>::new()
        .add("is_john",   Condition::from_str("name is John"))
        .add("known_fam", Condition::from_str("surname is-one-of [Doe, Smith, Williams] {ignore-case}"))
        .add("adult_plus",Condition::from_str("age > 30"))
        .build("is_john && known_fam && adult_plus")
        .unwrap();

    let person = Person {
        name: "John".to_string(),
        surname: "doe".to_string(),
        age: 33,
    };

    println!("matches: {}", expr.matches(&person)); // matches: true
}

How it works

1. Implement Attributes

The Attributes trait is the bridge between the library and your type. You implement three methods:

Method Purpose
get(idx) Return the field value for the given index
kind(idx) Return the static ValueKind for a field index
index(name) Map a field name string to its AttributeIndex

Or let #[derive(Attributes)] write it for you: see Deriving Attributes.

2. Define conditions

Conditions are named rules, each testing one field of your struct.

From a string (condition DSL):

Condition::from_str("age >= 18")
Condition::from_str("status is-not banned {ignore-case}")
Condition::from_str("score in-range [0, 100]")

Programmatically:

let pred = Predicate::with_value(Operation::GreaterThan, 18u32)?;
Condition::new("age", pred)

3. Build an expression

Register your conditions with ExpressionBuilder and pass a boolean expression string:

let expr = ExpressionBuilder::<MyType>::new()
    .add("rule_a", Condition::from_str("field_x is foo"))
    .add("rule_b", Condition::from_str("field_y > 10"))
    .build("rule_a && !rule_b")
    .unwrap();

4. Or compile a rule string

Expression::from_rule_str takes the conditions and the boolean expression in one string. Each condition value is a condition-DSL string inside single quotes, double quotes, or triple quotes. The where block combines those names with the same operators as build: AND/&&, OR/||, NOT/!/~, and parentheses.

let expr = Expression::from_rule_str::<Person>(r#"
rule adults: Person {
    is_john    = "name is John"
    known_fam  = "surname is-one-of [Doe, Smith, Williams] {ignore-case}"
    adult_plus = "age > 30"
} where {
    is_john AND known_fam AND adult_plus
}
"#).unwrap();

The type name after : is the type the rule applies to. With the enable_type_check feature it must equal T::TYPE_NAME; otherwise that name is not checked.

Whitespace is allowed in the where block. A condition name starts with a letter and may then contain letters, digits, _, and -.

The from_rule example filters a small bookshelf with one rule:

cd examples
cargo run --example from_rule

Several rules in one text

RuleParser reads any number of rules from one text, such as a rules file, one rule at a time. next_rule_definition returns the next rule as a RuleDefinition; expression::<T>() compiles it for the type T:

let text = std::fs::read_to_string("rules.wx").unwrap();
let mut parser = RuleParser::new(&text);
let mut people = Vec::new();
let mut books = Vec::new();

while let Some(definition) = parser.next_rule_definition()? {
    let name = definition.name().to_string();
    match definition.type_name() {
        "Person" => people.push((name, definition.expression::<Person>()?)),
        "Book" => books.push((name, definition.expression::<Book>()?)),
        other => eprintln!("rule {name}: unknown type {other}"),
    }
}
rule adults: Person { adult = "age >= 18" } where { adult }
rule alice:  Person { named = "name is Alice" } where { named }
rule cheap:  Book   { low = "price < 10" } where { low }
  • One rule at a time. A RuleDefinition borrows the parser, so take what you need from it (compile it, copy its name and metadata) before reading the next rule.
  • You choose the type. type_name() is the name after :, so one file can hold rules for several types; compile each with the matching T. With the enable_type_check feature, expression::<T>() returns Error::TypeMismatch when that name is not T::TYPE_NAME; without it, the name is not checked.
  • A rule that does not compile (an unknown attribute, a malformed condition, an unknown name in where) is an Err from expression::<T>(). You decide whether to stop or continue with the next rule.
  • A syntax error (a missing :, {, } or where, an unclosed string) is returned once by next_rule_definition, and every later call returns Ok(None), because the start of the next rule is no longer known.
  • Duplicate rule names are allowed. Every rule is returned.

Metadata: with meta

A rule can end with a with meta { ... } block of key = value entries. Metadata never affects matching: it is data for your own code, such as a severity, an owner or tags.

rule big_logs: File {
    log = "extension is log"
    big = "size > 1000"
} where {
    log AND big
} with meta {
    severity = high
    priority = 3
    tags     = [disk, "high load"]
}
  • A value is a word or a number (high, 3, -1, 0.5, 2024-01-01, 10.0.0.1), or a string in single, double or triple quotes. Quotes are removed, so owner = ops and owner = "ops" are the same value.
  • A list is written [a, b, c], with commas between the values. [] is an empty list, and a comma after the last value is allowed.
  • Keys start with a letter, ignore ASCII case (Owner is owner), and can appear only once per rule (Error::DuplicateMetaKey).
  • Values are text. Your code knows what each key means and converts it, for example with parse::<i32>().
let meta = definition.meta();

let severity = meta.get("severity").and_then(|item| item.value()).unwrap_or("low");
let priority: i32 = meta
    .get("priority")
    .and_then(|item| item.value())
    .and_then(|value| value.parse().ok())
    .unwrap_or(0);

if let Some(tags) = meta.get("tags") {
    for index in 0..tags.values_count() {
        println!("tag: {}", tags.array_value(index).unwrap());
    }
}

// every entry, in the order written
for index in 0..meta.len() {
    let item = meta.get(index).unwrap();
    println!("{} = {:?}", item.key(), item.value());
}

value() is the first value (None for an empty list), array_value(i) is the value at position i, and values_count() is how many there are. A single value counts as a list of one, so code that expects a list also accepts tags = disk. is_list() tells whether the value was written with brackets.

  • Rules can be separated by any whitespace, or none. Empty text yields no rules.

from_rule_str reads only the first rule of a text.

5. Evaluate

// true only if the expression is definitely true; an unknown result counts as no match
let result: bool = expr.matches(&my_value);

// Some(true) / Some(false), or None if the result is unknown
let result: Option<bool> = expr.evaluate(&my_value);

A condition is unknown when its attribute is missing (get returns None). That does not automatically make the whole expression unknown. See Missing values and three-valued logic.

Both methods panic if the object is not the type the expression was built for (only checked with the enable_type_check feature, see Features).

The compiled Expression is reusable — build it once and call matches on many values.


Deriving Attributes

#[derive(Attributes)] writes the impl for you. It is always available; no feature is needed. The generated get, kind and index are the same match statements you would write by hand.

use whereexpr::*;

#[derive(Attributes)]
#[whereexpr(property = "fn word_count as words -> u32")]
struct Person {
    name: String,
    #[whereexpr(name = "last_name")]
    surname: String,
    age: Option<u32>,          // None → conditions on `age` are unknown
    #[whereexpr(kind = datetime)]
    created: u64,              // a Unix timestamp, not a plain number
    #[whereexpr(ignore)]
    cache: Vec<u64>,
}

impl Person {
    fn word_count(&self) -> Option<u32> {
        Some(self.name.split_whitespace().count() as u32)
    }
}

The kind of each field is inferred from its type:

Field type Kind
String, &str, Box<str>, Rc<str>, Arc<str>, Cow<str> string
PathBuf, &Path, Cow<Path> path (non UTF-8 paths are unknown)
u8…u64, i8…i64, f32, f64 the same numeric kind
usize, isize u64, i64
bool bool
IpAddr, Ipv4Addr, Ipv6Addr ip
SystemTime datetime
[u8; 16], [u8; 20], [u8; 32] hash128, hash160, hash256
Vec<u8>, &[u8] bytes
Vec<&str>, &[&str] string_list
Option<T> the kind of T; None is unknown
Option Where Effect
name = "name" field Name used in conditions (letters, digits, _ and ., starting with a letter)
kind = datetime field Kind for an ambiguous or unrecognised type (numbers may be widened, never narrowed)
kind = enum field An enum implementing WhereexprEnum, compared by variant (see Enums); never inferred
ignore field Not exposed (needed for types that cannot be inferred)
name = "Person" struct TYPE_NAME, used by rule x: Person { ... } (default: the struct name)
id = 42 struct TYPE_ID (default: the FNV-64 hash of TYPE_NAME)
property = "fn word_count as words -> u32" struct A property: self.word_count() exposed as words, called only when needed. A path with :: calls a function

Mistakes are compile errors: an unsupported type without kind or ignore, a name the condition parser would reject, a lossy numeric kind, or two attributes with the same name.


Properties

A property is an attribute that is not a stored field: its value is computed from the object when a condition needs it. Conditions use properties exactly like fields (words > 5), so you can filter on derived values without storing them.

With #[derive(Attributes)], declare a property with property = "fn <name> [as <attribute>] -> <kind>" (see Deriving Attributes):

#[derive(Attributes)]
#[whereexpr(property = "fn word_count as words -> u32")]
struct TextStats {
    text: String,
}

impl TextStats {
    fn word_count(&self) -> Option<u32> {
        Some(self.text.split_whitespace().count() as u32)
    }
}

Written by hand, a property is simply a value that Attributes::get computes instead of reading a field:

use whereexpr::*;

struct TextStats {
    text: String,
}

impl TextStats {
    const TEXT:   AttributeIndex = AttributeIndex::new(0);
    const LENGTH: AttributeIndex = AttributeIndex::new(1); // property, not stored
    const WORDS:  AttributeIndex = AttributeIndex::new(2); // property, not stored
}

impl Attributes for TextStats {
    const TYPE_ID: u64 = 2;
    const TYPE_NAME: &'static str = "TextStats";

    fn get(&self, idx: AttributeIndex) -> Option<Value<'_>> {
        match idx {
            Self::TEXT   => Some(Value::String(&self.text)),
            Self::LENGTH => Some(Value::U32(self.text.chars().count() as u32)),
            Self::WORDS  => Some(Value::U32(self.text.split_whitespace().count() as u32)),
            _            => None,
        }
    }
    fn kind(idx: AttributeIndex) -> Option<ValueKind> {
        match idx {
            Self::TEXT   => Some(ValueKind::String),
            Self::LENGTH => Some(ValueKind::U32),
            Self::WORDS  => Some(ValueKind::U32),
            _            => None,
        }
    }
    fn index(name: &str) -> Option<AttributeIndex> {
        match name {
            "text"   => Some(Self::TEXT),
            "length" => Some(Self::LENGTH),
            "words"  => Some(Self::WORDS),
            _        => None,
        }
    }
}

let expr = ExpressionBuilder::<TextStats>::new()
    .add("has_rust",   Condition::from_str("text contains Rust"))
    .add("not_too_long", Condition::from_str("length <= 60"))
    .add("many_words", Condition::from_str("words > 5"))
    .build("has_rust && not_too_long && many_words")
    .unwrap();

Properties are evaluated lazily — only when the expression actually reaches the condition that references them.


Enums

An enum field can be compared by variant: color is red, color is-one-of [red, blue]. Names are turned into numbers once, when the expression is built, so evaluating the condition compares integers (and is-one-of tests a single bit), never strings.

Two derives do the work: #[derive(WhereexprEnum)] on the enum gives each variant an id and a way to find it by name, and #[whereexpr(kind = enum)] on the field tells #[derive(Attributes)] to use it:

use whereexpr::*;

#[derive(WhereexprEnum, Clone, Copy)]
enum Color {
    #[whereexpr(aliases = ["red", "rosu"])]
    Red,      // id 0, written `red` or `rosu`
    Green,    // id 1, written `green`
    Blue,     // id 2, written `blue`
}

#[derive(Attributes)]
struct Car {
    #[whereexpr(kind = enum)]
    color: Color,
    doors: u8,
}

let expr = ExpressionBuilder::<Car>::new()
    .add("warm", Condition::from_str("color is-one-of [rosu, green]"))
    .add("small", Condition::from_str("doors <= 3"))
    .build("warm && small")
    .unwrap();

assert!(expr.matches(&Car { color: Color::Red, doors: 3 }));
assert!(!expr.matches(&Car { color: Color::Blue, doors: 3 }));

#[derive(WhereexprEnum)]

  • Variants are numbered in declaration order (0, 1, 2, ...). Rules store names, never numbers, so reordering the enum does not change what a rule means.
  • A variant is written as its name in lower case (Green → green). #[whereexpr(aliases = ["red", "rosu"])] replaces that name with the listed ones. Names ignore ASCII case (RED, Rosu), and the same name cannot be used twice.
  • Only variants without data and without an explicit discriminant are accepted (Red(u8) and Red = 1 are compile errors).
  • The names are not stored in the program: as_u16_from_str is a match on a 64-bit hash of the name. A text that is not a name is recognised as such unless it happens to have the same hash as one.
  • The derive implements WhereexprEnum without its full path, so import the trait (use whereexpr::WhereexprEnum; or use whereexpr::*;) where you use it.

#[whereexpr(kind = enum)]

  • Works for E, Option<E> (None is unknown) and &E. The kind is never inferred: the derive cannot tell from the field type that it is an enum.
  • Each enum field uses its own enum, so a struct can hold several (color is electric fails if electric is a Fuel value).
  • Properties cannot be enums (the derive would not know which enum the function returns).

In conditions only is, is-not, is-one-of and is-not-one-of are allowed. {ignore-case} is accepted and changes nothing, since names always ignore case. An unknown name is an error when the expression is built: Error::UnknownEnumValue("rde", "color"), whose message names what was written but not the valid names.

Without the derives, return Value::Enum(id) from get, ValueKind::Enum from kind, and convert names to ids in enum_value (its default knows no names):

impl Attributes for Car {
    // ... get returns Some(Value::Enum(self.color.as_u16())) for COLOR,
    //     kind returns Some(ValueKind::Enum) for COLOR ...
    fn enum_value(idx: AttributeIndex, name: &str) -> Option<u16> {
        match idx {
            Self::COLOR => Color::as_u16_from_str(name),
            _ => None,
        }
    }
}

as_u16 and as_u16_from_str can also be written by hand: WhereexprEnum is an ordinary trait.

A complete example, with rules, an optional enum field and an unknown name:

cd examples
cargo run --example enums

Condition DSL syntax

A condition string has the form:

<attribute> <operation> <value>  [<modifiers>]

Single value:

name is Alice
status is-not active
age > 30
created-at < 1700000000

List value — one or more comma-separated entries inside [ ]:

score in-range [1, 100]
role is-one-of [admin, moderator] {ignore-case}
path ends-with-one-of [.log, .tmp]
tag contains-one-of [warn, error, fatal]

Glob with a list — the glob operation also accepts a list; the attribute matches if it matches any of the patterns:

full_path glob-match [**/*.rs, **/*.md, **/Cargo.*]
filename  not-glob   [*.tmp, *.bak, *.swp]

Regular expressions — re-match tests a String or Path attribute against a regex. The pattern must be single-quoted: regex syntax uses characters the parser reserves (, separates list entries, a leading [ opens a list, a trailing } opens a modifier block). Matching is unanchored, so use ^ and $ for a full match:

name     re-match '^report_\d{4}-\d{2}-\d{2}$'
path     re-match '\.log$'
filename not-re-match '^tmp_'

Double quotes are rejected — they go through escape processing, which would reject \d. The pattern is compiled when the expression is built, so an invalid regex is reported by build() rather than at match time. {ignore-case} is not accepted here; use the inline (?i) flag, which can also be scoped to part of the pattern:

message re-match '(?i)\berror\b'
name    re-match '(?i:alice)Smith'

DateTime strings — DateTime attributes accept both raw Unix timestamps and human-readable date strings in YYYY-MM-DD format:

modified_at > 2024-01-01
created_at  in-range [2020-01-01, 2024-12-31]
expires_at  < 1700000000

Modifiers appear at the end inside {...}:

Modifier Effect
{ignore-case} Case-insensitive match (strings and paths)

{ignore-case} applies to the equality, prefix, suffix, substring and membership operations. It is rejected by re-match / not-re-match — use the inline (?i) flag in the pattern instead — and has no effect on glob or on numeric types.


Operations

Operation DSL keywords
Equality is, eq, ==
Inequality is-not, neq, !=
One of a list is-one-of, in
Not one of is-not-one-of, not-in
Starts with starts-with
Does not start with not-starts-with
Starts with one of starts-with-one-of
Does not start with any not-starts-with-one-of
Ends with ends-with
Does not end with not-ends-with
Ends with one of ends-with-one-of
Does not end with any not-ends-with-one-of
Contains contains
Does not contain not-contains
Contains one of contains-one-of
Contains none of not-contains-one-of
Has has
Does not have not-has
Has one of has-one-of
Does not have one of not-has-one-of
Glob pattern match glob, glob-match
Glob pattern no match not-glob, not-glob-match
Regex match re-match
Regex no match not-re-match
Greater than >, gt, greater-than
Greater than or equal >=, gte, greater-than-or-equal
Less than <, lt, less-than
Less than or equal <=, lte, less-than-or-equal
In range (inclusive) in-range
Not in range not-in-range

Expression syntax

Boolean expressions combine named condition rules:

Syntax Meaning
rule_a && rule_b AND (and also accepted)
rule_a || rule_b OR (or also accepted)
!rule_a or ~rule_a NOT (not also accepted)
(rule_a || rule_b) && rule_c Grouping with parentheses

Conditions:

  • conditions names must start with a letter and contain only letters, digits, _, and -.
  • AND and OR cannot be mixed at the same parenthesis level without explicit grouping.
  • Parentheses can be nested up to 8 levels deep.

A rule's where block, passed to Expression::from_rule_str or read by RuleParser, uses exactly the same syntax.


Missing values and three-valued logic

A condition does not always produce true or false. If Attributes::get returns None for the attribute it tests (the field is missing, optional, or cannot be computed for this object), the condition's result is unknown.

Expressions combine true, false and unknown using three-valued (Kleene) logic, the same rules SQL uses for NULL:

  • AND: false if any operand is false. Otherwise unknown if any operand is unknown. Otherwise true.
  • OR: true if any operand is true. Otherwise unknown if any operand is unknown. Otherwise false.
  • NOT: swaps true and false. Unknown stays unknown.

In other words, an unknown operand only matters when the known operands can't decide the result on their own.

a b a && b a || b !a
true unknown unknown true false
false unknown false unknown true
unknown unknown unknown unknown unknown

The result does not depend on the order of the operands. false && unknown and unknown && false are both false, and the same holds for any number of operands:

Expression Result
false && false && unknown && false false
false && false && unknown && true false
true && true && unknown && true unknown
false || false || unknown || false unknown
false || false || unknown || true true

Evaluation still short-circuits: an AND stops at the first false and an OR stops at the first true. An unknown operand does not stop evaluation, because a later operand may still decide the result.

matches and evaluate report an unknown result differently:

Result of the expression matches evaluate
true true Some(true)
false false Some(false)
unknown false None

matches treats unknown as "no match", the same way a SQL WHERE clause drops rows whose condition is NULL. It never panics because of a missing attribute, so it is safe to use when filtering records with optional fields. Use evaluate when you need to tell "does not match" apart from "cannot tell".

// `age` is an `Option<u32>`, and `get` returns `None` for AGE when it is not set.
let expr = ExpressionBuilder::<Person>::new()
    .add("is_john", Condition::from_str("name is John"))
    .add("over_30", Condition::from_str("age > 30"))
    .build("is_john && over_30")
    .unwrap();

let alice = Person { name: "Alice".into(), age: None };
let john  = Person { name: "John".into(),  age: None };

// The name already makes the AND false, so the missing age does not matter.
expr.matches(&alice);     // false
expr.evaluate(&alice);    // Some(false)

// The name is John, so the missing age decides the result: unknown.
expr.matches(&john);      // false
expr.evaluate(&john);     // None

The three_valued_logic example checks these rules against a set of expressions and exits with an error if any result is wrong:

cd examples
cargo run --example three_valued_logic

Supported value types

ValueKind Rust type DSL name
String &str string
Path &str path
Bool bool bool
U8–U64 u8–u64 u8–u64
I8–I64 i8–i64 i8–i64
F32, F64 f32, f64 f32, f64
Hash128/160/256 [u8; N] hash128, hash160, hash256
IpAddr std::net::IpAddr ip
DateTime u64 (Unix timestamp) datetime
Enum u16 (variant id) enum

Features

Feature Effect
error_description Enables Display and description() on Error, Operation, and ValueKind for human-readable error messages
enable_type_check matches and evaluate panic if the object is not the type the expression was built for. from_rule_str and RuleDefinition::expression also return Error::TypeMismatch when the rule's type name is not T::TYPE_NAME

Enable in Cargo.toml:

[dependencies]
whereexpr = { version = "0.3", features = ["error_description"] }

License

MIT

About

A fast, expressive rule-based filtering engine for Rust — evaluate boolean expressions over any data structure

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages