Skip to content

Commit 5bb8600

Browse files
authored
Merge branch '1BADragon:main' into main
2 parents 830c258 + b6f1da0 commit 5bb8600

File tree

20 files changed

+1407
-845
lines changed

20 files changed

+1407
-845
lines changed

.github/workflows/rust.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,34 @@ jobs:
1515

1616
steps:
1717
- uses: actions/checkout@v3
18+
1819
- name: Build
1920
run: cargo build --verbose
2021

2122
test_default:
2223
runs-on: ubuntu-latest
2324

2425
steps:
26+
- name: Install Protoc
27+
uses: arduino/setup-protoc@v3
28+
2529
- uses: actions/checkout@v3
30+
2631
- name: Run tests
2732
run: cargo test --verbose
33+
env:
34+
RSCEL_TEST_PROTO: 1
2835

2936
test_no_default:
3037
runs-on: ubuntu-latest
3138

3239
steps:
40+
- name: Install Protoc
41+
uses: arduino/setup-protoc@v3
42+
3343
- uses: actions/checkout@v3
44+
3445
- name: Run tests
3546
run: cargo test --verbose --no-default-features
47+
env:
48+
RSCEL_TEST_PROTO: 1

Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
[package]
22
name = "rscel"
3-
version = "0.8.0"
3+
version = "0.10.0"
44
edition = "2021"
55
description = "Cel interpreter in rust"
66
license = "MIT"
77

88
[lib]
99
crate-type = ["cdylib", "rlib"]
1010

11+
[profile.release-with-debug]
12+
inherits = "release"
13+
debug = true
14+
1115
[features]
1216
default = ["type_prop"]
1317
ast_ser = []
@@ -20,6 +24,9 @@ wasm = [
2024
"console_error_panic_hook", "chrono/wasmbind"
2125
]
2226

27+
[build-dependencies]
28+
protobuf-codegen = "3.4.0"
29+
protoc-bin-vendored = "3.0.0"
2330

2431
[dependencies]
2532
test-case = "3.2.1"
@@ -30,6 +37,7 @@ serde_json = { version = "1.0.108", features = ["raw_value"] }
3037
chrono = { version = "0.4.31", features = ["serde"] }
3138
duration-str = "0.7.0"
3239
num = "0.4.1"
40+
protobuf = { version = "3.4.0" }
3341

3442
# Dependencies for python bindings
3543
pyo3 = { version = "0.20.0", optional = true, features = ["extension-module", "chrono"] }

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ default: build
66
build:
77
cargo build $(CARGO_ARGS)
88

9-
test:
9+
run-tests:
1010
cargo test $(CARGO_ARGS)
1111

12-
test-all: test
12+
run-all-tests: run-tests
1313
cargo test --no-default-features $(CARGO_ARGS)
1414

1515
.env:

README.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,58 @@ The design goals of this project were are as follows:
1010
* Sandbox'ed in such a way that only specific values can be bound
1111
* Can be used as a wasm depenedency (or other ffi)
1212

13-
While Google's CEL spec was designed around the protobuf message,
14-
I decided to focus on using the JSON message instead (CEL spec specifies
15-
how to convert from JSON to CEL types).
16-
1713
The basic example of how to use:
1814
```rust
19-
use rscel::{CelContext, BindContext, serde_json};
15+
use rscel::{CelContext, BindContext};
2016

2117
let mut ctx = CelContext::new();
2218
let mut exec_ctx = BindContext::new();
2319

2420
ctx.add_program_str("main", "foo + 3").unwrap();
25-
exec_ctx.bind_param("foo", 3.into()); // convert to serde_json::Value
21+
exec_ctx.bind_param("foo", 3.into()); // convert to CelValue
22+
23+
let res = ctx.exec("main", &exec_ctx).unwrap(); // CelValue::Int(6)
24+
assert_eq!(res, 6.into());
25+
```
26+
27+
As of 0.10.0 binding protobuf messages from the protobuf crate is now available! Given
28+
the following protobuf message:
29+
```protobuf
30+
31+
message Point {
32+
int32 x = 1;
33+
int32 y = 2;
34+
}
35+
36+
```
37+
The following code can be used to evaluate a CEL expression on a Point message:
38+
39+
```rust
40+
use rscel::{CelContext, BindContext};
41+
42+
// currently rscel required protobuf messages to be in a box
43+
let p = Box::new(protos::Point::new());
44+
p.x = 4;
45+
p.y = 5;
46+
47+
let mut ctx = CelContext::new();
48+
let mut exec_ctx = BindContext::new();
49+
50+
ctx.add_program_str("main", "p.x + 3").unwrap();
51+
exec_ctx.bind_protobuf_msg("p", p);
2652

27-
let res = ctx.exec("main", &exec_ctx).unwrap(); // ValueCell::Int(6)
28-
assert!(TryInto::<i64>::try_into(res).unwrap() == 6);
53+
assert_eq!(ctx.exec("main", &exec_ctx), 7.into());
54+
2955
```
3056

3157
## Current Benchmark Times
3258
| Bench Run | Time |
3359
|--------------------------|----------------|
34-
|Run One No Binding: | 0.000310S |
35-
|Run Many No Binding: | 0.003657S |
36-
|Run One With Binding: | 0.000033S |
37-
|Run Many With Bindings: | 0.004281S |
38-
|Build Many: | 0.002312S |
39-
|Build Many With Bindings: | 0.051369S |
60+
|Run One No Binding | 0.000070054S |
61+
|Run Many No Binding | 0.003451698S |
62+
|Run One With Binding | 0.000015177S |
63+
|Run Many With Bindings | 0.004443471S |
64+
|Build Many | 0.006989954S |
65+
|Build Many With Bindings | 0.084859163S |
4066

4167
Build status: [![Rust](https://github.com/1BADragon/rscel/actions/workflows/rust.yml/badge.svg)](https://github.com/1BADragon/rscel/actions/workflows/rust.yml)

build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::env;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-env-changed=RSCEL_TEST_PROTO");
5+
6+
if let Ok(_) = env::var("RSCEL_TEST_PROTO") {
7+
println!("cargo:rustc-cfg=test_protos");
8+
protobuf_codegen::Codegen::new()
9+
.protoc()
10+
.include("test/protos")
11+
.inputs(["test/protos/test.proto"])
12+
.cargo_out_dir("test_protos")
13+
.run_from_script();
14+
}
15+
}

0 commit comments

Comments
 (0)