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
use eyre::{eyre, Error, Result};
use serde::{Deserialize, Serialize};
use serde_cbor::Value;
use std::collections::BTreeMap;
mod fasteval;
#[cfg(feature = "python")]
mod python;
#[cfg(feature = "savage")]
mod savage;
pub type VarMap = BTreeMap<String, Value>;
pub type LoopbackResult = Box<dyn FnOnce(Value) + Send>;
pub type LoopbackError = Box<dyn FnOnce(Error) + Send>;
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Interpreter {
Inherit,
Fasteval,
#[cfg(feature = "savage")]
Savage,
#[cfg(feature = "python")]
Python,
}
pub enum Evaluator {
Fasteval(fasteval::Evaluator),
#[cfg(feature = "savage")]
Savage(savage::Evaluator),
#[cfg(feature = "python")]
Python(python::Evaluator),
}
impl Default for Interpreter {
fn default() -> Self {
Interpreter::Inherit
}
}
impl Interpreter {
pub fn parse(&self, init: &str, expr: &str, vars: &mut VarMap) -> Result<Evaluator> {
match self {
Interpreter::Inherit => Err(eyre!("Cannot parse with interpreter=`Inherit`.")),
Interpreter::Fasteval => Ok(Evaluator::Fasteval(fasteval::Evaluator::new(
init, expr, vars,
)?)),
#[cfg(feature = "savage")]
Interpreter::Savage => Ok(Evaluator::Savage(savage::Evaluator::new(init, expr, vars)?)),
#[cfg(feature = "python")]
Interpreter::Python => Ok(Evaluator::Python(python::Evaluator::new(init, expr, vars)?)),
}
}
pub fn or(&self, other: &Self) -> Self {
if let Self::Inherit = self {
*other
} else {
*self
}
}
}
impl Evaluator {
pub fn eval(&self, vars: &mut VarMap) -> Result<Value> {
match self {
Evaluator::Fasteval(evaler) => evaler.eval(vars),
#[cfg(feature = "savage")]
Evaluator::Savage(evaler) => evaler.eval(vars),
#[cfg(feature = "python")]
Evaluator::Python(evaler) => evaler.eval(vars),
}
}
pub fn eval_lazy(
&self,
vars: &mut VarMap,
loopback: LoopbackResult,
error: LoopbackError,
) -> Result<()> {
match self {
Evaluator::Fasteval(evaler) => evaler.eval_lazy(vars, loopback, error),
#[cfg(feature = "savage")]
Evaluator::Savage(evaler) => evaler.eval_lazy(vars, loopback, error),
#[cfg(feature = "python")]
Evaluator::Python(evaler) => evaler.eval_lazy(vars, loopback, error),
}
}
}