Skip to main content

quickstart/
quickstart.rs

1//! Complete CEL workflow: parse, type-check, and evaluate.
2//!
3//! Run with: cargo run -p cel-core --example quickstart
4
5use cel_core::{CelType, Env, MapActivation, Value};
6
7fn main() {
8    // 1. Create an environment with the standard library and declare variables
9    let env = Env::with_standard_library()
10        .with_variable("user", CelType::String)
11        .with_variable("age", CelType::Int);
12
13    // 2. Compile the expression (parse + type-check)
14    let ast = env
15        .compile("age >= 21 && user.startsWith('admin')")
16        .unwrap();
17
18    // 3. Create a program from the compiled AST
19    let program = env.program(&ast).unwrap();
20
21    // 4. Set up variable bindings for evaluation
22    let mut activation = MapActivation::new();
23    activation.insert("user", "admin_alice"); // &str converts automatically
24    activation.insert("age", 25); // integers widen automatically
25
26    // 5. Evaluate the expression
27    let result = program.eval(&activation);
28    assert_eq!(result, Value::Bool(true));
29
30    println!("Expression: age >= 21 && user.startsWith('admin')");
31    println!("Result: {}", result);
32}