Struct VariableRegistry

Source
pub struct VariableRegistry { /* private fields */ }
Expand description

Compile-time variable registry.

VariableRegistry manages variable declarations and constant definitions during the CEL environment compilation phase. It maintains a mapping from variable names to variable entries, where each entry can be either:

  • A constant value (Constant): A fixed value known at compile time
  • A variable declaration: A type declaration with value provided at runtime

§Examples

use cel_cxx::{ValueType, VariableRegistry};

let mut registry = VariableRegistry::new();

// Define constants
registry.define_constant("PI", 3.14159)?;
registry.define_constant("APP_NAME", "MyApp")?;

// Declare variables
registry.declare::<String>("user_input")?;

assert_eq!(registry.len(), 3);

Implementations§

Source§

impl VariableRegistry

Source

pub fn new() -> Self

Creates a new empty variable registry.

§Returns

A new empty VariableRegistry

Source

pub fn define_constant<T>( &mut self, name: impl Into<String>, value: T, ) -> Result<&mut Self, Error>
where T: IntoConstant,

Defines a constant value.

Constants are values that are known at compile time and don’t change during evaluation. They can be used directly in CEL expressions without requiring runtime bindings.

§Type Parameters
§Parameters
  • name: The constant name
  • value: The constant value
§Returns

Returns &mut Self to support method chaining, or Error if an error occurs

§Examples
use cel_cxx::VariableRegistry;

let mut registry = VariableRegistry::new();
registry
    .define_constant("PI", 3.14159)?
    .define_constant("APP_NAME", "MyApp")?
    .define_constant("MAX_USERS", 1000i64)?;
Source

pub fn declare<T>( &mut self, name: impl Into<String>, ) -> Result<&mut Self, Error>
where T: TypedValue,

Declares a variable with a specific type.

Variable declarations only specify the type, with actual values provided at runtime through Activation. The type is determined by the generic parameter T which must implement TypedValue.

§Type Parameters
§Parameters
  • name: The variable name
§Returns

Returns &mut Self to support method chaining, or Error if an error occurs

§Examples
use cel_cxx::VariableRegistry;

let mut registry = VariableRegistry::new();
registry
    .declare::<String>("user_name")?
    .declare::<i64>("user_id")?
    .declare::<bool>("is_admin")?;
Source

pub fn entries( &self, ) -> impl Iterator<Item = (&String, &VariableDeclOrConstant)>

Returns an iterator over all variable entries.

The iterator yields (name, entry) pairs for all registered variables and constants.

§Returns

Iterator yielding (&String, &VariableDeclOrConstant) pairs

Source

pub fn entries_mut( &mut self, ) -> impl Iterator<Item = (&String, &mut VariableDeclOrConstant)>

Returns a mutable iterator over all variable entries.

The iterator yields (name, entry) pairs and allows modifying the entries.

§Returns

Iterator yielding (&String, &mut VariableDeclOrConstant) pairs

Source

pub fn find(&self, name: &str) -> Option<&VariableDeclOrConstant>

Finds a variable entry by name.

§Parameters
  • name: The variable name to search for
§Returns

Returns Some(&VariableDeclOrConstant) if found, None otherwise

Source

pub fn find_mut(&mut self, name: &str) -> Option<&mut VariableDeclOrConstant>

Finds a mutable variable entry by name.

§Parameters
  • name: The variable name to search for
§Returns

Returns Some(&mut VariableDeclOrConstant) if found, None otherwise

Source

pub fn remove(&mut self, name: &str) -> Option<VariableDeclOrConstant>

Removes a variable entry by name.

§Parameters
  • name: The variable name to remove
§Returns

Returns Some(VariableDeclOrConstant) if the entry was found and removed, None otherwise

Source

pub fn clear(&mut self)

Clears all variable entries.

Source

pub fn len(&self) -> usize

Returns the number of variable entries.

§Returns

Number of registered variables and constants

Source

pub fn is_empty(&self) -> bool

Returns whether the registry is empty.

§Returns

true if no variables or constants are registered, false otherwise

Trait Implementations§

Source§

impl Debug for VariableRegistry

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for VariableRegistry

Source§

fn default() -> VariableRegistry

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more