pub enum Context<'a> {
    Root {
        functions: FunctionRegistry,
        variables: HashMap<String, Value>,
    },
    Child {
        parent: &'a Context<'a>,
        variables: HashMap<String, Value>,
    },
}
Expand description

Context is a collection of variables and functions that can be used by the interpreter to resolve expressions. The context can be either a parent context, or a child context. A parent context is created by default and contains all of the built-in functions. A child context can be created by calling .clone(). The child context has it’s own variables (which can be added to), but it will also reference the parent context. This allows for variables to be overridden within the child context while still being able to resolve variables in the child’s parents. You can have theoretically have an infinite number of child contexts that reference each-other.

So why is this important? Well some CEL-macros such as the .map macro declare intermediate user-specified identifiers that should only be available within the macro, and should not override variables in the parent context. The .map macro can clone the parent context, add the intermediate identifier to the child context, and then evaluate the map expression.

Intermediate variable stored in child context ↓ [1, 2, 3].map(x, x * 2) == [2, 4, 6] ↑ Only in scope for the duration of the map expression

Variants§

§

Root

Fields

§functions: FunctionRegistry
§variables: HashMap<String, Value>
§

Child

Fields

§parent: &'a Context<'a>
§variables: HashMap<String, Value>

Implementations§

source§

impl<'a> Context<'a>

source

pub fn add_variable<S, V>( &mut self, name: S, value: V ) -> Result<(), Box<dyn Error>>
where S: Into<String>, V: TryIntoValue,

source

pub fn add_variable_from_value<S, V>(&mut self, name: S, value: V)
where S: Into<String>, V: Into<Value>,

source

pub fn get_variable<S>(&self, name: S) -> Result<Value, ExecutionError>
where S: Into<String>,

source

pub fn add_function<T: 'static, F>(&mut self, name: &str, value: F)
where F: Handler<T> + 'static,

source

pub fn resolve(&self, expr: &Expression) -> Result<Value, ExecutionError>

source

pub fn resolve_all(&self, exprs: &[Expression]) -> Result<Value, ExecutionError>

source

pub fn clone(&self) -> Context<'_>

Trait Implementations§

source§

impl<'a> Default for Context<'a>

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'a> Freeze for Context<'a>

§

impl<'a> !RefUnwindSafe for Context<'a>

§

impl<'a> !Send for Context<'a>

§

impl<'a> !Sync for Context<'a>

§

impl<'a> Unpin for Context<'a>

§

impl<'a> !UnwindSafe for Context<'a>

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.