-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
132 lines (122 loc) · 3.48 KB
/
Copy pathlib.rs
File metadata and controls
132 lines (122 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! cel-rs provides a stack-based runtime for developing domain specific languages, including
//! concatenative languages to describe concurrent processes.
//!
//! This crate exposes two main components:
//!
//! - **cel-runtime**: The core stack-based runtime and CEL parser
//! - **cel-rs-macros**: Procedural macros for CEL expressions
//!
//! For **Adam** — the property-model DSL built on top of this runtime (`adam-lang`,
//! `adam-rs`) — see [*The Adam Programming Language*](../book/index.html), a tutorial and
//! reference manual for that language specifically.
//!
//! # Examples
//!
//! ## Using the Runtime
//!
//! ```rust
//! use cel_rs::cel_runtime::*;
//!
//! // Create a segment that takes a u32 and &str as arguments
//! let segment = Segment::<(u32, &str)>::new()
//! .op1r(|s| {
//! let r = s.parse::<u32>()?;
//! Ok(r)
//! })
//! .op2(|a, b| a + b)
//! .op1(|r| r.to_string());
//! assert_eq!(segment.call((1u32, "2")).unwrap(), "3");
//! ```
//!
//! ## Using the Parser
//!
//! ```rust
//! use cel_rs::cel_parser::{CELParser, OpLookup};
//! use proc_macro2::TokenStream;
//! use std::str::FromStr;
//!
//! let input = TokenStream::from_str("10").unwrap();
//! let mut parser = CELParser::new(OpLookup::new());
//! parser.set_tokens(input.into_iter());
//! let result = parser.is_expression();
//! assert!(result.is_ok());
//! ```
//!
//! ## Parsing an Array Literal
//!
//! A non-empty CEL array literal evaluates to one
//! [`DynamicArray`](cel_runtime::DynamicArray) that converts to the corresponding `Vec<T>`
//! without moving or reallocating its elements:
//!
//! ```rust
//! use cel_rs::parser::{CELParser, OpLookup};
//! use cel_rs::runtime::DynamicArray;
//!
//! let mut segment = CELParser::new(OpLookup::new()).parse_str("[0, 1, 2]").unwrap();
//! let array: DynamicArray = segment.call0().unwrap();
//! assert_eq!(array.try_into_vec::<i32>().unwrap(), vec![0, 1, 2]);
//! ```
//!
//! ## Using the Macros
//!
//! ```rust
//! use cel_rs::cel_rs_macros::expression;
//!
//! expression! {
//! 54 + 25 * (11 + 6 * 6)
//! };
//! ```
pub use cel_parser;
pub use cel_rs_macros;
pub use cel_runtime;
// Re-export commonly used items for convenience
/// Re-exports from the cel-runtime crate for convenient access.
pub mod runtime {
pub use cel_runtime::*;
}
/// Re-exports for the CEL parser.
pub mod parser {
pub use cel_parser::{CELParser, op_table::OpLookup};
}
/// Re-exports from the cel-rs-macros crate for convenient access.
pub mod macros {
pub use cel_rs_macros::*;
}
#[cfg(all(test, feature = "playground"))]
mod playground {
struct Experiment {
a: u32,
}
struct Segment<T, F: Fn(&mut T) -> String> {
context: T,
f: F,
}
impl<T, F: Fn(&mut T) -> String> Segment<T, F> {
fn new(context: T, f: F) -> Self {
Self { context, f }
}
fn call(&mut self) -> String {
(self.f)(&mut self.context)
}
}
#[test]
fn context_segment() {
let experiment = Experiment { a: 1 };
let mut segment = Segment::new(experiment, |e| e.a.to_string());
println!("{}", segment.call());
}
/*
#[test]
fn expression_macro_error() {
let result = cel_rs_macros::expression! {
"Hello" + "World" + 32.0;
};
}
#[test]
fn expression_macro_error2() {
let result = cel_rs_macros::expression! {
"Hello" + "World" + 32.0
};
}
*/
}