12个版本 (7个重大更新)
0.8.1 | 2024年7月29日 |
---|---|
0.7.1 | 2024年6月14日 |
0.6.1 | 2024年3月20日 |
0.5.0 | 2023年12月1日 |
0.1.1 | 2020年12月20日 |
在 编程语言 类别中排名 54
每月下载量 812 次
在 2 crates 中使用
180KB
4K SLoC
CEL解释器
通用表达式语言(CEL)是一种非图灵完备的语言,设计用于简单性、速度、安全性和可移植性。CEL的C-like语法与C++、Go、Java和TypeScript中的等效表达式几乎相同。CEL非常适合轻量级表达式评估,当完全沙箱化的脚本语言过于资源密集时。
// Check whether a resource name starts with a group name.
resource.name.startsWith("/groups/" + auth.claims.group)
// Determine whether the request is in the permitted time window.
request.time - resource.age < duration("24h")
// Check whether all resource names in a list match a given filter.
auth.claims.email_verified && resources.all(r, r.startsWith(auth.claims.email))
入门指南
此项目包含一个解析器和解释器,这意味着它可以用来评估CEL表达式。该库旨在非常易于使用,同时仍然快速、安全且可定制。
use cel_interpreter::{Context, Program};
fn main() {
// Compile a CEL program
let program = Program::compile("add(2, 3)").unwrap();
// Add any variables or functions that the program will need
let mut context = Context::default();
context.add_function("add", |a: i64, b: i64| a + b);
// Run the program
let value = program.execute(&context).unwrap();
assert_eq!(value, 5.into());
}
示例
查看以下其他示例以了解如何使用此库
依赖关系
~4.5–8MB
~131K SLoC