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
impl Env
Sourcepub fn new() -> Self
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.
Sourcepub fn with_standard_library() -> Self
pub fn with_standard_library() -> Self
Create a new environment with the CEL standard library.
This includes all standard operators, functions, and type constants.
Sourcepub fn with_variable(self, name: impl Into<String>, cel_type: CelType) -> Self
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);Sourcepub fn add_variable(&mut self, name: impl Into<String>, cel_type: CelType)
pub fn add_variable(&mut self, name: impl Into<String>, cel_type: CelType)
Add a variable to the environment (mutable).
Sourcepub fn with_function(self, decl: FunctionDecl) -> Self
pub fn with_function(self, decl: FunctionDecl) -> Self
Add a function declaration to the environment (builder pattern).
Sourcepub fn add_function(&mut self, decl: FunctionDecl)
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.
Sourcepub fn with_container(self, container: impl Into<String>) -> Self
pub fn with_container(self, container: impl Into<String>) -> Self
Set the container namespace (builder pattern).
The container is used for qualified name resolution.
Sourcepub fn set_container(&mut self, container: impl Into<String>)
pub fn set_container(&mut self, container: impl Into<String>)
Set the container namespace (mutable).
Sourcepub fn with_proto_types(self, registry: ProtoTypeRegistry) -> Self
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.
Sourcepub fn proto_types(&self) -> Option<&ProtoTypeRegistry>
pub fn proto_types(&self) -> Option<&ProtoTypeRegistry>
Get the proto type registry.
Sourcepub fn with_extension(
self,
extension: impl IntoIterator<Item = FunctionDecl>,
) -> Self
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());Sourcepub fn with_all_extensions(self) -> Self
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();Sourcepub fn functions(&self) -> &HashMap<String, FunctionDecl>
pub fn functions(&self) -> &HashMap<String, FunctionDecl>
Get the functions map.
Sourcepub fn parse(&self, source: &str) -> ParseResult
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.
Sourcepub fn check(&self, expr: &SpannedExpr) -> CheckResult
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.
Sourcepub fn compile(&self, source: &str) -> Result<Ast, CompileError>
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));Sourcepub fn parse_only(&self, source: &str) -> Result<Ast, CompileError>
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");