BindContext

Struct BindContext 

Source
pub struct BindContext<'a> { /* private fields */ }
Expand description

Bindings context for a cel evaluation.

This struct houses all of the bindings (dynamic values) for a cel evaluation. Currently the types of identifiers that can be bound to are variables, functions and macros. This context is separate from the contents of the CelContext to allow for multiple runs with different bound values on the same programs without the need to maintain multiple copies of the programs.

Implementations§

Source§

impl<'a> BindContext<'a>

Source

pub fn new() -> BindContext<'a>

Create a new bind context contain default functions and macros.

Examples found in repository?
examples/bench.rs (line 30)
28fn bench_run_one_nobindings() {
29    let mut cel = CelContext::new();
30    let exec = BindContext::new();
31
32    cel.add_program_str("entry", "((4 * 3) - 4) + 3").unwrap();
33
34    cel.exec("entry", &exec).unwrap();
35}
36
37fn bench_run_many_no_bindings() {
38    let mut cel = CelContext::new();
39    let exec = BindContext::new();
40
41    cel.add_program_str("entry", "((4 * 3) - 4) + 3").unwrap();
42
43    for _ in 0..10_000 {
44        cel.exec("entry", &exec).unwrap();
45    }
46}
47
48fn bench_run_one_with_binding() {
49    let mut cel = CelContext::new();
50    let mut exec = BindContext::new();
51
52    cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
53    exec.bind_param("foo", 6.into());
54
55    cel.exec("entry", &exec).unwrap();
56}
57
58fn bench_run_one_with_many_bindings() {
59    let mut cel = CelContext::new();
60    let mut exec = BindContext::new();
61
62    cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
63
64    for o in 0..10_000 {
65        exec.bind_param("foo", o.into());
66
67        cel.exec("entry", &exec).unwrap();
68    }
69}
70
71fn bench_build_many() {
72    let mut cel = CelContext::new();
73
74    for o in 0..1_000 {
75        cel.add_program_str(&format!("prog{}", o), &format!("((4 * 3) - {}) + 3", o))
76            .unwrap();
77    }
78}
79
80fn bench_construct_many_with_bindings() {
81    for o in 0..10_000 {
82        let mut cel = CelContext::new();
83        let mut exec = BindContext::new();
84
85        cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
86        exec.bind_param("foo", o.into());
87
88        cel.exec("entry", &exec).unwrap();
89    }
90}
More examples
Hide additional examples
examples/explain.rs (line 90)
85fn eval(source: &str, bindings: &serde_json::Value) -> CelResult<CelValue> {
86    let mut ctx = CelContext::new();
87
88    ctx.add_program_str("main", source)?;
89
90    let mut bind = BindContext::new();
91    bind.bind_params_from_json_obj(bindings.clone())?;
92
93    Ok(ctx.exec("main", &bind)?)
94}
examples/eval.rs (line 7)
4fn main() {
5    let args: Vec<String> = env::args().collect();
6    let mut context = CelContext::new();
7    let mut exec = BindContext::new();
8
9    context.add_program_str("prog", &args[1]).unwrap();
10
11    if args.len() > 2 {
12        exec.bind_params_from_json_obj(args[2].parse().unwrap())
13            .unwrap();
14    }
15
16    let res = context.exec("prog", &exec).unwrap();
17    println!("{}", res);
18}
Source

pub fn for_compile() -> BindContext<'a>

Source

pub fn bind_param(&mut self, name: &str, value: CelValue)

Bind a param with the given name and value.

Examples found in repository?
examples/bench.rs (line 53)
48fn bench_run_one_with_binding() {
49    let mut cel = CelContext::new();
50    let mut exec = BindContext::new();
51
52    cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
53    exec.bind_param("foo", 6.into());
54
55    cel.exec("entry", &exec).unwrap();
56}
57
58fn bench_run_one_with_many_bindings() {
59    let mut cel = CelContext::new();
60    let mut exec = BindContext::new();
61
62    cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
63
64    for o in 0..10_000 {
65        exec.bind_param("foo", o.into());
66
67        cel.exec("entry", &exec).unwrap();
68    }
69}
70
71fn bench_build_many() {
72    let mut cel = CelContext::new();
73
74    for o in 0..1_000 {
75        cel.add_program_str(&format!("prog{}", o), &format!("((4 * 3) - {}) + 3", o))
76            .unwrap();
77    }
78}
79
80fn bench_construct_many_with_bindings() {
81    for o in 0..10_000 {
82        let mut cel = CelContext::new();
83        let mut exec = BindContext::new();
84
85        cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
86        exec.bind_param("foo", o.into());
87
88        cel.exec("entry", &exec).unwrap();
89    }
90}
Source

pub fn bind_params_from_json_obj(&mut self, values: Value) -> CelResult<()>

Cheater function to bind the keys of an JSON object with its values

Examples found in repository?
examples/explain.rs (line 91)
85fn eval(source: &str, bindings: &serde_json::Value) -> CelResult<CelValue> {
86    let mut ctx = CelContext::new();
87
88    ctx.add_program_str("main", source)?;
89
90    let mut bind = BindContext::new();
91    bind.bind_params_from_json_obj(bindings.clone())?;
92
93    Ok(ctx.exec("main", &bind)?)
94}
More examples
Hide additional examples
examples/eval.rs (line 12)
4fn main() {
5    let args: Vec<String> = env::args().collect();
6    let mut context = CelContext::new();
7    let mut exec = BindContext::new();
8
9    context.add_program_str("prog", &args[1]).unwrap();
10
11    if args.len() > 2 {
12        exec.bind_params_from_json_obj(args[2].parse().unwrap())
13            .unwrap();
14    }
15
16    let res = context.exec("prog", &exec).unwrap();
17    println!("{}", res);
18}
Source

pub fn bind_param_proto_msg(&mut self, name: &str, msg: Box<dyn MessageDyn>)

Source

pub fn bind_func(&mut self, name: &str, func: &'a RsCelFunction)

Bind a function to the bind context, can be new or overwrite an existing (including default)

Source

pub fn bind_macro(&mut self, name: &str, macro_: &'a RsCelMacro)

Bind a macro to the bind context.

Source

pub fn get_param<'l>(&'l self, name: &str) -> Option<&'l CelValue>

Get a param by name.

Source

pub fn get_func(&self, name: &str) -> Option<&'a RsCelFunction>

Get a function by name.

Source

pub fn get_macro(&self, name: &str) -> Option<&'a RsCelMacro>

Get a macro by name.

Source

pub fn is_bound(&self, name: &str) -> bool

Trait Implementations§

Source§

impl<'a> Clone for BindContext<'a>

Source§

fn clone(&self) -> BindContext<'a>

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

Auto Trait Implementations§

§

impl<'a> Freeze for BindContext<'a>

§

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

§

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

§

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

§

impl<'a> Unpin for BindContext<'a>

§

impl<'a> !UnwindSafe for BindContext<'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> 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> Same for T

Source§

type Output = T

Should always be Self
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.