Skip to main content

VariableBindings

Struct VariableBindings 

Source
pub struct VariableBindings<'f> { /* private fields */ }
Expand description

Runtime variable bindings.

VariableBindings manages variable bindings during CEL expression evaluation. It maps variable names to concrete values or value providers.

Unlike compile-time VariableRegistry, VariableBindings provides actual variable values at runtime and supports two types of bindings:

  • Value bindings: Directly stored variable values
  • Provider bindings: Function-computed variable values through lazy evaluation

§Lifetime Parameters

  • 'f: Lifetime of function providers, allowing closures valid within specific scopes

§Examples

use cel_cxx::VariableBindings;

let mut bindings = VariableBindings::new();

// Bind static values
bindings.bind("name", "Alice")?;
bindings.bind("age", 30i64)?;

// Bind dynamic value providers
bindings.bind_provider("current_time", || {
    std::time::SystemTime::now()
})?;

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

Implementations§

Source§

impl<'f> VariableBindings<'f>

Source

pub fn new() -> Self

Creates a new empty variable bindings.

Source§

impl<'f> VariableBindings<'f>

Source

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

Binds a variable value.

Binds a variable name to a concrete value. The value must implement IntoValue and TypedValue traits.

§Type Parameters
  • T: Value type, must implement IntoValue + TypedValue
§Parameters
  • name: Variable name
  • value: Variable value
§Returns

Returns &mut Self to support method chaining

§Examples
use cel_cxx::VariableBindings;

let mut bindings = VariableBindings::new();
bindings
    .bind("user_id", 123i64)?
    .bind("is_admin", true)?
    .bind("username", "alice")?;
Source

pub fn bind_with_value( &mut self, name: impl Into<String>, value_type: ValueType, value: Value, ) -> Result<&mut Self, Error>

Binds a variable with an explicit type and pre-converted value.

Unlike bind which infers the type from the value, this method takes the type and value separately. This is useful for types like protobuf messages where the concrete type name is not known at compile time.

Source

pub fn bind_provider<F, Fm>( &mut self, name: impl Into<String>, provider: F, ) -> Result<&mut Self, Error>
where F: IntoFunction<'f, Fm>, Fm: FnMarker,

Binds a variable to a value provider.

Binds a variable name to a provider function that computes the value dynamically. This enables lazy evaluation and allows variables to have values computed at runtime.

§Type Parameters
  • F: Provider function type, must implement IntoFunction
  • Fm: Function marker type (sync/async)
§Parameters
  • name: Variable name
  • provider: Provider function that computes the variable value
§Returns

Returns &mut Self to support method chaining

§Examples
use cel_cxx::VariableBindings;

let mut bindings = VariableBindings::new();
bindings.bind_provider("current_time", || -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs() as i64
})?;
Source

pub fn find(&self, name: &str) -> Option<&VariableBinding<'f>>

Finds a variable binding by name.

§Parameters
  • name: Variable name
§Returns

Returns Some(&VariableBinding) if found, None otherwise

Source

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

Finds a mutable variable binding by name.

§Parameters
  • name: Variable name
§Returns

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

Source

pub fn entries(&self) -> impl Iterator<Item = (&str, &VariableBinding<'f>)>

Returns an iterator over all variable bindings.

The iterator yields (name, binding) pairs for all bound variables.

§Returns

Iterator yielding (&str, &VariableBinding) pairs

Source

pub fn entries_mut( &mut self, ) -> impl Iterator<Item = (&str, &mut VariableBinding<'f>)>

Returns a mutable iterator over all variable bindings.

The iterator yields (name, binding) pairs and allows modifying the bindings.

§Returns

Iterator yielding (&str, &mut VariableBinding) pairs

Source

pub fn remove(&mut self, name: &str) -> Result<(), Error>

Removes a variable binding by name.

§Parameters
  • name: Variable name to remove
§Returns

Returns Ok(()) if successfully removed, or an error if the variable doesn’t exist

Source

pub fn clear(&mut self)

Clears all variable bindings.

Source

pub fn len(&self) -> usize

Returns the number of variable bindings.

§Returns

Number of bound variables

Source

pub fn is_empty(&self) -> bool

Returns whether the bindings are empty.

§Returns

true if no variables are bound, false otherwise

Trait Implementations§

Source§

impl<'f> Debug for VariableBindings<'f>

Source§

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

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

impl<'f> Default for VariableBindings<'f>

Source§

fn default() -> VariableBindings<'f>

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

Auto Trait Implementations§

§

impl<'f> !RefUnwindSafe for VariableBindings<'f>

§

impl<'f> !UnwindSafe for VariableBindings<'f>

§

impl<'f> Freeze for VariableBindings<'f>

§

impl<'f> Send for VariableBindings<'f>

§

impl<'f> Sync for VariableBindings<'f>

§

impl<'f> Unpin for VariableBindings<'f>

§

impl<'f> UnsafeUnpin for VariableBindings<'f>

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, 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.