Skip to main content

Env

Struct Env 

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

Unified environment for CEL expression processing.

The Env wraps the parser and checker, providing a high-level API for working with CEL expressions. It manages:

  • Variable declarations with their types
  • Function declarations (standard library + extensions)
  • Container namespace for qualified name resolution

§Example

use cel_core::Env;
use cel_core::types::CelType;

let env = Env::with_standard_library()
    .with_variable("x", CelType::Int);

let result = env.compile("x + 1");
assert!(result.is_ok());

Implementations§

Source§

impl Env

Source

pub fn new() -> Self

Create a new empty environment.

This environment has no functions or variables defined. Use with_standard_library() for a fully-featured environment.

Source

pub fn with_standard_library() -> Self

Create a new environment with the CEL standard library.

This includes all standard operators, functions, and type constants.

Source

pub fn with_variable(self, name: impl Into<String>, cel_type: CelType) -> Self

Add a variable to the environment (builder pattern).

§Example
use cel_core::Env;
use cel_core::types::CelType;

let env = Env::with_standard_library()
    .with_variable("x", CelType::Int)
    .with_variable("y", CelType::String);
Source

pub fn add_variable(&mut self, name: impl Into<String>, cel_type: CelType)

Add a variable to the environment (mutable).

Source

pub fn with_function(self, decl: FunctionDecl) -> Self

Add a function declaration to the environment (builder pattern).

Source

pub fn add_function(&mut self, decl: FunctionDecl)

Add a function declaration to the environment (mutable).

If a function with the same name already exists, overloads are merged.

Source

pub fn with_container(self, container: impl Into<String>) -> Self

Set the container namespace (builder pattern).

The container is used for qualified name resolution.

Source

pub fn set_container(&mut self, container: impl Into<String>)

Set the container namespace (mutable).

Source

pub fn container(&self) -> &str

Get the container namespace.

Source

pub fn with_proto_types(self, registry: ProtoTypeRegistry) -> Self

Set the proto type registry (builder pattern).

The proto type registry is used for resolving protobuf types during type checking.

Source

pub fn proto_types(&self) -> Option<&ProtoTypeRegistry>

Get the proto type registry.

Source

pub fn with_extension( self, extension: impl IntoIterator<Item = FunctionDecl>, ) -> Self

Add an extension library to the environment (builder pattern).

Extensions provide additional functions beyond the standard library. Each extension is a collection of FunctionDecl values.

§Example
use cel_core::Env;
use cel_core::ext::string_extension;

let env = Env::with_standard_library()
    .with_extension(string_extension());
Source

pub fn with_all_extensions(self) -> Self

Add all available extension libraries to the environment (builder pattern).

This is a convenience method that adds all standard extensions:

  • String extension (charAt, indexOf, substring, etc.)
  • Math extension (math.greatest, math.least, math.abs, etc.)
  • Encoders extension (base64.encode, base64.decode)
  • Optionals extension (optional.of, optional.none, hasValue, etc.)
§Example
use cel_core::Env;

let env = Env::with_standard_library()
    .with_all_extensions();
Source

pub fn variables(&self) -> &HashMap<String, CelType>

Get the variables map.

Source

pub fn functions(&self) -> &HashMap<String, FunctionDecl>

Get the functions map.

Source

pub fn parse(&self, source: &str) -> ParseResult

Parse a CEL expression.

This delegates to the parser. The returned ParseResult may contain both a partial AST and errors if parsing partially succeeded.

Source

pub fn check(&self, expr: &SpannedExpr) -> CheckResult

Type-check a parsed expression.

This delegates to the checker with the environment’s variables, functions, and container.

Source

pub fn compile(&self, source: &str) -> Result<Ast, CompileError>

Parse and type-check a CEL expression, returning a checked Ast.

This is the primary entry point for compiling CEL expressions. Returns a checked Ast that can be used for evaluation.

§Example
use cel_core::Env;
use cel_core::types::CelType;

let env = Env::with_standard_library()
    .with_variable("x", CelType::Int);

let ast = env.compile("x + 1").unwrap();
assert!(ast.is_checked());
assert_eq!(ast.result_type(), Some(&CelType::Int));
Source

pub fn parse_only(&self, source: &str) -> Result<Ast, CompileError>

Parse a CEL expression without type-checking, returning an unchecked Ast.

This is useful when you want to parse an expression but don’t need type information, or when you want to defer type-checking.

§Example
use cel_core::Env;

let env = Env::with_standard_library();

let ast = env.parse_only("1 + 2").unwrap();
assert!(!ast.is_checked());
assert_eq!(ast.to_cel_string(), "1 + 2");

Trait Implementations§

Source§

impl Clone for Env

Source§

fn clone(&self) -> Env

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Env

Source§

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

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

impl Default for Env

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Env

§

impl RefUnwindSafe for Env

§

impl Send for Env

§

impl Sync for Env

§

impl Unpin for Env

§

impl UnwindSafe for Env

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.