Struct CelContext

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

The CelContext is the core context in RsCel. This context contains Program information as well as the primary entry point for evaluating an expression.

Implementations§

Source§

impl CelContext

Source

pub fn new() -> CelContext

Constructs a new empty CelContext

Examples found in repository?
examples/bench.rs (line 29)
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 86)
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 6)
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 add_program(&mut self, name: &str, prog: Program)

Add an already constructed Program to the context with a given name. Using This method can allow a Program to be constructed once and shared between contexts, if desired. Will override an existing program with same name.

Source

pub fn add_program_str(&mut self, name: &str, prog_str: &str) -> CelResult<()>

Add a Program to the context with the given name and source string. Return of this function indicates parseing result of the constructed Program. This method will not allow for a Program to be shared. Will override an existing program with same name.

Examples found in repository?
examples/bench.rs (line 32)
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 88)
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 9)
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 program_details<'a>(&'a self, name: &str) -> Option<&'a ProgramDetails>

Returns ProgramDetails for a program by name if it exists.

Source

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

Source

pub fn exec<'l>( &'l mut self, name: &str, bindings: &'l BindContext<'_>, ) -> CelResult<CelValue>

Evaluate a Program with the given name with a provided ExecContext. A single CelContext can be run multiple times with different ExecContext’s. The return of this function is a Result with either a ValueCell representing the final solution of the Program or an Error that is discovered during execution, such as mismatch of types

Examples found in repository?
examples/bench.rs (line 34)
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 93)
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 16)
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}

Trait Implementations§

Source§

impl Clone for CelContext

Source§

fn clone(&self) -> Self

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§

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.