#cel #interpreter

cel-interpreter

An interpreter for the Common Expression Language (CEL)

7 releases (breaking)

new 0.6.0 Feb 17, 2024
0.5.0 Dec 1, 2023
0.4.1 Sep 18, 2023
0.4.0 Aug 27, 2023
0.1.1 Dec 20, 2020

#1 in #cel

Download history 26/week @ 2023-10-26 15/week @ 2023-11-02 18/week @ 2023-11-09 16/week @ 2023-11-16 24/week @ 2023-11-23 67/week @ 2023-11-30 15/week @ 2023-12-07 37/week @ 2023-12-14 26/week @ 2023-12-21 18/week @ 2023-12-28 21/week @ 2024-01-04 30/week @ 2024-01-11 17/week @ 2024-01-18 57/week @ 2024-01-25 46/week @ 2024-02-01 36/week @ 2024-02-08

159 downloads per month
Used in 2 crates

MIT license

130KB
3K SLoC

CEL Interpreter

Rust

The Common Expression Language (CEL) is a non-Turing complete language designed for simplicity, speed, safety, and portability. CEL's C-like syntax looks nearly identical to equivalent expressions in C++, Go, Java, and TypeScript. CEL is ideal for lightweight expression evaluation when a fully sandboxed scripting language is too resource intensive.

// 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))

Getting Started

This project includes a parser and an interpreter which means that it can be used to evaluate CEL-expressions. The library aims to be very simple to use, while still being fast, safe, and customizable.

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());
}

Examples

Check out these other examples to learn how to use this library:

  • Simple - A simple example of how to use the library.
  • Variables - Passing variables and using them in your program.
  • Functions - Defining and using custom functions in your program.
  • Concurrent Execution - Executing the same program concurrently.

Dependencies

~4.5–8MB
~130K SLoC