pub struct Env<'f, Fm: FnMarker = (), Rm: RuntimeMarker = ()> { /* private fields */ }
Expand description
CEL expression evaluation environment.
The Env
struct represents a CEL environment that can compile expressions
into programs. It encapsulates function registries, variable declarations,
and type information needed for expression compilation.
§Type Parameters
'f
: Lifetime of functions registered in this environmentFm
: Function marker type indicating sync/async function supportRm
: Runtime marker type indicating the async runtime (if any)
§Examples
§Basic Usage
use cel_cxx::*;
let env = Env::builder()
.declare_variable::<String>("name")?
.build()?;
let program = env.compile("'Hello, ' + name")?;
§With Custom Functions
use cel_cxx::*;
let env = Env::builder()
.register_global_function("add", |x: i64, y: i64| -> i64 { x + y })?
.build()?;
let program = env.compile("add(10, 20)")?;
Implementations§
Source§impl<'f> Env<'f>
impl<'f> Env<'f>
Sourcepub fn builder() -> EnvBuilder<'f, ()>
pub fn builder() -> EnvBuilder<'f, ()>
Creates a new environment builder.
This is the starting point for creating a CEL environment. The builder allows you to register functions, declare variables, and configure the environment before building it.
§Examples
use cel_cxx::*;
let builder = Env::builder();
Source§impl<'f, Fm: FnMarker, Rm: RuntimeMarker> Env<'f, Fm, Rm>
impl<'f, Fm: FnMarker, Rm: RuntimeMarker> Env<'f, Fm, Rm>
Sourcepub fn compile<S: AsRef<[u8]>>(
&self,
source: S,
) -> Result<Program<'f, Fm, Rm>, Error>
pub fn compile<S: AsRef<[u8]>>( &self, source: S, ) -> Result<Program<'f, Fm, Rm>, Error>
Compiles a CEL expression into a Program.
This method takes a CEL expression as a string or byte slice and compiles
it into a Program
that can be evaluated with different activations.
§Arguments
source
- The CEL expression to compile
§Returns
Returns a Result
containing the compiled Program
or an Error
if compilation fails.
§Examples
use cel_cxx::*;
let env = Env::builder().build().unwrap();
let program = env.compile("1 + 2 * 3").unwrap();
§Errors
Returns an error if:
- The expression contains syntax errors
- Referenced functions or variables are not declared
- Type checking fails
Source§impl<'f, Rm: RuntimeMarker> Env<'f, (), Rm>
impl<'f, Rm: RuntimeMarker> Env<'f, (), Rm>
Sourcepub fn force_async(self) -> Env<'f, Async, Rm>
Available on crate feature async
only.
pub fn force_async(self) -> Env<'f, Async, Rm>
async
only.Forces conversion to an async environment.
This method converts a synchronous environment to an asynchronous one, allowing it to work with async functions and evaluation.
§Type Parameters
Rt
- The async runtime type to use
§Examples
use cel_cxx::*;
let sync_env = Env::builder().build()?;
let async_env = sync_env.force_async();
Source§impl<'f, Fm: FnMarker> Env<'f, Fm, ()>
impl<'f, Fm: FnMarker> Env<'f, Fm, ()>
Sourcepub fn use_runtime<Rt: Runtime>(self) -> Env<'f, Fm, Rt>
Available on crate feature async
only.
pub fn use_runtime<Rt: Runtime>(self) -> Env<'f, Fm, Rt>
async
only.Sets the async runtime for this environment.
This method specifies which async runtime should be used for asynchronous evaluation of expressions.
§Type Parameters
Rt
- The runtime type to use (must implementRuntime
)
§Examples
use cel_cxx::*;
let env = Env::builder()
.build()?
.use_runtime::<Tokio>();
Sourcepub fn use_tokio(self) -> Env<'f, Fm, Tokio>
Available on crate features async
and tokio
only.
pub fn use_tokio(self) -> Env<'f, Fm, Tokio>
async
and tokio
only.Configures the environment to use the Tokio async runtime.
This is a convenience method for setting the runtime to Tokio.
Requires the tokio
feature to be enabled.
§Examples
use cel_cxx::*;
let env = Env::builder()
.build()?
.use_tokio();
Sourcepub fn use_async_std(self) -> Env<'f, Fm, AsyncStd>
Available on crate features async
and async-std
only.
pub fn use_async_std(self) -> Env<'f, Fm, AsyncStd>
async
and async-std
only.Configures the environment to use the async-std runtime.
This is a convenience method for setting the runtime to async-std.
Requires the async-std
feature to be enabled.
§Examples
use cel_cxx::*;
let env = Env::builder()
.build()?
.use_async_std();
Sourcepub fn use_smol(self) -> Env<'f, Fm, Smol>
Available on crate features async
and smol
only.
pub fn use_smol(self) -> Env<'f, Fm, Smol>
async
and smol
only.Configures the environment to use the smol runtime.
This is a convenience method for setting the runtime to smol.
Requires the smol
feature to be enabled.
§Examples
use cel_cxx::*;
let env = Env::builder().use_smol();
Trait Implementations§
Auto Trait Implementations§
impl<'f, Fm, Rm> Freeze for Env<'f, Fm, Rm>
impl<'f, Fm = (), Rm = ()> !RefUnwindSafe for Env<'f, Fm, Rm>
impl<'f, Fm, Rm> Send for Env<'f, Fm, Rm>
impl<'f, Fm, Rm> Sync for Env<'f, Fm, Rm>
impl<'f, Fm, Rm> Unpin for Env<'f, Fm, Rm>
impl<'f, Fm = (), Rm = ()> !UnwindSafe for Env<'f, Fm, Rm>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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