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
use crate::Config;
pub struct MultiConfig {
configs: Vec<Box<dyn Config>>,
}
impl MultiConfig {
pub fn new(configs: Vec<Box<dyn Config>>) -> Self {
Self { configs }
}
}
impl Config for MultiConfig {
fn get(&self, key: &str) -> Option<String> {
for config in self.configs.iter() {
match config.get(key) {
Some(value) => return Some(value),
None => continue,
};
}
None
}
}
#[cfg(test)]
mod tests {
use crate::{multi::MultiConfig, Config};
#[test]
fn multi() {
use std::collections::HashMap;
let mut m1 = HashMap::new();
m1.insert("foo", "bar");
m1.insert("bar", "baz");
let mut m2 = HashMap::new();
m2.insert("foo", "buz");
m2.insert("buz", "foo");
let mc = MultiConfig::new(vec![Box::new(m2), Box::new(m1)]);
assert_eq!(mc.get("foo"), Some("buz".to_string()));
assert_eq!(mc.get("bar"), Some("baz".to_string()));
assert_eq!(mc.get("buz"), Some("foo".to_string()));
}
}