Crate mf_expression

Source
Expand description

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

NameDescriptionDefault?
regex-deprecatedUses standard regex crateYes
regex-liteOpts for usage of lightweight regex-lite crate. Useful for reducing build size, especially in WASM.No

Re-exports§

pub use expression::Expression;
pub use expression::ExpressionKind;
pub use variable::Variable;
pub use functions::mf_function::MfFunction;
pub use functions::mf_function::MfFunctionRegistry;
pub use functions::defs::FunctionSignature;
pub use functions::StateGuard;
pub use functions::with_state_async;

Modules§

compiler
编译器模块
expression
functions
函数模块
intellisense
lexer
词法分析器模块
parser
语法分析器模块
validate
variable
vm
虚拟机模块 - 操作码执行引擎

Macros§

with_state
便利宏,用于在指定作用域内设置 State

Structs§

Isolate
Isolate 是一个组件,用于封装一个隔离的环境,用于执行表达式。

Enums§

IsolateError
Errors which happen within isolate or during evaluation

Functions§

compile_expression
Compiles a standard expression
compile_unary_expression
Compiles an unary expression
evaluate_expression
Evaluates a standard expression
evaluate_unary_expression
Evaluates a unary expression; Required: context must be an object with “$” key.