Skip to main content

Ast

Struct Ast 

Source
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 checked Ast
  • Use Env::parse_only() to get an unchecked Ast

§Proto Conversion

For proto wire format conversion, use the cel-core-proto crate directly.

Implementations§

Source§

impl Ast

Source

pub fn new_unchecked(expr: SpannedExpr, source: impl Into<Arc<str>>) -> Self

Create an unchecked AST (after parsing, before type checking).

Source

pub fn new_checked( expr: SpannedExpr, source: impl Into<Arc<str>>, check_result: CheckResult, ) -> Self

Create a checked AST (after type checking).

Source

pub fn is_checked(&self) -> bool

Returns true if this AST has been type-checked.

Source

pub fn expr(&self) -> &SpannedExpr

Get the expression tree.

Source

pub fn source(&self) -> &str

Get the original source text.

Source

pub fn type_info(&self) -> Option<&CheckResult>

Get type checking results (if checked).

Source

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?
examples/extensions.rs (line 20)
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}
Source

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");

Trait Implementations§

Source§

impl Clone for Ast

Source§

fn clone(&self) -> Ast

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ast

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Ast

§

impl RefUnwindSafe for Ast

§

impl Send for Ast

§

impl Sync for Ast

§

impl Unpin for Ast

§

impl UnwindSafe for Ast

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.