This repository contains expression_engine, a Rust library for parsing and executing configurable expressions (useful for rules/business logic).
- Rust (edition 2021) — core implementation.
rust_decimal— decimal-based numeric model used by the runtimeValue::Number.once_cell— global, lazily initialized registries (operators/functions/init guards).rstest— parameterized unit tests.criterion— benchmarks inbenches/.
Expression execution pipeline:
- Tokenize input string into lexical tokens.
- Parse tokens into
ExprAST. - Execute AST against a mutable
Context. - Dispatch operators/functions through registries (built-in + user-registered).
Primary API entrypoints are in src/lib.rs:
execute(expr, ctx)parse_expression(expr)register_function(...)register_prefix_op(...)register_postfix_op(...)register_infix_op(...)create_context!macro (fromsrc/context.rs)
-
src/lib.rs
Public API surface and crate-level docs; initializes registries and forwards to parser/runtime. -
src/context.rs
Runtime symbol table (Context) for variables/functions backed byArc<Mutex<HashMap<...>>>; includescreate_context!macro. -
src/value.rs
Runtime value model:String,Number,Bool,List,Map,None, plus conversion helpers. -
src/error.rs/src/define.rs
Error enum andResult<T>alias used crate-wide.
-
src/tokenizer.rs
Lexical scanner turning raw text intoTokens. -
src/token.rs
Token/Span definitions and token utility methods. -
src/parser.rs
ExprASTdefinition, expression parser (precedence/associativity aware), AST display/description, and AST execution logic.
-
src/operator.rs
Prefix/infix/postfix operator managers, built-in operator registration, precedence/associativity metadata. -
src/function.rs
Built-in function registry (min,max,sum,avg,len,concat,first,keys, etc.) and lookup/register APIs. -
src/keyword.rs
Operator classification helpers used by tokenizer/parser. -
src/init.rs
One-time initialization for built-in operators/functions.
src/descriptor.rs
Descriptor manager for custom AST string descriptions (ExprAST::describecustomization hooks).
- Unit tests are colocated in each
src/*.rsmodule. - Doc tests run from API docs in
src/lib.rsand macro docs insrc/context.rs. - Benchmarks:
benches/execute_expression.rsbenches/display_expression.rs
- GitHub Actions (
.github/workflows/) cover multi-platform tests, formatting checks, clippy analysis, and coverage.
src/lib.rs(public API)src/value.rs+src/context.rs(runtime data model)src/tokenizer.rs+src/token.rs(lexing)src/parser.rs(AST + parse + execute flow)src/operator.rs+src/function.rs(semantic dispatch/extensibility)
cargo testcargo buildcargo clippy --all-targetscargo bench