1 unstable release
| 0.5.0 | Sep 23, 2025 |
|---|
#265 in Template engine
64KB
1.5K
SLoC
evalx
Evalx is a powerful expression evaluator.
This crate is a maintained fork and further development of the original eval by fengcen. The continuation mainly includes bugfixes and updating the Rust edition to 2021, while keeping the public API compatible.
Documentation
Features
Supported operators: ! != "" '' () [] , > < >= <= ==
+ - * / % && || n..m.
Built-in functions: min() max() len() is_empty() array().
Where can evalx be used?
- Template engine
- ...
Usage
Add dependency to Cargo.toml
[dependencies]
evalx = "^0.5"
In your code:
use evalx::{eval, Expr, to_value};
Examples
You can do mathematical calculations with supported operators:
use evalx::{eval, to_value};
assert_eq!(eval("1 + 2 + 3"), Ok(to_value(6)));
assert_eq!(eval("2 * 2 + 3"), Ok(to_value(7)));
assert_eq!(eval("2 / 2 + 3 / 3"), Ok(to_value(2.0)));
You can eval with context:
use evalx::{Expr, to_value};
assert_eq!(Expr::new("foo == bar")
.value("foo", true)
.value("bar", true)
.exec(),
Ok(to_value(true)));
You can access data like javascript by using . and []. [] supports expression.
use evalx::{Expr, to_value};
use std::collections::HashMap;
let mut object = HashMap::new();
object.insert("foos", vec!["Hello", "world", "!"]);
assert_eq!(Expr::new("object.foos[1-1] == 'Hello'")
.value("object", object)
.exec(),
Ok(to_value(true)));
You can eval with function:
use evalx::{Expr, to_value};
assert_eq!(Expr::new("say_hello()")
.function("say_hello", |_| Ok(to_value("Hello world!")))
.exec(),
Ok(to_value("Hello world!")));
You can create an array with array():
use evalx::{eval, to_value};
assert_eq!(eval("array(1, 2, 3, 4, 5)"), Ok(to_value(vec![1, 2, 3, 4, 5])));
You can create an integer array with n..m:
use evalx::{eval, to_value};
assert_eq!(eval("0..5"), Ok(to_value(vec![0, 1, 2, 3, 4])));
Linting (Clippy)
This repository is set up with Clippy for linting.
- Install Clippy (if needed):
rustup component add clippy
- Run Clippy normally:
cargo clippy
- Strict mode (treat all warnings as errors):
cargo clippy-strict
- Pedantic mode (more lints, as warnings):
cargo clippy-pedantic
- CI: Clippy runs in GitHub Actions (see
.github/workflows/ci.yml).
License
evalx is primarily distributed under the terms of the MIT license. See LICENSE for details.
Dependencies
~0.5–1.1MB
~23K SLoC