rscel/
lib.rs

1#![feature(string_remove_matches)]
2//! RsCel is a CEL evaluator written in Rust. CEL is a google project that
3//! describes a turing-incomplete language that can be used to evaluate
4//! a user provdided expression. The language specification can be found
5//! [here](https://github.com/google/cel-spec/blob/master/doc/langdef.md).
6//!
7//! The design goals of this project were are as follows:
8//!   * Flexible enough to allow for a user to bend the spec if needed
9//!   * Sandbox'ed in such a way that only specific values can be bound
10//!   * Can be used as a wasm depenedency (or other ffi)
11//!
12//! The basic example of how to use:
13//! ```
14//! use rscel::{CelContext, BindContext, serde_json};
15//!
16//! let mut ctx = CelContext::new();
17//! let mut exec_ctx = BindContext::new();
18//!
19//! ctx.add_program_str("main", "foo + 3").unwrap();
20//! exec_ctx.bind_param("foo", 3.into()); // 3 converted to CelValue
21//!
22//! let res = ctx.exec("main", &exec_ctx).unwrap(); // CelValue::Int(6)
23//! assert_eq!(res, 6.into());
24//! ```
25//! As of 0.10.0 binding protobuf messages from the protobuf crate is now available! Given
26//! the following protobuf message:
27//! ```protobuf
28//!
29//! message Point {
30//!   int32 x = 1;
31//!   int32 y = 2;
32//! }
33//!
34//! ```
35//! The following code can be used to evaluate a CEL expression on a Point message:
36//!
37//! ```ignore
38//! use rscel::{CelContext, BindContext};
39//!
40//! // currently rscel required protobuf messages to be in a box
41//! let p = Box::new(protos::Point::new());
42//! p.x = 4;
43//! p.y = 5;
44//!
45//! let mut ctx = CelContext::new();
46//! let mut exec_ctx = BindContext::new();
47//!
48//! ctx.add_program_str("main", "p.x + 3").unwrap();
49//! exec_ctx.bind_protobuf_msg("p", p);
50//!
51//! assert_eq!(ctx.exec("main", &exec_ctx).unwrap(), 7.into());
52//!
53//! ```
54mod compiler;
55mod context;
56mod interp;
57mod program;
58mod types;
59
60// Export some public interface
61pub mod utils;
62pub use compiler::{
63    ast_node::AstNode, compiler::CelCompiler, grammar::*, source_location::SourceLocation,
64    source_range::SourceRange, string_tokenizer::StringTokenizer, tokenizer::Tokenizer,
65};
66pub use context::{BindContext, CelContext, RsCelFunction, RsCelMacro};
67pub use interp::ByteCode;
68pub use program::{Program, ProgramDetails};
69pub use types::{CelError, CelResult, CelValue, CelValueDyn};
70
71// Some re-exports to allow a consistent use of serde
72pub use serde;
73pub use serde_json;
74
75pub use rscel_macro as macros;
76
77#[cfg(test)]
78mod tests;