From c7c48f94c5e582fc596a3797ca5fdaad7a6c6a58 Mon Sep 17 00:00:00 2001 From: Clark McCauley Date: Mon, 29 May 2023 12:15:47 -0600 Subject: [PATCH] Added some examples about how to use the library --- Cargo.toml | 2 +- README.md | 8 ++++++++ example/Cargo.toml | 21 +++++++++++++++++++++ example/src/functions.rs | 19 +++++++++++++++++++ example/src/simple.rs | 8 ++++++++ example/src/variables.rs | 10 ++++++++++ interpreter/src/lib.rs | 6 +++--- 7 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 example/Cargo.toml create mode 100644 example/src/functions.rs create mode 100644 example/src/simple.rs create mode 100644 example/src/variables.rs diff --git a/Cargo.toml b/Cargo.toml index 00b8f8c..66ee733 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["cli", "parser", "interpreter"] +members = ["cli", "parser", "interpreter", "example"] [profile.bench] lto = true diff --git a/README.md b/README.md index 68e875f..fe293ec 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,11 @@ fn main() { assert_eq!(value, true.into()); } ``` + +### Examples + +Check out these other examples to learn how to use this library: + +- [Simple](./example/src/simple.rs) - A simple example of how to use the library. +- [Variables](./example/src/variables.rs) - Passing variables and using them in your program. +- [Functions](./example/src/functions.rs) - Defining and using custom functions in your program. \ No newline at end of file diff --git a/example/Cargo.toml b/example/Cargo.toml new file mode 100644 index 0000000..0dfbf17 --- /dev/null +++ b/example/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cel-interpreter = { path = "../interpreter" } + +[[bin]] +name = "simple" +path = "src/simple.rs" + +[[bin]] +name = "variables" +path = "src/variables.rs" + +[[bin]] +name = "functions" +path = "src/functions.rs" \ No newline at end of file diff --git a/example/src/functions.rs b/example/src/functions.rs new file mode 100644 index 0000000..55b31b8 --- /dev/null +++ b/example/src/functions.rs @@ -0,0 +1,19 @@ +use cel_interpreter::{CelType, Context, Expression, Program, ResolveResult}; + +fn main() { + let program = Program::compile("add(2, 3)").unwrap(); + let mut context = Context::default(); + context.add_function("add", add); + + let value = program.execute(&context).unwrap(); + assert_eq!(value, 5.into()); +} + +/// The add function takes two arguments and returns their sum. We discard the first +/// parameter because the add function is not a method, it is always called with two +/// arguments. +fn add(_: Option<&CelType>, args: &[Expression], ctx: &Context) -> ResolveResult { + let a = CelType::resolve(args.get(0).unwrap(), ctx)?; + let b = CelType::resolve(args.get(1).unwrap(), ctx)?; + Ok(a + b) +} diff --git a/example/src/simple.rs b/example/src/simple.rs new file mode 100644 index 0000000..1dae526 --- /dev/null +++ b/example/src/simple.rs @@ -0,0 +1,8 @@ +use cel_interpreter::{Context, Program}; + +fn main() { + let program = Program::compile("1 == 1").unwrap(); + let context = Context::default(); + let value = program.execute(&context).unwrap(); + assert_eq!(value, true.into()); +} diff --git a/example/src/variables.rs b/example/src/variables.rs new file mode 100644 index 0000000..76f1f6e --- /dev/null +++ b/example/src/variables.rs @@ -0,0 +1,10 @@ +use cel_interpreter::{Context, Program}; + +fn main() { + let program = Program::compile("foo * 2").unwrap(); + let mut context = Context::default(); + context.add_variable("foo", 10); + + let value = program.execute(&context).unwrap(); + assert_eq!(value, 20.into()); +} diff --git a/interpreter/src/lib.rs b/interpreter/src/lib.rs index 8cec926..51beff0 100644 --- a/interpreter/src/lib.rs +++ b/interpreter/src/lib.rs @@ -1,14 +1,14 @@ extern crate core; -use crate::context::Context; -use crate::objects::{CelType, ResolveResult}; -use cel_parser::ast::Expression; use cel_parser::parser::ExpressionParser; use std::convert::TryFrom; use std::rc::Rc; use thiserror::Error; pub mod context; +pub use cel_parser::Expression; +pub use context::Context; +pub use objects::{CelType, ResolveResult}; mod duration; mod functions; pub mod objects;