pub struct Expr { /* private fields */ }Expand description
A CEL expression node in the abstract syntax tree.
Expr represents a single node in a CEL expression tree during macro expansion.
Each expression has a unique ID for source location tracking and an optional kind
that determines what type of expression it is (constant, function call, etc.).
§Structure
An expression consists of:
id: A unique identifier for source position tracking and error reportingkind: The specific type and content of the expression (orNonefor unspecified)
§Expression Kinds
The kind field determines the expression type:
Constant: Literal values (numbers, strings, booleans, etc.)IdentExpr: Variable or identifier referencesSelectExpr: Field selection (e.g.,obj.field)CallExpr: Function or method callsListExpr: List literals (e.g.,[1, 2, 3])StructExpr: Struct literals (e.g.,Person{name: "Alice"})MapExpr: Map literals (e.g.,{"key": "value"})ComprehensionExpr: List/map comprehensions
§Examples
// Access expression properties
let id = expr.id();
// Check expression kind
if let Some(kind) = expr.kind() {
match kind {
ExprKind::Constant(c) => println!("Constant: {:?}", c),
ExprKind::Ident(i) => println!("Identifier: {}", i.name),
ExprKind::Call(c) => println!("Function: {}", c.function),
_ => {}
}
}Implementations§
Source§impl Expr
impl Expr
Sourcepub fn id(&self) -> i64
pub fn id(&self) -> i64
Returns the unique identifier of this expression.
Expression IDs are used for source location tracking and correlating expressions with their positions in the original source code.
Sourcepub fn set_id(&mut self, id: i64)
pub fn set_id(&mut self, id: i64)
Sets the unique identifier of this expression.
This is typically handled by expression factory methods and rarely needs to be called directly in user code.
Sourcepub fn kind(&self) -> Option<&ExprKind>
pub fn kind(&self) -> Option<&ExprKind>
Returns a reference to the expression kind, if set.
Returns None for unspecified expressions (expressions without a kind set).
§Examples
if let Some(kind) = expr.kind() {
if kind.is_constant() {
println!("This is a constant expression");
}
}Sourcepub fn set_kind(&mut self, kind: ExprKind)
pub fn set_kind(&mut self, kind: ExprKind)
Sets the expression kind.
This replaces any existing kind with the new one. Use this to construct or modify expressions during macro expansion.
Sourcepub fn clear_kind(&mut self)
pub fn clear_kind(&mut self)
Clears the expression kind, making this an unspecified expression.
This is rarely needed in practice; most expressions should have a kind.
Methods from Deref<Target = ExprKind>§
Sourcepub fn is_constant(&self) -> bool
pub fn is_constant(&self) -> bool
Returns true if this is a constant expression.
Sourcepub fn is_comprehension(&self) -> bool
pub fn is_comprehension(&self) -> bool
Returns true if this is a comprehension expression.
Sourcepub fn as_constant(&self) -> Option<&Constant>
pub fn as_constant(&self) -> Option<&Constant>
Returns a reference to the constant value if this is a constant expression.
Sourcepub fn as_ident(&self) -> Option<&IdentExpr>
pub fn as_ident(&self) -> Option<&IdentExpr>
Returns a reference to the identifier if this is an identifier expression.
Sourcepub fn as_select(&self) -> Option<&SelectExpr>
pub fn as_select(&self) -> Option<&SelectExpr>
Returns a reference to the select expression if this is a select expression.
Sourcepub fn as_call(&self) -> Option<&CallExpr>
pub fn as_call(&self) -> Option<&CallExpr>
Returns a reference to the call expression if this is a call expression.
Sourcepub fn as_list(&self) -> Option<&ListExpr>
pub fn as_list(&self) -> Option<&ListExpr>
Returns a reference to the list expression if this is a list expression.
Sourcepub fn as_struct(&self) -> Option<&StructExpr>
pub fn as_struct(&self) -> Option<&StructExpr>
Returns a reference to the struct expression if this is a struct expression.
Sourcepub fn as_map(&self) -> Option<&MapExpr>
pub fn as_map(&self) -> Option<&MapExpr>
Returns a reference to the map expression if this is a map expression.
Sourcepub fn as_comprehension(&self) -> Option<&ComprehensionExpr>
pub fn as_comprehension(&self) -> Option<&ComprehensionExpr>
Returns a reference to the comprehension expression if this is a comprehension expression.
Sourcepub fn as_constant_mut(&mut self) -> Option<&mut Constant>
pub fn as_constant_mut(&mut self) -> Option<&mut Constant>
Returns a mutable reference to the constant value if this is a constant expression.
Sourcepub fn as_ident_mut(&mut self) -> Option<&mut IdentExpr>
pub fn as_ident_mut(&mut self) -> Option<&mut IdentExpr>
Returns a mutable reference to the identifier if this is an identifier expression.
Sourcepub fn as_select_mut(&mut self) -> Option<&mut SelectExpr>
pub fn as_select_mut(&mut self) -> Option<&mut SelectExpr>
Returns a mutable reference to the select expression if this is a select expression.
Sourcepub fn as_call_mut(&mut self) -> Option<&mut CallExpr>
pub fn as_call_mut(&mut self) -> Option<&mut CallExpr>
Returns a mutable reference to the call expression if this is a call expression.
Sourcepub fn as_list_mut(&mut self) -> Option<&mut ListExpr>
pub fn as_list_mut(&mut self) -> Option<&mut ListExpr>
Returns a mutable reference to the list expression if this is a list expression.
Sourcepub fn as_struct_mut(&mut self) -> Option<&mut StructExpr>
pub fn as_struct_mut(&mut self) -> Option<&mut StructExpr>
Returns a mutable reference to the struct expression if this is a struct expression.
Sourcepub fn as_map_mut(&mut self) -> Option<&mut MapExpr>
pub fn as_map_mut(&mut self) -> Option<&mut MapExpr>
Returns a mutable reference to the map expression if this is a map expression.
Sourcepub fn as_comprehension_mut(&mut self) -> Option<&mut ComprehensionExpr>
pub fn as_comprehension_mut(&mut self) -> Option<&mut ComprehensionExpr>
Returns a mutable reference to the comprehension expression if this is a comprehension expression.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Expr
impl RefUnwindSafe for Expr
impl Send for Expr
impl Sync for Expr
impl Unpin for Expr
impl UnwindSafe for Expr
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more