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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::collections::HashMap;
use crate::{
function::Function,
value::{value_type::ValueType, Value},
EvalexprError, EvalexprResult,
};
mod predefined;
pub trait Context {
fn get_value(&self, identifier: &str) -> Option<&Value>;
fn call_function(&self, identifier: &str, argument: &Value) -> EvalexprResult<Value>;
}
pub trait ContextWithMutableVariables: Context {
fn set_value(&mut self, _identifier: String, _value: Value) -> EvalexprResult<()> {
Err(EvalexprError::ContextNotMutable)
}
}
pub trait ContextWithMutableFunctions: Context {
fn set_function(&mut self, _identifier: String, _function: Function) -> EvalexprResult<()> {
Err(EvalexprError::ContextNotMutable)
}
}
#[derive(Debug, Default)]
pub struct EmptyContext;
impl Context for EmptyContext {
fn get_value(&self, _identifier: &str) -> Option<&Value> {
None
}
fn call_function(&self, identifier: &str, _argument: &Value) -> EvalexprResult<Value> {
Err(EvalexprError::FunctionIdentifierNotFound(
identifier.to_string(),
))
}
}
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct HashMapContext {
variables: HashMap<String, Value>,
#[cfg_attr(feature = "serde_support", serde(skip))]
functions: HashMap<String, Function>,
}
impl HashMapContext {
pub fn new() -> Self {
Default::default()
}
}
impl Context for HashMapContext {
fn get_value(&self, identifier: &str) -> Option<&Value> {
self.variables.get(identifier)
}
fn call_function(&self, identifier: &str, argument: &Value) -> EvalexprResult<Value> {
if let Some(function) = self.functions.get(identifier) {
function.call(argument)
} else {
Err(EvalexprError::FunctionIdentifierNotFound(
identifier.to_string(),
))
}
}
}
impl ContextWithMutableVariables for HashMapContext {
fn set_value(&mut self, identifier: String, value: Value) -> EvalexprResult<()> {
if let Some(existing_value) = self.variables.get_mut(&identifier) {
if ValueType::from(&existing_value) == ValueType::from(&value) {
*existing_value = value;
return Ok(());
} else {
return Err(EvalexprError::expected_type(existing_value, value));
}
}
self.variables.insert(identifier, value);
Ok(())
}
}
impl ContextWithMutableFunctions for HashMapContext {
fn set_function(&mut self, identifier: String, function: Function) -> EvalexprResult<()> {
self.functions.insert(identifier, function);
Ok(())
}
}
#[macro_export]
macro_rules! context_map {
( ($ctx:expr) $k:expr => Function::new($($v:tt)*) ) =>
{ $crate::context_map!(($ctx) $k => Function::new($($v)*),) };
( ($ctx:expr) $k:expr => $v:expr ) =>
{ $crate::context_map!(($ctx) $k => $v,) };
( ($ctx:expr) ) => { Ok(()) };
( ($ctx:expr) $k:expr => Function::new($($v:tt)*) , $($tt:tt)*) => {{
$crate::ContextWithMutableFunctions::set_function($ctx, $k.into(), $crate::Function::new($($v)*))
.and($crate::context_map!(($ctx) $($tt)*))
}};
( ($ctx:expr) $k:expr => $v:expr , $($tt:tt)*) => {{
$crate::ContextWithMutableVariables::set_value($ctx, $k.into(), $v.into())
.and($crate::context_map!(($ctx) $($tt)*))
}};
( $($tt:tt)* ) => {{
let mut context = $crate::HashMapContext::new();
$crate::context_map!((&mut context) $($tt)*)
.map(|_| context)
}};
}