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
use crate::*;
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Default)]
pub struct ConfigurationProviderBuilder {
values: HashMap<ConfigKey, String>,
}
impl ConfigurationProviderBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn add<K, V>(&mut self, key: K, value: V)
where
K: Into<ConfigKey>,
V: Into<String>,
{
self.values.insert(key.into(), value.into());
}
pub fn build(self) -> DefaultConfigurationProvider {
DefaultConfigurationProvider::new(self.values)
}
}
#[derive(Clone)]
pub struct DefaultConfigurationProvider {
values: Arc<HashMap<ConfigKey, String>>,
}
impl DefaultConfigurationProvider {
fn new(map: HashMap<ConfigKey, String>) -> Self {
Self {
values: Arc::new(map),
}
}
pub fn get<K: Into<ConfigKey>>(&self, key: K) -> Option<&str> {
self.try_get(&key.into())
}
pub fn len(&self) -> usize {
self.values.len()
}
}
impl ConfigurationSource for DefaultConfigurationProvider {
fn build<B: ConfigurationBuilder>(self, builder: B) -> std::io::Result<B> {
Ok(builder.push_provider(self))
}
}
impl ConfigurationProvider for DefaultConfigurationProvider {
fn try_get(&self, key: &ConfigKey) -> Option<&str> {
match self.values.get(key) {
None => None,
Some(s) => Some(s.as_ref()),
}
}
fn get_child_keys(&self, key: &ConfigKey, keys: &mut HashSet<ConfigKey>) {
for config_key in self.values.keys() {
if keys.contains(config_key) {
continue;
}
let mut config_key = config_key.clone();
while !config_key.is_empty() {
let parent = config_key.parent();
if parent == *key {
keys.insert(config_key.section_key());
}
config_key = parent;
}
}
}
}