13 releases (8 breaking)

0.9.0 Nov 4, 2024
0.8.1 Jul 29, 2024
0.7.1 Jun 14, 2024
0.6.1 Mar 20, 2024
0.1.1 Dec 20, 2020

#25 in Programming languages

Download history 269/week @ 2024-08-16 176/week @ 2024-08-23 637/week @ 2024-08-30 226/week @ 2024-09-06 232/week @ 2024-09-13 320/week @ 2024-09-20 528/week @ 2024-09-27 594/week @ 2024-10-04 653/week @ 2024-10-11 626/week @ 2024-10-18 524/week @ 2024-10-25 859/week @ 2024-11-01 390/week @ 2024-11-08 484/week @ 2024-11-15 335/week @ 2024-11-22 225/week @ 2024-11-29

1,542 downloads per month
Used in 5 crates (4 directly)

MIT license

195KB
4.5K 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–7.5MB
~128K SLoC