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
impl CelContext
Sourcepub fn new() -> CelContext
pub fn new() -> CelContext
Constructs a new empty CelContext
Examples found in repository?
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
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}Sourcepub fn add_program(&mut self, name: &str, prog: Program)
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.
Sourcepub fn add_program_str(&mut self, name: &str, prog_str: &str) -> CelResult<()>
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?
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
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}Sourcepub fn program_details<'a>(&'a self, name: &str) -> Option<&'a ProgramDetails>
pub fn program_details<'a>(&'a self, name: &str) -> Option<&'a ProgramDetails>
Returns ProgramDetails for a program by name if it exists.
pub fn get_program<'a>(&'a self, name: &str) -> Option<&'a Program>
Sourcepub fn exec<'l>(
&'l mut self,
name: &str,
bindings: &'l BindContext<'_>,
) -> CelResult<CelValue>
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?
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
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}