Skip to main content

extract_values/
extract_values.rs

1//! Extracting Rust types from CEL Values using TryFrom.
2//!
3//! Run with: cargo run -p cel-core --example extract_values
4
5use cel_core::{CelType, Env, MapActivation, OptionalValue, Value, ValueMap};
6
7fn main() {
8    let env = Env::with_standard_library()
9        .with_variable("count", CelType::Int)
10        .with_variable("items", CelType::list(CelType::Int))
11        .with_variable("config", CelType::map(CelType::String, CelType::String));
12
13    let mut activation = MapActivation::new();
14    activation.insert("count", 42); // i32 automatically widens to i64
15    activation.insert("items", Value::list([1, 2, 3]));
16    activation.insert(
17        "config",
18        Value::map([("host", "localhost"), ("port", "8080")]),
19    );
20
21    // Extract i64
22    let ast = env.compile("count * 2").unwrap();
23    let program = env.program(&ast).unwrap();
24    let result = program.eval(&activation);
25
26    let value: i64 = (&result).try_into().expect("expected int");
27    println!("i64: {}", value);
28
29    // Extract bool
30    let ast = env.compile("count > 10").unwrap();
31    let program = env.program(&ast).unwrap();
32    let result = program.eval(&activation);
33
34    let value: bool = (&result).try_into().expect("expected bool");
35    println!("bool: {}", value);
36
37    // Extract &str
38    let ast = env.compile("config.host").unwrap();
39    let program = env.program(&ast).unwrap();
40    let result = program.eval(&activation);
41
42    let value: &str = (&result).try_into().expect("expected string");
43    println!("&str: {}", value);
44
45    // Extract &[Value] (list)
46    let ast = env.compile("items.filter(x, x > 1)").unwrap();
47    let program = env.program(&ast).unwrap();
48    let result = program.eval(&activation);
49
50    let list: &[Value] = (&result).try_into().expect("expected list");
51    println!("list length: {}", list.len());
52    for (i, v) in list.iter().enumerate() {
53        let n: i64 = v.try_into().expect("expected int");
54        println!("  [{}] = {}", i, n);
55    }
56
57    // Extract &ValueMap
58    let ast = env.compile("config").unwrap();
59    let program = env.program(&ast).unwrap();
60    let result = program.eval(&activation);
61
62    let map: &ValueMap = (&result).try_into().expect("expected map");
63    println!("map size: {}", map.len());
64    for (k, v) in map.iter() {
65        println!("  {:?} = {}", k, v);
66    }
67
68    // Handle errors gracefully
69    let result = Value::from("not an int");
70    let attempt: Result<i64, _> = (&result).try_into();
71    match attempt {
72        Ok(v) => println!("got: {}", v),
73        Err(e) => println!("conversion error: {}", e),
74    }
75
76    // Extract &OptionalValue
77    let opt_result = Value::optional_some(Value::from(99));
78    let opt: &OptionalValue = (&opt_result).try_into().expect("expected optional");
79    if opt.is_present() {
80        let inner: i64 = opt.as_value().unwrap().try_into().expect("expected int");
81        println!("optional value: {}", inner);
82    }
83}