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 activationFm
- 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, ()>
impl<'f> Activation<'f, ()>
Sourcepub fn new() -> Self
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();
Sourcepub fn force_async(self) -> Activation<'f, Async>
Available on crate feature async
only.
pub fn force_async(self) -> Activation<'f, Async>
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>
impl<'f> Activation<'f, Async>
Sourcepub fn new_async() -> Self
Available on crate feature async
only.
pub fn new_async() -> Self
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>
impl<'f, Fm: FnMarker> Activation<'f, Fm>
Sourcepub fn bind_variable<S, T>(self, name: S, value: T) -> Result<Self, Error>
pub fn bind_variable<S, T>(self, name: S, value: T) -> Result<Self, Error>
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 bindvalue
- The value to bind to the variable
§Type Parameters
S
- The type of the variable name (must convert toString
)T
- The type of the value (must implementIntoValue
andTypedValue
)
§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
Sourcepub fn bind_variable_provider<S, F, Ffm>(
self,
name: S,
provider: F,
) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
pub fn bind_variable_provider<S, F, Ffm>( self, name: S, provider: F, ) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
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 bindprovider
- The provider function that will supply the value
§Type Parameters
S
- The type of the variable name (must convert toString
)F
- The provider function type (must implementIntoFunction
)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();
Sourcepub fn bind_function<S, F, Ffm, Args>(
self,
name: S,
member: bool,
f: F,
) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
pub fn bind_function<S, F, Ffm, Args>( self, name: S, member: bool, f: F, ) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
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 bindmember
- 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 toString
)F
- The function implementation type (must implementIntoFunction
)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();
Sourcepub fn bind_member_function<S, F, Ffm, Args>(
self,
name: S,
f: F,
) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
pub fn bind_member_function<S, F, Ffm, Args>( self, name: S, f: F, ) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
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 bindf
- The function implementation
§Type Parameters
S
- The type of the function name (must convert toString
)F
- The function implementation type (must implementIntoFunction
)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();
Sourcepub fn bind_global_function<S, F, Ffm, Args>(
self,
name: S,
f: F,
) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
pub fn bind_global_function<S, F, Ffm, Args>( self, name: S, f: F, ) -> Result<Activation<'f, <Ffm as FnMarkerAggr<Fm>>::Output>, Error>
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 bindf
- The function implementation
§Type Parameters
S
- The type of the function name (must convert toString
)F
- The function implementation type (must implementIntoFunction
)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>
impl<'f, Fm: FnMarker> ActivationInterface<'f, Fm> for &Activation<'f, Fm>
Source§fn variables(&self) -> &VariableBindings<'f>
fn variables(&self) -> &VariableBindings<'f>
Source§fn functions(&self) -> &FunctionBindings<'f>
fn functions(&self) -> &FunctionBindings<'f>
Source§impl<'f, Fm: FnMarker> ActivationInterface<'f, Fm> for Activation<'f, Fm>
impl<'f, Fm: FnMarker> ActivationInterface<'f, Fm> for Activation<'f, Fm>
Source§fn variables(&self) -> &VariableBindings<'f>
fn variables(&self) -> &VariableBindings<'f>
Source§fn functions(&self) -> &FunctionBindings<'f>
fn functions(&self) -> &FunctionBindings<'f>
Source§impl<'f, Fm: FnMarker> ActivationInterface<'f, Fm> for Box<Activation<'f, Fm>>
impl<'f, Fm: FnMarker> ActivationInterface<'f, Fm> for Box<Activation<'f, Fm>>
Source§fn variables(&self) -> &VariableBindings<'f>
fn variables(&self) -> &VariableBindings<'f>
Source§fn functions(&self) -> &FunctionBindings<'f>
fn functions(&self) -> &FunctionBindings<'f>
Source§impl<'f, Fm: FnMarker> Debug for Activation<'f, Fm>
impl<'f, Fm: FnMarker> Debug for Activation<'f, Fm>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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