#表达式语言 #解释器 #CEL #评估 #持续时间 #变量 #如何

cel-interpreter

通用表达式语言(CEL)的解释器

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

Download history 41/week @ 2024-05-04 84/week @ 2024-05-11 37/week @ 2024-05-18 24/week @ 2024-05-25 43/week @ 2024-06-01 98/week @ 2024-06-08 336/week @ 2024-06-15 175/week @ 2024-06-22 216/week @ 2024-06-29 409/week @ 2024-07-06 310/week @ 2024-07-13 94/week @ 2024-07-20 254/week @ 2024-07-27 140/week @ 2024-08-03 174/week @ 2024-08-10 238/week @ 2024-08-17

每月下载量 812
2 crates 中使用

MIT许可 MIT

180KB
4K SLoC

CEL解释器

Rust

通用表达式语言(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