pub struct Ast { /* private fields */ }Expand description
Unified AST representation matching cel-go’s Ast type.
An Ast can be either checked (has type info) or unchecked (parsed only).
This follows the cel-go pattern where a single type represents both states.
§Creating an Ast
- Use
Env::compile()to get a checkedAst - Use
Env::parse_only()to get an uncheckedAst
§Proto Conversion
For proto wire format conversion, use the cel-core-proto crate directly.
Implementations§
Source§impl Ast
impl Ast
Sourcepub fn new_unchecked(expr: SpannedExpr, source: impl Into<Arc<str>>) -> Self
pub fn new_unchecked(expr: SpannedExpr, source: impl Into<Arc<str>>) -> Self
Create an unchecked AST (after parsing, before type checking).
Sourcepub fn new_checked(
expr: SpannedExpr,
source: impl Into<Arc<str>>,
check_result: CheckResult,
) -> Self
pub fn new_checked( expr: SpannedExpr, source: impl Into<Arc<str>>, check_result: CheckResult, ) -> Self
Create a checked AST (after type checking).
Sourcepub fn is_checked(&self) -> bool
pub fn is_checked(&self) -> bool
Returns true if this AST has been type-checked.
Sourcepub fn expr(&self) -> &SpannedExpr
pub fn expr(&self) -> &SpannedExpr
Get the expression tree.
Sourcepub fn type_info(&self) -> Option<&CheckResult>
pub fn type_info(&self) -> Option<&CheckResult>
Get type checking results (if checked).
Sourcepub fn result_type(&self) -> Option<&CelType>
pub fn result_type(&self) -> Option<&CelType>
Get the result type of the expression (if checked).
Returns the type of the root expression from the type map.
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 to_cel_string(&self) -> String
pub fn to_cel_string(&self) -> String
Convert the AST back to CEL source text.
The output is a valid CEL expression that is semantically equivalent to the original. Formatting may differ (whitespace, parenthesization).
§Example
use cel_core::Env;
let env = Env::with_standard_library();
let ast = env.compile("1 + 2 * 3").unwrap();
assert_eq!(ast.to_cel_string(), "1 + 2 * 3");