mf_expression/
lib.rs

1//! A lightweight expression language designed for the evaluation of expressions in various contexts.
2//!
3//! Zen Expression is a versatile single-threaded expression language designed for simplicity
4//! high-performance. It's primarily used for evaluating and processing JSON data offers key components that empower developers in creating responsive and
5//! non-blocking I/O applications
6//! Out of the box, it comes with amazing benefits:
7//! - 🚀 Blazingly fast - Perform millions of evaluations per second
8//! - 🧠 Intuitive syntax - Minimalistic and expressive syntax
9//! - 💼 Portable - Can be compiled for all standard architectures including WASM
10//!
11//! For a full list of language references, visit [documentation](https://gorules.io/docs/rules-engine/expression-language/).
12//!
13//! # Example
14//! Evaluate expression using isolate:
15//! ```
16//! use zen_expression::evaluate_expression;
17//! use zen_expression::variable::Variable;
18//! use rust_decimal_macros::dec;
19//! use serde_json::json;
20//!
21//! fn main() {
22//!     let context = json!({ "tax": { "percentage": 10 } });
23//!     let tax_amount = evaluate_expression("50 * tax.percentage / 100", context.into()).unwrap();
24//!
25//!     assert_eq!(tax_amount, Variable::Number(dec!(5)));
26//! }
27//! ```
28//!
29//! ## High Performance
30//! When evaluating a lot of expressions at once, you can use Isolate directly. Under the hood, Isolate
31//! will re-use allocated memory from previous evaluations, drastically improving performance.
32//!
33//! ```
34//! use zen_expression::Isolate;
35//! use zen_expression::variable::Variable;
36//! use rust_decimal_macros::dec;
37//! use serde_json::json;
38//!
39//! fn main() {
40//!     let context = json!({ "tax": { "percentage": 10 } });
41//!     let mut isolate = Isolate::with_environment(context.into());
42//!
43//!     // Fast 🚀
44//!     for _ in 0..1_000 {
45//!         let tax_amount = isolate.run_standard("50 * tax.percentage / 100").unwrap();
46//!         assert_eq!(tax_amount, Variable::Number(dec!(5)));
47//!     }
48//! }
49//! ```
50//!
51//! # Feature flags
52//!
53//! Name | Description | Default?
54//! ---|---|---
55//! `regex-deprecated` | Uses standard `regex` crate | Yes
56//! `regex-lite` | Opts for usage of lightweight `regex-lite` crate. Useful for reducing build size, especially in WASM. | No
57
58mod isolate;
59
60mod arena;
61pub mod compiler;
62mod exports;
63pub mod expression;
64pub mod functions;
65pub mod intellisense;
66pub mod lexer;
67pub mod parser;
68pub mod validate;
69pub mod variable;
70pub mod vm;
71
72pub use exports::{
73    compile_expression, compile_unary_expression, evaluate_expression,
74    evaluate_unary_expression,
75};
76pub use expression::{Expression, ExpressionKind};
77pub use isolate::{Isolate, IsolateError};
78pub use variable::Variable;
79
80// 导出自定义函数相关
81pub use functions::mf_function::{MfFunction, MfFunctionRegistry};
82pub use functions::defs::FunctionSignature;
83pub use functions::{StateGuard, with_state_async};