Skip to main content

extensions/
extensions.rs

1//! Using CEL extension libraries for type checking.
2//!
3//! Note: Extension function evaluation is in progress. This example demonstrates
4//! that expressions using extension functions type-check correctly.
5//!
6//! Run with: cargo run -p cel-core --example extensions
7
8use cel_core::{CelType, Env};
9
10fn main() {
11    // Enable all extensions (strings, math, encoders, optionals)
12    // Extensions currently provide type declarations for the checker
13    let env = Env::with_standard_library()
14        .with_all_extensions()
15        .with_variable("values", CelType::list(CelType::Int))
16        .with_variable("text", CelType::String);
17
18    // Math extension functions type-check correctly
19    let ast = env.compile("math.greatest(values)").unwrap();
20    println!("math.greatest(values) type: {:?}", ast.result_type());
21
22    let ast = env.compile("math.least(values)").unwrap();
23    println!("math.least(values) type:    {:?}", ast.result_type());
24
25    let ast = env.compile("math.abs(-42)").unwrap();
26    println!("math.abs(-42) type:         {:?}", ast.result_type());
27
28    // String extension functions type-check correctly
29    let ast = env.compile("text.split(' ')").unwrap();
30    println!("text.split(' ') type:       {:?}", ast.result_type());
31
32    let ast = env.compile("['a', 'b'].join('-')").unwrap();
33    println!("['a','b'].join('-') type:   {:?}", ast.result_type());
34
35    // Note: Runtime evaluation of extension functions is in progress.
36    // For now, use standard library functions that are fully implemented:
37    println!("\n=== Standard library (fully implemented) ===");
38
39    let ast = env.compile("size(values)").unwrap();
40    println!("size(values) type: {:?}", ast.result_type());
41
42    let ast = env.compile("text.contains('hello')").unwrap();
43    println!("text.contains type: {:?}", ast.result_type());
44
45    let ast = env.compile("text.startsWith('h')").unwrap();
46    println!("text.startsWith type: {:?}", ast.result_type());
47}