Skip to content

Releases: clarkmcc/cel-rust

v0.4.0 - Better Custom Functions

27 Aug 16:45
Compare
Choose a tag to compare

This release was focused on making it easier to create and register user-defined functions for use in the interpreter. Previously, user-defined functions had to work directly with the internals of the interpreter to load arguments, resolve expressions, and handle errors. This was verbose, repetitive, and not easy for someone unfamiliar with the internals of the library. This release adds Axum-style magic function parameters that allow you to register almost any function or closure using primitives supported by the CEL interpreter. For example, a function that adds two numbers:

Before After
fn add(ftx: FunctionContext) -> ResolveResult {
  let arg1 = ftx.resolve_arg(0)?;
  let arg2 = ftx.resolve_arg(1)?;
  Ok((arg1 + arg2).into())
}
fn add(a: i32, b: i32) -> i32 {
  a + b
}

Notice how:

  • Error handling for missing args is handled implicitly
  • Error handling for wrong types is handled implicitly
  • The return type can be any supported primitive

The hope is that this approach will make custom functions more approachable.