55 releases (30 breaking)

0.33.0 Oct 23, 2024
0.31.0 Oct 10, 2024
0.24.2 Jul 17, 2024
0.19.0 Mar 25, 2024
0.8.0 Jul 28, 2023

#76 in Date and time

Download history 689/week @ 2024-07-15 190/week @ 2024-07-22 526/week @ 2024-07-29 387/week @ 2024-08-05 110/week @ 2024-08-12 191/week @ 2024-08-19 187/week @ 2024-08-26 143/week @ 2024-09-02 556/week @ 2024-09-09 362/week @ 2024-09-16 839/week @ 2024-09-23 1041/week @ 2024-09-30 692/week @ 2024-10-07 289/week @ 2024-10-14 254/week @ 2024-10-21 251/week @ 2024-10-28

1,528 downloads per month
Used in 2 crates

MIT license

285KB
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