Struct Activation

Source
pub struct Activation<'f, Fm: FnMarker = ()> { /* private fields */ }
Expand description

Activation context for CEL expression evaluation.

An Activation provides variable and function bindings that are used during the evaluation of a CEL expression. It allows you to bind runtime values to variables and functions that were declared in the environment.

§Type Parameters

  • 'f - Lifetime of the functions in the activation
  • Fm - Function marker type indicating sync/async function support

§Examples

§Basic Variable Binding

use cel_cxx::*;

let activation = Activation::new()
    .bind_variable("name", "Alice")
    .unwrap()
    .bind_variable("age", 30i64)
    .unwrap();

§Function Binding

use cel_cxx::*;

let activation = Activation::new()
    .bind_global_function("custom_fn", |x: i64| -> i64 { x * 2 })
    .unwrap();

§Empty Activation

For expressions that don’t need any bindings, you can use ():

use cel_cxx::*;

let env = Env::builder().build().unwrap();
let program = env.compile("1 + 2").unwrap();
let result = program.evaluate(()).unwrap();

Implementations§

Source§

impl<'f> Activation<'f, ()>

Source

pub fn new() -> Self

Creates a new empty activation.

This creates an activation with no variable or function bindings. You can then use the builder methods to add bindings.

§Examples
use cel_cxx::*;

let activation = Activation::new();
Source

pub fn force_async(self) -> Activation<'f, Async>

Available on crate feature async only.

Force the activation to be async.

This method is only available when the async feature is enabled. It converts the activation to an AsyncActivation.

§Examples
use cel_cxx::*;

let activation = Activation::new().force_async();
Source§

impl<'f> Activation<'f, Async>

Source

pub fn new_async() -> Self

Available on crate feature async only.

Creates a new empty activation with async support.

This creates an activation with no variable or function bindings. You can then use the builder methods to add bindings.

§Examples
use cel_cxx::*;

let activation = Activation::new_async();
Source§

impl<'f, Fm: FnMarker> Activation<'f, Fm>

Source

pub fn bind_variable<S, T>(self, name: S, value: T) -> Result<Self, Error>
where S: Into<String>, T: IntoValue + TypedValue,

Binds a variable to a value.

This method adds a variable binding to the activation. The variable name must match a variable declared in the environment, and the value must be compatible with the declared type.

§Arguments
  • name - The name of the variable to bind
  • value - The value to bind to the variable
§Type Parameters
  • S - The type of the variable name (must convert to String)
  • T - The type of the value (must implement IntoValue and TypedValue)
§Returns

Returns a Result containing the updated activation or an error if the binding failed.

§Examples
use cel_cxx::*;

let activation = Activation::new()
    .bind_variable("name", "World")
    .unwrap()
    .bind_variable("count", 42i64)
    .unwrap();
§Errors

Returns an error if:

  • The variable name is not declared in the environment
  • The value type doesn’t match the declared variable type
  • The value cannot be converted to a CEL value
Source

pub fn bind_variable_provider<S, F, Ffm>( self, name: S, provider: F, ) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
where S: Into<String>, F: IntoFunction<'f, Ffm>, Ffm: FnMarkerAggr<Fm>,

Binds a variable to a value provider.

This method allows you to bind a variable to a provider function that will be called to get the value when the variable is accessed during evaluation. This can be useful for lazy evaluation or for variables whose values are expensive to compute.

§Arguments
  • name - The name of the variable to bind
  • provider - The provider function that will supply the value
§Type Parameters
  • S - The type of the variable name (must convert to String)
  • F - The provider function type (must implement IntoFunction)
  • Ffm - The function marker type (sync/async)
§Returns

Returns a Result containing the updated activation with the appropriate function marker type, or an error if the binding failed.

§Examples
use cel_cxx::*;

let activation = Activation::new()
    .bind_variable_provider("timestamp", || -> i64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64
    })
    .unwrap();
Source

pub fn bind_function<S, F, Ffm, Args>( self, name: S, member: bool, f: F, ) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
where S: Into<String>, F: IntoFunction<'f, Ffm, Args>, Ffm: FnMarkerAggr<Fm>, Args: Arguments,

Binds a function (either global or member).

This method allows you to bind a function implementation that can be called during expression evaluation. The function can be either a global function or a member function.

§Arguments
  • name - The name of the function to bind
  • member - Whether this is a member function (true) or global function (false)
  • f - The function implementation
§Type Parameters
  • S - The type of the function name (must convert to String)
  • F - The function implementation type (must implement IntoFunction)
  • Ffm - The function marker type (sync/async)
§Returns

Returns a Result containing the updated activation with the appropriate function marker type, or an error if the binding failed.

§Examples
use cel_cxx::*;

// Bind a global function
let activation = Activation::new()
    .bind_function("double", false, |x: i64| -> i64 { x * 2 })
    .unwrap();

// Bind a member function
let activation = Activation::new()
    .bind_function("to_upper", true, |s: String| -> String { s.to_uppercase() })
    .unwrap();
Source

pub fn bind_member_function<S, F, Ffm, Args>( self, name: S, f: F, ) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
where S: Into<String>, F: IntoFunction<'f, Ffm, Args>, Ffm: FnMarkerAggr<Fm>, Args: Arguments,

Binds a member function.

This is a convenience method for binding member functions (functions that are called as methods on values, like value.method()).

§Arguments
  • name - The name of the function to bind
  • f - The function implementation
§Type Parameters
  • S - The type of the function name (must convert to String)
  • F - The function implementation type (must implement IntoFunction)
  • Ffm - The function marker type (sync/async)
§Returns

Returns a Result containing the updated activation with the appropriate function marker type, or an error if the binding failed.

§Examples
use cel_cxx::*;

let activation = Activation::new()
    .bind_member_function("to_upper", |s: String| -> String { s.to_uppercase() })
    .unwrap();
Source

pub fn bind_global_function<S, F, Ffm, Args>( self, name: S, f: F, ) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
where S: Into<String>, F: IntoFunction<'f, Ffm, Args>, Ffm: FnMarkerAggr<Fm>, Args: Arguments,

Binds a global function.

This is a convenience method for binding global functions (functions that can be called from any context).

§Arguments
  • name - The name of the function to bind
  • f - The function implementation
§Type Parameters
  • S - The type of the function name (must convert to String)
  • F - The function implementation type (must implement IntoFunction)
  • Ffm - The function marker type (sync/async)
§Returns

Returns a Result containing the updated activation with the appropriate function marker type, or an error if the binding failed.

§Examples
use cel_cxx::*;

let activation = Activation::new()
    .bind_global_function("double", |x: i64| -> i64 { x * 2 })
    .unwrap();

Trait Implementations§

Source§

impl<'f, Fm: FnMarker> ActivationInterface<'f, Fm> for &Activation<'f, Fm>

Source§

fn variables(&self) -> &VariableBindings<'f>

Returns a reference to the variable bindings. Read more
Source§

fn functions(&self) -> &FunctionBindings<'f>

Returns a reference to the function bindings. Read more
Source§

impl<'f, Fm: FnMarker> ActivationInterface<'f, Fm> for Activation<'f, Fm>

Source§

fn variables(&self) -> &VariableBindings<'f>

Returns a reference to the variable bindings. Read more
Source§

fn functions(&self) -> &FunctionBindings<'f>

Returns a reference to the function bindings. Read more
Source§

impl<'f, Fm: FnMarker> ActivationInterface<'f, Fm> for Box<Activation<'f, Fm>>

Source§

fn variables(&self) -> &VariableBindings<'f>

Returns a reference to the variable bindings. Read more
Source§

fn functions(&self) -> &FunctionBindings<'f>

Returns a reference to the function bindings. Read more
Source§

impl<'f, Fm: FnMarker> Debug for Activation<'f, Fm>

Source§

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

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

impl<'f> Default for Activation<'f, ()>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'f, Fm> Freeze for Activation<'f, Fm>

§

impl<'f, Fm = ()> !RefUnwindSafe for Activation<'f, Fm>

§

impl<'f, Fm> Send for Activation<'f, Fm>
where Fm: Send,

§

impl<'f, Fm> Sync for Activation<'f, Fm>
where Fm: Sync,

§

impl<'f, Fm> Unpin for Activation<'f, Fm>
where Fm: Unpin,

§

impl<'f, Fm = ()> !UnwindSafe for Activation<'f, Fm>

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