Module macros

Module macros 

Source
Expand description

CEL macro system for compile-time expression expansion. CEL macro system for extending the expression language.

This module provides functionality for defining and registering custom macros that can expand CEL expressions at compile time. Macros enable syntactic sugar and domain-specific language extensions without modifying the core parser.

§Overview

CEL macros transform parsed expressions into new expression trees before type checking and evaluation. This allows you to:

  • Add new syntactic constructs (e.g., has(foo.bar) for presence testing)
  • Implement domain-specific operators (e.g., all(list, predicate))
  • Optimize common patterns by rewriting expressions
  • Extend CEL with custom comprehension forms

§Macro Types

There are two types of macros:

  • Global macros: Called as functions, e.g., all([1, 2, 3], x, x > 0)
  • Receiver macros: Called as methods, e.g., list.filter(x, x > 0)

Both types can accept fixed or variable numbers of arguments.

§Examples

§Defining a global macro

use cel_cxx::macros::{Macro, MacroExprFactory, Expr};

// Macro that creates a constant expression with doubled value
let double_macro = Macro::new_global("double", 1, |factory, args| {
    if let Some(expr) = args.first() {
        if let Some(val) = expr.kind().and_then(|k| k.as_constant()) {
            if let Some(int_val) = val.as_int() {
                return Some(factory.new_const(int_val * 2));
            }
        }
    }
    None
});

§Defining a receiver macro

// Macro for optional element access: list.maybe_get(index)
let maybe_get_macro = Macro::new_receiver("maybe_get", 1, |factory, target, args| {
    // Implementation would handle optional list indexing
    Some(factory.new_call("_?.[]", &[target, args[0].clone()]))
});

Structs§

CallExpr
A function or method call expression.
ComprehensionExpr
A comprehension expression for list/map processing.
Expr
A CEL expression node in the abstract syntax tree.
IdentExpr
An identifier expression referencing a variable or constant.
ListExpr
A list literal expression.
ListExprElement
An element in a list expression.
Macro
A CEL macro that expands expressions at compile time.
MacroExprFactory
Factory for constructing CEL expressions during macro expansion.
MapExpr
A map literal expression.
MapExprEntry
An entry in a map expression.
SelectExpr
A field selection expression.
StructExpr
A struct literal expression.
StructExprField
A field in a struct expression.

Enums§

ExprKind
The kind/type of a CEL expression.

Traits§

GlobalMacroExpander
Trait for global macro expansion functions.
ReceiverMacroExpander
Trait for receiver macro expansion functions.