| Crates.io | rulesxp |
| lib.rs | rulesxp |
| version | 0.3.0 |
| created_at | 2025-10-20 23:40:03.709915+00 |
| updated_at | 2025-12-08 17:46:29.384697+00 |
| description | Multi-language rules expression evaluator supporting JSONLogic and Scheme with strict typing |
| homepage | |
| repository | https://github.com/microsoft/rulesxp |
| max_upload_size | |
| id | 1892891 |
| size | 314,721 |
Multi-Language Rules Expression Evaluator
RulesXP is a minimalistic expression evaluator that supports both JSONLogic and Scheme syntax with strict typing. It's designed for reliable rule evaluation with predictable behavior.
Note that this project is a work in progress and the API and feature set are expected to change
The project supports minimalistic subsets of:
1 !== "1" and 0 !== false. No "truthiness" or automatic conversions42, -5, #xFF)true/false (JSONLogic) or #t/#f (Scheme)"hello world"[1,2,3] (JSONLogic) or (list 1 2 3) (Scheme)foo, +, >={"===": [1, 1]} // Strict equality
{"and": [true, false]} // Boolean logic
{"+": [1, 2, 3]} // Arithmetic
{"if": [true, "yes", "no"]} // Conditionals
{"<": [1, 2, 3]} // Chained comparisons
(equal? 1 1) ; Strict equality
(and #t #f) ; Boolean logic
(+ 1 2 3) ; Arithmetic
(if #t "yes" "no") ; Conditionals
(< 1 2 3) ; Chained comparisons
| JSONLogic | Scheme | Result |
|---|---|---|
{"===": [1, 1]} |
(equal? 1 1) |
true |
{"!==": [1, 2]} |
(not (equal? 1 2)) |
true |
{"+": [1, 2]} |
(+ 1 2) |
3 |
{"and": [true, {">":[5,3]}]} |
(and #t (> 5 3)) |
true |
Add to your Cargo.toml:
[dependencies]
rulesxp = "0.1.0"
use rulesxp::{jsonlogic::parse_jsonlogic, scheme::parse_scheme, evaluator::*};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut env = create_global_env();
// JSONLogic evaluation
let jsonlogic_expr = parse_jsonlogic(r#"{"and": [true, {">": [5, 3]}]}"#)?;
let result = eval(&jsonlogic_expr, &mut env)?;
println!("Result: {}", result); // true
// Scheme evaluation
let scheme_expr = parse_scheme("(and #t (> 5 3))")?;
let result = eval(&scheme_expr, &mut env)?;
println!("Result: {}", result); // #t
Ok(())
}
cargo run --example repl --features="scheme jsonlogic"
+, -, *: Basic arithmetic with overflow detection(+ 1 2 3 4) or {"+": [1,2,3,4]}===, !==: Strict equality (no type coercion)>, <, >=, <=: Numeric comparisons with chainingand, or: Short-circuiting logical operationsnot (!): Logical negationstring-append (cat): String concatenationlist: Create lists from argumentscar, cdr: List access (first element, rest of list)if: Three-argument conditional (if condition then else)max, min: Find maximum/minimum valuesquote: Return literal data without evaluationRulesXP enforces strict error handling:
// Type mismatches are errors
{"===": [1, "1"]} // Error: Cannot compare number and string
{"and": [1, true]} // Error: Expected boolean, got number
// Arity errors caught at parse time
{"if": [true]} // Error: 'if' requires exactly 3 arguments
{"not": []} // Error: 'not' requires exactly 1 argument
...
use rulesxp::{jsonlogic::parse_jsonlogic, scheme::parse_scheme, evaluator::*};
let mut env = evaluator::create_global_env();
let expr = scheme::parse_scheme("(+ 1 2 3)").unwrap();
let result = evaluator::eval(&expr, &mut env).unwrap();
println!("{}", result); // 6
You can extend the evaluator with your own builtins using strongly-typed
Rust functions. These are registered as builtin operations on the
Environment.
use rulesxp::{Error, Value, evaluator};
// Infallible builtin: returns a bare i64
fn add2(a: i64, b: i64) -> i64 {
a + b
}
// Fallible builtin: returns Result<T, Error>
fn safe_div(a: i64, b: i64) -> Result<i64, Error> {
if b == 0 {
Err(Error::EvalError("division by zero".into()))
} else {
Ok(a / b)
}
}
let mut env = evaluator::create_global_env();
env.register_builtin_operation::<(i64, i64)>("add2", add2);
env.register_builtin_operation::<(i64, i64)>("safe-div", safe_div);
// Now you can call (add2 7 5) or (safe-div 6 3) from Scheme
// Or you can call {"add2" : [7, 5]} or {"safe-div" : [6, 3]} from JSONLogic
For list-style and variadic behavior, use the iterator-based
parameter types from rulesxp::evaluator.
use rulesxp::{Error, Value, evaluator};
use rulesxp::evaluator::{Arity, NumIter, ValueIter};
// Single list argument: (sum-list (list 1 2 3 4)) => 10
fn sum_list(nums: NumIter<'_>) -> i64 {
nums.sum()
}
// Variadic over all arguments: (count-numbers 1 "x" 2 #t 3) => 3
fn count_numbers(args: ValueIter<'_>) -> i64 {
args.filter(|v| matches!(v, Value::Number(_))).count() as i64
}
let mut env = evaluator::create_global_env();
// List parameter from a single list argument
env.register_builtin_operation::<(NumIter<'static>,)>("sum-list", sum_list);
// Variadic builtin with explicit arity metadata
env.register_variadic_builtin_operation::<(ValueIter<'static>,)>(
"count-numbers",
Arity::AtLeast(0),
count_numbers,
);
The typed registration APIs currently support:
Parameter types (as elements of the Args tuple):
i64 (number)bool (boolean)&str (borrowed string slices)Value (owned access to the raw AST value)ValueIter<'_> (iterate over &Value from a list/rest argument)NumIter<'_> (iterate over numeric elements as i64)BoolIter<'_> (iterate over boolean elements as bool)StringIter<'_> (iterate over string elements as &str)Return types:
Result<Value, Error>Result<T, Error> where T: Into<Value> (for example i64,
bool, &str, arrays/vectors of those types, or Vec<Value>)T where T: Into<Value> (for infallible helpers, which are
automatically wrapped as Ok(T))Arity is enforced automatically. Conversion errors yield TypeError,
and builtin errors are surfaced directly as Error values.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit Contributor License Agreements.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.