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
use crate::context::Context;
use crate::objects::CelType;
use cel_parser::ast::Expression;
use cel_parser::parser::ExpressionParser;
use std::convert::TryFrom;
use thiserror::Error;
pub mod context;
mod functions;
pub mod objects;
#[derive(Error, Debug)]
#[error("Error parsing {msg}")]
pub struct ParseError {
msg: String,
}
#[derive(Debug)]
pub struct Program {
expression: Expression,
}
impl Program {
pub fn compile(source: &str) -> Result<Program, ParseError> {
match ExpressionParser::new().parse(source) {
Ok(expression) => Ok(Program { expression }),
Err(e) => Err(ParseError {
msg: format!("{}", e),
}),
}
}
pub fn execute(&self, context: &Context) -> CelType {
CelType::resolve(&self.expression, &context)
}
}
impl TryFrom<&str> for Program {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Program::compile(value)
}
}
#[cfg(test)]
mod tests {
use crate::Program;
use std::convert::TryInto;
#[test]
fn parse() {
Program::compile("1 + 1").unwrap();
}
#[test]
fn from_str() {
let input = "1.1";
let _p: Program = input.try_into().unwrap();
}
}