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
use crate::value::Value;
use function::Function;
use std::collections::HashMap;
pub trait Configuration {
fn get_value(&self, identifier: &str) -> Option<&Value>;
fn get_function(&self, identifier: &str) -> Option<&Function>;
}
pub struct EmptyConfiguration;
impl Configuration for EmptyConfiguration {
fn get_value(&self, _identifier: &str) -> Option<&Value> {
None
}
fn get_function(&self, _identifier: &str) -> Option<&Function> {
None
}
}
pub struct HashMapConfiguration {
variables: HashMap<String, Value>,
functions: HashMap<String, Function>,
}
impl HashMapConfiguration {
pub fn new() -> Self {
Self {
variables: Default::default(),
functions: Default::default(),
}
}
pub fn insert_variable<S: Into<String>, V: Into<Value>>(&mut self, identifier: S, value: V) {
self.variables.insert(identifier.into(), value.into());
}
pub fn insert_function<S: Into<String>>(&mut self, identifier: S, function: Function) {
self.functions.insert(identifier.into(), function);
}
}
impl Configuration for HashMapConfiguration {
fn get_value(&self, identifier: &str) -> Option<&Value> {
self.variables.get(identifier)
}
fn get_function(&self, identifier: &str) -> Option<&Function> {
self.functions.get(identifier)
}
}