Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added examples on how to use the library #5

Merged
merged 1 commit into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["cli", "parser", "interpreter"]
members = ["cli", "parser", "interpreter", "example"]

[profile.bench]
lto = true
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
21 changes: 21 additions & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 19 additions & 0 deletions example/src/functions.rs
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 8 additions & 0 deletions example/src/simple.rs
Original file line number Diff line number Diff line change
@@ -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());
}
10 changes: 10 additions & 0 deletions example/src/variables.rs
Original file line number Diff line number Diff line change
@@ -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());
}
6 changes: 3 additions & 3 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down