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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::*;
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;

#[derive(Default)]
pub struct ConfigBuilder {
  providers: Vec<Box<dyn ConfigurationProvider>>,
}

impl ConfigBuilder {
  pub fn new() -> Self {
    Self {
      providers: Vec::new(),
    }
  }
}

struct ConfigRoot {
  providers: Vec<Box<dyn ConfigurationProvider>>,
}

#[derive(Clone)]
pub struct Config {
  root: Arc<ConfigRoot>,
}

impl fmt::Debug for Config {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    fn debug_build<'a, 'b, C: Configuration>(config: &C, builder: &mut fmt::DebugMap<'a, 'b>) {
      for s in config.sections() {
        match s.value() {
          None => {}
          Some(v) => {
            builder.entry(&s.path(), &v);
          }
        }

        debug_build(&s, builder);
      }
    }

    let mut m = f.debug_map();
    debug_build(self, &mut m);
    m.finish()
  }
}

trait ConfigExt: Configuration {
  fn child_sections(&self, key: &ConfigKey) -> Self::Sections;
  fn get_owned_section(&self, key: ConfigKey) -> Self::Section;
}

#[doc(hidden)]
impl Configuration for Arc<ConfigRoot> {
  type Section = ConfigSection;
  type Sections = std::vec::IntoIter<Self::Section>;

  fn get<K: Into<ConfigKey>>(&self, key: K) -> Option<&str> {
    self.deref().get(&key.into())
  }

  fn get_section<K: Into<ConfigKey>>(&self, key: K) -> Self::Section {
    let path = key.into();
    let key = path.section_key();
    let value = self.get(&path).map(ToOwned::to_owned);
    let root = self.clone();

    ConfigSection {
      root,
      key,
      path,
      value,
    }
  }

  fn sections(&self) -> Self::Sections {
    self.child_sections(&ConfigKey::empty())
  }
}

#[doc(hidden)]
impl ConfigExt for Arc<ConfigRoot> {
  fn get_owned_section(&self, path: ConfigKey) -> Self::Section {
    let key = path.section_key();
    let value = self.get(&path).map(ToOwned::to_owned);
    let root = self.clone();

    ConfigSection {
      root,
      key,
      path,
      value,
    }
  }

  fn child_sections(&self, key: &ConfigKey) -> Self::Sections {
    let mut section_names = HashSet::new();
    for provider in self.providers.iter() {
      provider.get_child_keys(&key, &mut section_names);
    }

    let collected: Vec<_> = section_names
      .into_iter()
      .map(|name| {
        let path = key.combine(name);
        self.get_owned_section(path)
      })
      .collect();

    collected.into_iter()
  }
}

#[derive(Clone)]
pub struct ConfigSection {
  root: Arc<ConfigRoot>,
  key: ConfigKey,
  path: ConfigKey,
  value: Option<String>,
}

impl ConfigRoot {
  fn get(&self, key: &ConfigKey) -> Option<&str> {
    for provider in self.providers.iter() {
      let value = provider.try_get(&key);
      if value.is_some() {
        return value;
      }
    }

    None
  }
}

impl ConfigurationBuilder for ConfigBuilder {
  type Config = Config;

  fn push_provider<P: ConfigurationProvider + 'static>(mut self, source: P) -> Self {
    self.providers.push(Box::new(source));
    self
  }

  fn build(self) -> std::io::Result<Self::Config> {
    Ok(Config::new(self.providers))
  }
}

impl Config {
  fn new(providers: Vec<Box<dyn ConfigurationProvider>>) -> Self {
    let root = ConfigRoot { providers };
    let root = Arc::new(root);
    Config { root }
  }
}

impl Configuration for ConfigSection {
  type Section = ConfigSection;
  type Sections = std::vec::IntoIter<Self::Section>;

  #[inline]
  fn get<K: Into<ConfigKey>>(&self, key: K) -> Option<&str> {
    let key = self.path().combine(key);
    self.root.get(key.as_ref())
  }

  #[inline]
  fn get_section<K: Into<ConfigKey>>(&self, key: K) -> Self::Section {
    let key = self.path().combine(key);
    self.root.get_owned_section(key)
  }

  #[inline]
  fn sections(&self) -> Self::Sections {
    self.root.child_sections(self.key())
  }
}

impl ConfigurationSection for ConfigSection {
  fn key(&self) -> &ConfigKey {
    &self.key
  }

  fn path(&self) -> &ConfigKey {
    &self.path
  }

  fn value(&self) -> Option<&str> {
    match &self.value {
      None => None,
      Some(s) => Some(s.as_ref()),
    }
  }
}

impl Configuration for Config {
  type Section = ConfigSection;
  type Sections = std::vec::IntoIter<Self::Section>;

  fn get<K: Into<ConfigKey>>(&self, key: K) -> Option<&str> {
    self.root.get(key)
  }

  fn get_section<K: Into<ConfigKey>>(&self, key: K) -> Self::Section {
    self.root.get_section(key)
  }

  fn sections(&self) -> Self::Sections {
    self.root.sections()
  }
}