pub struct Env { /* private fields */ }Expand description
Unified environment for CEL expression processing.
The Env wraps the parser and checker, providing a high-level API
for working with CEL expressions. It manages:
- Variable declarations with their types
- Function declarations (standard library + extensions)
- Container namespace for qualified name resolution
- Abbreviations for type name shortcuts
§Example
use cel_core::Env;
use cel_core::CelType;
let env = Env::with_standard_library()
.with_variable("x", CelType::Int);
let result = env.compile("x + 1");
assert!(result.is_ok());Implementations§
Source§impl Env
impl Env
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty environment.
This environment has no functions or variables defined.
Use with_standard_library() for a fully-featured environment.
Sourcepub fn with_standard_library() -> Self
pub fn with_standard_library() -> Self
Create a new environment with the CEL standard library.
This includes all standard operators, functions, and type constants.
Examples found in repository?
7fn main() {
8 let env = Env::with_standard_library().with_variable("name", CelType::String);
9
10 let ast = env.compile(r#""Hello, " + name + "!""#).unwrap();
11 let program = env.program(&ast).unwrap();
12
13 let mut activation = MapActivation::new();
14 activation.insert("name", "World");
15
16 let result = program.eval(&activation);
17 println!("{}", result);
18}More examples
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}7fn main() {
8 let env = Env::with_standard_library().with_variable("numbers", CelType::list(CelType::Int));
9
10 let mut activation = MapActivation::new();
11 activation.insert("numbers", Value::list([1, 5, 3, 8, 2]));
12
13 // Filter: keep only values > 3
14 let ast = env.compile("numbers.filter(x, x > 3)").unwrap();
15 let program = env.program(&ast).unwrap();
16 let result = program.eval(&activation);
17 println!("filter(x, x > 3): {}", result);
18
19 // Map: double each value
20 let ast = env.compile("numbers.map(x, x * 2)").unwrap();
21 let program = env.program(&ast).unwrap();
22 let result = program.eval(&activation);
23 println!("map(x, x * 2): {}", result);
24
25 // Exists: any value > 7?
26 let ast = env.compile("numbers.exists(x, x > 7)").unwrap();
27 let program = env.program(&ast).unwrap();
28 let result = program.eval(&activation);
29 println!("exists(x, x > 7): {}", result);
30
31 // All: all values > 0?
32 let ast = env.compile("numbers.all(x, x > 0)").unwrap();
33 let program = env.program(&ast).unwrap();
34 let result = program.eval(&activation);
35 println!("all(x, x > 0): {}", result);
36
37 // Size
38 let ast = env.compile("numbers.size()").unwrap();
39 let program = env.program(&ast).unwrap();
40 let result = program.eval(&activation);
41 println!("size(): {}", result);
42
43 // Contains (using 'in' operator)
44 let ast = env.compile("5 in numbers").unwrap();
45 let program = env.program(&ast).unwrap();
46 let result = program.eval(&activation);
47 println!("5 in numbers: {}", result);
48}7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("user", CelType::map(CelType::String, CelType::Dyn));
10
11 // Mixed value types require explicit Value::from()
12 let user = Value::map([
13 ("name", Value::from("Alice")),
14 ("age", Value::from(30)), // i32 automatically widens to i64
15 ("active", Value::from(true)),
16 ]);
17
18 let mut activation = MapActivation::new();
19 activation.insert("user", user);
20
21 // Field access
22 let ast = env.compile("user.name").unwrap();
23 let program = env.program(&ast).unwrap();
24 let result = program.eval(&activation);
25 println!("user.name: {}", result);
26
27 // Index access
28 let ast = env.compile(r#"user["age"]"#).unwrap();
29 let program = env.program(&ast).unwrap();
30 let result = program.eval(&activation);
31 println!(r#"user["age"]: {}"#, result);
32
33 // Check field existence with 'in'
34 let ast = env.compile(r#""name" in user"#).unwrap();
35 let program = env.program(&ast).unwrap();
36 let result = program.eval(&activation);
37 println!(r#""name" in user: {}"#, result);
38
39 // Check field existence with has()
40 let ast = env.compile("has(user.email)").unwrap();
41 let program = env.program(&ast).unwrap();
42 let result = program.eval(&activation);
43 println!("has(user.email): {}", result);
44
45 // Combine conditions
46 let ast = env.compile(r#"user.active && user.age >= 21"#).unwrap();
47 let program = env.program(&ast).unwrap();
48 let result = program.eval(&activation);
49 println!("active && age >= 21: {}", result);
50}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}7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("x", CelType::Int)
10 .with_variable("items", CelType::list(CelType::Int));
11
12 let mut activation = MapActivation::new();
13
14 // Division by zero returns an error value
15 println!("=== Division by zero ===");
16 activation.insert("x", 0);
17 let ast = env.compile("10 / x").unwrap();
18 let program = env.program(&ast).unwrap();
19 let result = program.eval(&activation);
20
21 match &result {
22 Value::Error(err) => println!("Error: {}", err),
23 other => println!("Result: {}", other),
24 }
25
26 // Index out of bounds
27 println!("\n=== Index out of bounds ===");
28 activation.insert("items", Value::list([1, 2, 3]));
29 let ast = env.compile("items[10]").unwrap();
30 let program = env.program(&ast).unwrap();
31 let result = program.eval(&activation);
32
33 match &result {
34 Value::Error(err) => println!("Error: {}", err),
35 other => println!("Result: {}", other),
36 }
37
38 // Key not found in map
39 println!("\n=== Key not found ===");
40 let env = Env::with_standard_library()
41 .with_variable("config", CelType::map(CelType::String, CelType::String));
42 let mut activation = MapActivation::new();
43 activation.insert("config", Value::map([("host", "localhost")]));
44
45 let ast = env.compile("config.missing_key").unwrap();
46 let program = env.program(&ast).unwrap();
47 let result = program.eval(&activation);
48
49 match &result {
50 Value::Error(err) => println!("Error: {}", err),
51 other => println!("Result: {}", other),
52 }
53
54 // Use has() to safely check field existence
55 println!("\n=== Safe field access with has() ===");
56 let ast = env
57 .compile("has(config.missing_key) ? config.missing_key : 'default'")
58 .unwrap();
59 let program = env.program(&ast).unwrap();
60 let result = program.eval(&activation);
61 println!("Result: {}", result);
62}Sourcepub fn with_variable(self, name: impl Into<String>, cel_type: CelType) -> Self
pub fn with_variable(self, name: impl Into<String>, cel_type: CelType) -> Self
Add a variable to the environment (builder pattern).
§Example
use cel_core::Env;
use cel_core::CelType;
let env = Env::with_standard_library()
.with_variable("x", CelType::Int)
.with_variable("y", CelType::String);Examples found in repository?
7fn main() {
8 let env = Env::with_standard_library().with_variable("name", CelType::String);
9
10 let ast = env.compile(r#""Hello, " + name + "!""#).unwrap();
11 let program = env.program(&ast).unwrap();
12
13 let mut activation = MapActivation::new();
14 activation.insert("name", "World");
15
16 let result = program.eval(&activation);
17 println!("{}", result);
18}More examples
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}7fn main() {
8 let env = Env::with_standard_library().with_variable("numbers", CelType::list(CelType::Int));
9
10 let mut activation = MapActivation::new();
11 activation.insert("numbers", Value::list([1, 5, 3, 8, 2]));
12
13 // Filter: keep only values > 3
14 let ast = env.compile("numbers.filter(x, x > 3)").unwrap();
15 let program = env.program(&ast).unwrap();
16 let result = program.eval(&activation);
17 println!("filter(x, x > 3): {}", result);
18
19 // Map: double each value
20 let ast = env.compile("numbers.map(x, x * 2)").unwrap();
21 let program = env.program(&ast).unwrap();
22 let result = program.eval(&activation);
23 println!("map(x, x * 2): {}", result);
24
25 // Exists: any value > 7?
26 let ast = env.compile("numbers.exists(x, x > 7)").unwrap();
27 let program = env.program(&ast).unwrap();
28 let result = program.eval(&activation);
29 println!("exists(x, x > 7): {}", result);
30
31 // All: all values > 0?
32 let ast = env.compile("numbers.all(x, x > 0)").unwrap();
33 let program = env.program(&ast).unwrap();
34 let result = program.eval(&activation);
35 println!("all(x, x > 0): {}", result);
36
37 // Size
38 let ast = env.compile("numbers.size()").unwrap();
39 let program = env.program(&ast).unwrap();
40 let result = program.eval(&activation);
41 println!("size(): {}", result);
42
43 // Contains (using 'in' operator)
44 let ast = env.compile("5 in numbers").unwrap();
45 let program = env.program(&ast).unwrap();
46 let result = program.eval(&activation);
47 println!("5 in numbers: {}", result);
48}7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("user", CelType::map(CelType::String, CelType::Dyn));
10
11 // Mixed value types require explicit Value::from()
12 let user = Value::map([
13 ("name", Value::from("Alice")),
14 ("age", Value::from(30)), // i32 automatically widens to i64
15 ("active", Value::from(true)),
16 ]);
17
18 let mut activation = MapActivation::new();
19 activation.insert("user", user);
20
21 // Field access
22 let ast = env.compile("user.name").unwrap();
23 let program = env.program(&ast).unwrap();
24 let result = program.eval(&activation);
25 println!("user.name: {}", result);
26
27 // Index access
28 let ast = env.compile(r#"user["age"]"#).unwrap();
29 let program = env.program(&ast).unwrap();
30 let result = program.eval(&activation);
31 println!(r#"user["age"]: {}"#, result);
32
33 // Check field existence with 'in'
34 let ast = env.compile(r#""name" in user"#).unwrap();
35 let program = env.program(&ast).unwrap();
36 let result = program.eval(&activation);
37 println!(r#""name" in user: {}"#, result);
38
39 // Check field existence with has()
40 let ast = env.compile("has(user.email)").unwrap();
41 let program = env.program(&ast).unwrap();
42 let result = program.eval(&activation);
43 println!("has(user.email): {}", result);
44
45 // Combine conditions
46 let ast = env.compile(r#"user.active && user.age >= 21"#).unwrap();
47 let program = env.program(&ast).unwrap();
48 let result = program.eval(&activation);
49 println!("active && age >= 21: {}", result);
50}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}7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("x", CelType::Int)
10 .with_variable("items", CelType::list(CelType::Int));
11
12 let mut activation = MapActivation::new();
13
14 // Division by zero returns an error value
15 println!("=== Division by zero ===");
16 activation.insert("x", 0);
17 let ast = env.compile("10 / x").unwrap();
18 let program = env.program(&ast).unwrap();
19 let result = program.eval(&activation);
20
21 match &result {
22 Value::Error(err) => println!("Error: {}", err),
23 other => println!("Result: {}", other),
24 }
25
26 // Index out of bounds
27 println!("\n=== Index out of bounds ===");
28 activation.insert("items", Value::list([1, 2, 3]));
29 let ast = env.compile("items[10]").unwrap();
30 let program = env.program(&ast).unwrap();
31 let result = program.eval(&activation);
32
33 match &result {
34 Value::Error(err) => println!("Error: {}", err),
35 other => println!("Result: {}", other),
36 }
37
38 // Key not found in map
39 println!("\n=== Key not found ===");
40 let env = Env::with_standard_library()
41 .with_variable("config", CelType::map(CelType::String, CelType::String));
42 let mut activation = MapActivation::new();
43 activation.insert("config", Value::map([("host", "localhost")]));
44
45 let ast = env.compile("config.missing_key").unwrap();
46 let program = env.program(&ast).unwrap();
47 let result = program.eval(&activation);
48
49 match &result {
50 Value::Error(err) => println!("Error: {}", err),
51 other => println!("Result: {}", other),
52 }
53
54 // Use has() to safely check field existence
55 println!("\n=== Safe field access with has() ===");
56 let ast = env
57 .compile("has(config.missing_key) ? config.missing_key : 'default'")
58 .unwrap();
59 let program = env.program(&ast).unwrap();
60 let result = program.eval(&activation);
61 println!("Result: {}", result);
62}Sourcepub fn add_variable(&mut self, name: impl Into<String>, cel_type: CelType)
pub fn add_variable(&mut self, name: impl Into<String>, cel_type: CelType)
Add a variable to the environment (mutable).
Sourcepub fn with_function(self, decl: FunctionDecl) -> Self
pub fn with_function(self, decl: FunctionDecl) -> Self
Add a function declaration to the environment (builder pattern).
Sourcepub fn add_function(&mut self, decl: FunctionDecl)
pub fn add_function(&mut self, decl: FunctionDecl)
Add a function declaration to the environment (mutable).
If a function with the same name already exists, overloads are merged.
Sourcepub fn with_container(self, container: impl Into<String>) -> Self
pub fn with_container(self, container: impl Into<String>) -> Self
Set the container namespace (builder pattern).
The container is used for qualified name resolution.
Sourcepub fn set_container(&mut self, container: impl Into<String>)
pub fn set_container(&mut self, container: impl Into<String>)
Set the container namespace (mutable).
Sourcepub fn with_proto_registry(self, registry: Arc<dyn ProtoRegistry>) -> Self
pub fn with_proto_registry(self, registry: Arc<dyn ProtoRegistry>) -> Self
Set the proto registry (builder pattern).
The proto registry is used for resolving protobuf types during type checking
and evaluation. Pass an Arc<dyn ProtoRegistry> implementation such as
ProstProtoRegistry from cel-core-proto.
Sourcepub fn proto_registry(&self) -> Option<&dyn ProtoRegistry>
pub fn proto_registry(&self) -> Option<&dyn ProtoRegistry>
Get the proto registry.
Sourcepub fn with_abbreviations(self, abbreviations: Abbreviations) -> Self
pub fn with_abbreviations(self, abbreviations: Abbreviations) -> Self
Set abbreviations for qualified name resolution (builder pattern).
Abbreviations allow short names to be used instead of fully-qualified type names in expressions. This is useful when working with protobuf types from multiple packages.
§Example
use cel_core::{Env, Abbreviations};
let abbrevs = Abbreviations::new()
.with_abbreviation("google.protobuf.Duration").unwrap();
let env = Env::with_standard_library()
.with_abbreviations(abbrevs);Sourcepub fn abbreviations(&self) -> &Abbreviations
pub fn abbreviations(&self) -> &Abbreviations
Get the abbreviations.
Sourcepub fn with_legacy_enums(self) -> Self
pub fn with_legacy_enums(self) -> Self
Use legacy (weak) enum mode where enum values are returned as plain integers.
By default, strong enum typing is enabled and enum values carry their
fully-qualified type name. In legacy mode, enum values are represented
as Value::Int, matching older CEL behavior.
Sourcepub fn strong_enums(&self) -> bool
pub fn strong_enums(&self) -> bool
Check if strong enum typing is enabled.
Sourcepub fn with_extension(
self,
extension: impl IntoIterator<Item = FunctionDecl>,
) -> Self
pub fn with_extension( self, extension: impl IntoIterator<Item = FunctionDecl>, ) -> Self
Add an extension library to the environment (builder pattern).
Extensions provide additional functions beyond the standard library.
Each extension is a collection of FunctionDecl values.
§Example
use cel_core::Env;
use cel_core::ext::string_extension;
let env = Env::with_standard_library()
.with_extension(string_extension());Sourcepub fn with_all_extensions(self) -> Self
pub fn with_all_extensions(self) -> Self
Add all available extension libraries to the environment (builder pattern).
This is a convenience method that adds all standard extensions:
- String extension (
charAt,indexOf,substring, etc.) - Math extension (
math.greatest,math.least,math.abs, etc.) - Encoders extension (
base64.encode,base64.decode) - Optionals extension (
optional.of,optional.none,hasValue, etc.)
§Example
use cel_core::Env;
let env = Env::with_standard_library()
.with_all_extensions();Examples found in repository?
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}Sourcepub fn functions(&self) -> &HashMap<String, FunctionDecl>
pub fn functions(&self) -> &HashMap<String, FunctionDecl>
Get the functions map.
Sourcepub fn methods_for_type(&self, receiver: &CelType) -> Vec<(&str, &OverloadDecl)>
pub fn methods_for_type(&self, receiver: &CelType) -> Vec<(&str, &OverloadDecl)>
Get all member functions compatible with a given receiver type.
Returns (function_name, overload) pairs where at least one member overload
has a receiver type that is assignable from the given type.
Handles generic type parameters (e.g., list(T)) by treating them as wildcards.
Sourcepub fn standalone_functions(&self) -> Vec<&str>
pub fn standalone_functions(&self) -> Vec<&str>
Get all standalone (non-operator) function names.
Filters out internal operator functions (like _+_, _-_) and other
internal functions that start with _ or contain @.
Sourcepub fn parse(&self, source: &str) -> ParseResult
pub fn parse(&self, source: &str) -> ParseResult
Parse a CEL expression.
This delegates to the parser. The returned ParseResult may contain
both a partial AST and errors if parsing partially succeeded.
Sourcepub fn check(&self, expr: &SpannedExpr) -> CheckResult
pub fn check(&self, expr: &SpannedExpr) -> CheckResult
Type-check a parsed expression.
This delegates to the checker with the environment’s variables, functions, container, and abbreviations.
Sourcepub fn compile(&self, source: &str) -> Result<Ast, CompileError>
pub fn compile(&self, source: &str) -> Result<Ast, CompileError>
Parse and type-check a CEL expression, returning a checked Ast.
This is the primary entry point for compiling CEL expressions.
Returns a checked Ast that can be used for evaluation.
§Example
use cel_core::Env;
use cel_core::CelType;
let env = Env::with_standard_library()
.with_variable("x", CelType::Int);
let ast = env.compile("x + 1").unwrap();
assert!(ast.is_checked());
assert_eq!(ast.result_type(), Some(&CelType::Int));Examples found in repository?
7fn main() {
8 let env = Env::with_standard_library().with_variable("name", CelType::String);
9
10 let ast = env.compile(r#""Hello, " + name + "!""#).unwrap();
11 let program = env.program(&ast).unwrap();
12
13 let mut activation = MapActivation::new();
14 activation.insert("name", "World");
15
16 let result = program.eval(&activation);
17 println!("{}", result);
18}More examples
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}7fn main() {
8 let env = Env::with_standard_library().with_variable("numbers", CelType::list(CelType::Int));
9
10 let mut activation = MapActivation::new();
11 activation.insert("numbers", Value::list([1, 5, 3, 8, 2]));
12
13 // Filter: keep only values > 3
14 let ast = env.compile("numbers.filter(x, x > 3)").unwrap();
15 let program = env.program(&ast).unwrap();
16 let result = program.eval(&activation);
17 println!("filter(x, x > 3): {}", result);
18
19 // Map: double each value
20 let ast = env.compile("numbers.map(x, x * 2)").unwrap();
21 let program = env.program(&ast).unwrap();
22 let result = program.eval(&activation);
23 println!("map(x, x * 2): {}", result);
24
25 // Exists: any value > 7?
26 let ast = env.compile("numbers.exists(x, x > 7)").unwrap();
27 let program = env.program(&ast).unwrap();
28 let result = program.eval(&activation);
29 println!("exists(x, x > 7): {}", result);
30
31 // All: all values > 0?
32 let ast = env.compile("numbers.all(x, x > 0)").unwrap();
33 let program = env.program(&ast).unwrap();
34 let result = program.eval(&activation);
35 println!("all(x, x > 0): {}", result);
36
37 // Size
38 let ast = env.compile("numbers.size()").unwrap();
39 let program = env.program(&ast).unwrap();
40 let result = program.eval(&activation);
41 println!("size(): {}", result);
42
43 // Contains (using 'in' operator)
44 let ast = env.compile("5 in numbers").unwrap();
45 let program = env.program(&ast).unwrap();
46 let result = program.eval(&activation);
47 println!("5 in numbers: {}", result);
48}7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("user", CelType::map(CelType::String, CelType::Dyn));
10
11 // Mixed value types require explicit Value::from()
12 let user = Value::map([
13 ("name", Value::from("Alice")),
14 ("age", Value::from(30)), // i32 automatically widens to i64
15 ("active", Value::from(true)),
16 ]);
17
18 let mut activation = MapActivation::new();
19 activation.insert("user", user);
20
21 // Field access
22 let ast = env.compile("user.name").unwrap();
23 let program = env.program(&ast).unwrap();
24 let result = program.eval(&activation);
25 println!("user.name: {}", result);
26
27 // Index access
28 let ast = env.compile(r#"user["age"]"#).unwrap();
29 let program = env.program(&ast).unwrap();
30 let result = program.eval(&activation);
31 println!(r#"user["age"]: {}"#, result);
32
33 // Check field existence with 'in'
34 let ast = env.compile(r#""name" in user"#).unwrap();
35 let program = env.program(&ast).unwrap();
36 let result = program.eval(&activation);
37 println!(r#""name" in user: {}"#, result);
38
39 // Check field existence with has()
40 let ast = env.compile("has(user.email)").unwrap();
41 let program = env.program(&ast).unwrap();
42 let result = program.eval(&activation);
43 println!("has(user.email): {}", result);
44
45 // Combine conditions
46 let ast = env.compile(r#"user.active && user.age >= 21"#).unwrap();
47 let program = env.program(&ast).unwrap();
48 let result = program.eval(&activation);
49 println!("active && age >= 21: {}", result);
50}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}7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("x", CelType::Int)
10 .with_variable("items", CelType::list(CelType::Int));
11
12 let mut activation = MapActivation::new();
13
14 // Division by zero returns an error value
15 println!("=== Division by zero ===");
16 activation.insert("x", 0);
17 let ast = env.compile("10 / x").unwrap();
18 let program = env.program(&ast).unwrap();
19 let result = program.eval(&activation);
20
21 match &result {
22 Value::Error(err) => println!("Error: {}", err),
23 other => println!("Result: {}", other),
24 }
25
26 // Index out of bounds
27 println!("\n=== Index out of bounds ===");
28 activation.insert("items", Value::list([1, 2, 3]));
29 let ast = env.compile("items[10]").unwrap();
30 let program = env.program(&ast).unwrap();
31 let result = program.eval(&activation);
32
33 match &result {
34 Value::Error(err) => println!("Error: {}", err),
35 other => println!("Result: {}", other),
36 }
37
38 // Key not found in map
39 println!("\n=== Key not found ===");
40 let env = Env::with_standard_library()
41 .with_variable("config", CelType::map(CelType::String, CelType::String));
42 let mut activation = MapActivation::new();
43 activation.insert("config", Value::map([("host", "localhost")]));
44
45 let ast = env.compile("config.missing_key").unwrap();
46 let program = env.program(&ast).unwrap();
47 let result = program.eval(&activation);
48
49 match &result {
50 Value::Error(err) => println!("Error: {}", err),
51 other => println!("Result: {}", other),
52 }
53
54 // Use has() to safely check field existence
55 println!("\n=== Safe field access with has() ===");
56 let ast = env
57 .compile("has(config.missing_key) ? config.missing_key : 'default'")
58 .unwrap();
59 let program = env.program(&ast).unwrap();
60 let result = program.eval(&activation);
61 println!("Result: {}", result);
62}Sourcepub fn parse_only(&self, source: &str) -> Result<Ast, CompileError>
pub fn parse_only(&self, source: &str) -> Result<Ast, CompileError>
Parse a CEL expression without type-checking, returning an unchecked Ast.
This is useful when you want to parse an expression but don’t need type information, or when you want to defer type-checking.
§Example
use cel_core::Env;
let env = Env::with_standard_library();
let ast = env.parse_only("1 + 2").unwrap();
assert!(!ast.is_checked());
assert_eq!(ast.to_cel_string(), "1 + 2");Sourcepub fn program(&self, ast: &Ast) -> Result<Program, CompileError>
pub fn program(&self, ast: &Ast) -> Result<Program, CompileError>
Create a program from a compiled AST.
The program contains the AST and a function registry with implementations for all functions registered in this environment.
§Example
use cel_core::{Env, CelType};
use cel_core::{Value, MapActivation, Activation};
let env = Env::with_standard_library()
.with_variable("x", CelType::Int);
let ast = env.compile("x + 1").unwrap();
let program = env.program(&ast).unwrap();
let mut activation = MapActivation::new();
activation.insert("x", Value::Int(41));
let result = program.eval(&activation);
assert_eq!(result, Value::Int(42));Examples found in repository?
7fn main() {
8 let env = Env::with_standard_library().with_variable("name", CelType::String);
9
10 let ast = env.compile(r#""Hello, " + name + "!""#).unwrap();
11 let program = env.program(&ast).unwrap();
12
13 let mut activation = MapActivation::new();
14 activation.insert("name", "World");
15
16 let result = program.eval(&activation);
17 println!("{}", result);
18}More examples
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}7fn main() {
8 let env = Env::with_standard_library().with_variable("numbers", CelType::list(CelType::Int));
9
10 let mut activation = MapActivation::new();
11 activation.insert("numbers", Value::list([1, 5, 3, 8, 2]));
12
13 // Filter: keep only values > 3
14 let ast = env.compile("numbers.filter(x, x > 3)").unwrap();
15 let program = env.program(&ast).unwrap();
16 let result = program.eval(&activation);
17 println!("filter(x, x > 3): {}", result);
18
19 // Map: double each value
20 let ast = env.compile("numbers.map(x, x * 2)").unwrap();
21 let program = env.program(&ast).unwrap();
22 let result = program.eval(&activation);
23 println!("map(x, x * 2): {}", result);
24
25 // Exists: any value > 7?
26 let ast = env.compile("numbers.exists(x, x > 7)").unwrap();
27 let program = env.program(&ast).unwrap();
28 let result = program.eval(&activation);
29 println!("exists(x, x > 7): {}", result);
30
31 // All: all values > 0?
32 let ast = env.compile("numbers.all(x, x > 0)").unwrap();
33 let program = env.program(&ast).unwrap();
34 let result = program.eval(&activation);
35 println!("all(x, x > 0): {}", result);
36
37 // Size
38 let ast = env.compile("numbers.size()").unwrap();
39 let program = env.program(&ast).unwrap();
40 let result = program.eval(&activation);
41 println!("size(): {}", result);
42
43 // Contains (using 'in' operator)
44 let ast = env.compile("5 in numbers").unwrap();
45 let program = env.program(&ast).unwrap();
46 let result = program.eval(&activation);
47 println!("5 in numbers: {}", result);
48}7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("user", CelType::map(CelType::String, CelType::Dyn));
10
11 // Mixed value types require explicit Value::from()
12 let user = Value::map([
13 ("name", Value::from("Alice")),
14 ("age", Value::from(30)), // i32 automatically widens to i64
15 ("active", Value::from(true)),
16 ]);
17
18 let mut activation = MapActivation::new();
19 activation.insert("user", user);
20
21 // Field access
22 let ast = env.compile("user.name").unwrap();
23 let program = env.program(&ast).unwrap();
24 let result = program.eval(&activation);
25 println!("user.name: {}", result);
26
27 // Index access
28 let ast = env.compile(r#"user["age"]"#).unwrap();
29 let program = env.program(&ast).unwrap();
30 let result = program.eval(&activation);
31 println!(r#"user["age"]: {}"#, result);
32
33 // Check field existence with 'in'
34 let ast = env.compile(r#""name" in user"#).unwrap();
35 let program = env.program(&ast).unwrap();
36 let result = program.eval(&activation);
37 println!(r#""name" in user: {}"#, result);
38
39 // Check field existence with has()
40 let ast = env.compile("has(user.email)").unwrap();
41 let program = env.program(&ast).unwrap();
42 let result = program.eval(&activation);
43 println!("has(user.email): {}", result);
44
45 // Combine conditions
46 let ast = env.compile(r#"user.active && user.age >= 21"#).unwrap();
47 let program = env.program(&ast).unwrap();
48 let result = program.eval(&activation);
49 println!("active && age >= 21: {}", result);
50}7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("x", CelType::Int)
10 .with_variable("items", CelType::list(CelType::Int));
11
12 let mut activation = MapActivation::new();
13
14 // Division by zero returns an error value
15 println!("=== Division by zero ===");
16 activation.insert("x", 0);
17 let ast = env.compile("10 / x").unwrap();
18 let program = env.program(&ast).unwrap();
19 let result = program.eval(&activation);
20
21 match &result {
22 Value::Error(err) => println!("Error: {}", err),
23 other => println!("Result: {}", other),
24 }
25
26 // Index out of bounds
27 println!("\n=== Index out of bounds ===");
28 activation.insert("items", Value::list([1, 2, 3]));
29 let ast = env.compile("items[10]").unwrap();
30 let program = env.program(&ast).unwrap();
31 let result = program.eval(&activation);
32
33 match &result {
34 Value::Error(err) => println!("Error: {}", err),
35 other => println!("Result: {}", other),
36 }
37
38 // Key not found in map
39 println!("\n=== Key not found ===");
40 let env = Env::with_standard_library()
41 .with_variable("config", CelType::map(CelType::String, CelType::String));
42 let mut activation = MapActivation::new();
43 activation.insert("config", Value::map([("host", "localhost")]));
44
45 let ast = env.compile("config.missing_key").unwrap();
46 let program = env.program(&ast).unwrap();
47 let result = program.eval(&activation);
48
49 match &result {
50 Value::Error(err) => println!("Error: {}", err),
51 other => println!("Result: {}", other),
52 }
53
54 // Use has() to safely check field existence
55 println!("\n=== Safe field access with has() ===");
56 let ast = env
57 .compile("has(config.missing_key) ? config.missing_key : 'default'")
58 .unwrap();
59 let program = env.program(&ast).unwrap();
60 let result = program.eval(&activation);
61 println!("Result: {}", result);
62}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}