clarkmcc / cel-rust

Common Expression Language interpreter written in Rust
https://crates.io/crates/cel-interpreter
MIT License
363 stars 18 forks source link

Added function context for better function utilities #7

Closed clarkmcc closed 1 year ago

clarkmcc commented 1 year ago

Adds several improvements to writing functions.

Function Signatures

Previously, every function signature accepted three parameters.

pub fn size(target: Option<&CelType>, args: &[Expression], ctx: &Context) -> Result<CelType, ExecutionError>

This has been simplified to

pub fn size(ftx: FunctionCtx) -> Result<CelType, ExecutionError>

and the FunctionCtx provides methods for accessing the target, args, and expression resolution.

Working With Args and Targets

Checking that the target was None, or that there were enough arges before indexing into them was verbose. The following methods have been added to simplify this.

pub fn contains(ftx: FunctionCtx) -> Result<CelType, ExecutionError> {
    let target = ftx.target()?;
    let arg = ftx.resolve_arg(0)?;
    ...
}