70 releases (38 breaking)

new 0.41.0 Apr 15, 2025
0.40.0 Feb 10, 2025
0.39.0 Dec 31, 2024
0.35.0 Nov 28, 2024
0.8.0 Jul 28, 2023

#110 in Template engine

Download history 359/week @ 2024-12-25 348/week @ 2025-01-01 146/week @ 2025-01-08 352/week @ 2025-01-15 111/week @ 2025-01-22 318/week @ 2025-01-29 601/week @ 2025-02-05 594/week @ 2025-02-12 903/week @ 2025-02-19 882/week @ 2025-02-26 884/week @ 2025-03-05 782/week @ 2025-03-12 729/week @ 2025-03-19 665/week @ 2025-03-26 558/week @ 2025-04-02 932/week @ 2025-04-09

2,977 downloads per month
Used in 2 crates

MIT license

295KB
7K SLoC

A lightweight expression language designed for the evaluation of expressions in various contexts.

Zen Expression is a versatile single-threaded expression language designed for simplicity high-performance. It's primarily used for evaluating and processing JSON data offers key components that empower developers in creating responsive and non-blocking I/O applications Out of the box, it comes with amazing benefits:

  • 🚀 Blazingly fast - Perform millions of evaluations per second
  • 🧠 Intuitive syntax - Minimalistic and expressive syntax
  • 💼 Portable - Can be compiled for all standard architectures including WASM

For a full list of language references, visit documentation.

Example

Evaluate expression using isolate:

use zen_expression::evaluate_expression;
use zen_expression::variable::Variable;
use rust_decimal_macros::dec;
use serde_json::json;

fn main() {
    let context = json!({ "tax": { "percentage": 10 } });
    let tax_amount = evaluate_expression("50 * tax.percentage / 100", context.into()).unwrap();

    assert_eq!(tax_amount, Variable::Number(dec!(5)));
}

High Performance

When evaluating a lot of expressions at once, you can use Isolate directly. Under the hood, Isolate will re-use allocated memory from previous evaluations, drastically improving performance.

use zen_expression::Isolate;
use zen_expression::variable::Variable;
use rust_decimal_macros::dec;
use serde_json::json;

fn main() {
    let context = json!({ "tax": { "percentage": 10 } });
    let mut isolate = Isolate::with_environment(context.into());

    // Fast 🚀
    for _ in 0..1_000 {
        let tax_amount = isolate.run_standard("50 * tax.percentage / 100").unwrap();
        assert_eq!(tax_amount, Variable::Number(dec!(5)));
    }
}

Feature flags

Name Description Default?
regex-deprecated Uses standard regex crate Yes
regex-lite Opts for usage of lightweight regex-lite crate. Useful for reducing build size, especially in WASM. No

Dependencies

~3–5MB
~88K SLoC