From 1cb926978e12d82bda2811e6edfbe28027a40da5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 1 Apr 2021 10:03:25 +0200 Subject: Remove ConfigKind type This patch removes the ConfigKind::Frozen mechansim, which wasn't exported through the public interface at all. Because the ::Frozen variant was removed, the ConfigKind::Mutable variant is the only one remaining and because the ConfigKind type isn't exported in the API as well, we can move the variant members to the Config struct itself. Signed-off-by: Matthias Beyer --- src/config.rs | 123 ++++++++++++---------------------------------------------- 1 file changed, 24 insertions(+), 99 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index a65b93e..9a09a0a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,36 +12,14 @@ use source::Source; use path; use value::{Table, Value, ValueKind}; -#[derive(Clone, Debug)] -enum ConfigKind { - // A mutable configuration. This is the default. - Mutable { - defaults: HashMap, - overrides: HashMap, - sources: Vec>, - }, - - // A frozen configuration. - // Configuration can no longer be mutated. - Frozen, -} - -impl Default for ConfigKind { - fn default() -> Self { - ConfigKind::Mutable { - defaults: HashMap::new(), - overrides: HashMap::new(), - sources: Vec::new(), - } - } -} - /// A prioritized configuration repository. It maintains a set of /// configuration sources, fetches values to populate those, and provides /// them according to the source's priority. #[derive(Clone, Debug)] pub struct Config { - kind: ConfigKind, + defaults: HashMap, + overrides: HashMap, + sources: Vec>, /// Root of the cached configuration. pub cache: Value, @@ -50,35 +28,22 @@ pub struct Config { impl Default for Config { fn default() -> Self { Config { - kind: ConfigKind::default(), + defaults: Default::default(), + overrides: Default::default(), + sources: Default::default(), cache: Value::new(None, Table::new()), } } } impl Config { - pub fn freeze(&mut self) { - self.kind = ConfigKind::Frozen - } - /// Merge in a configuration property source. pub fn merge(&mut self, source: T) -> Result<&mut Config> where T: 'static, T: Source + Send + Sync, { - match self.kind { - ConfigKind::Mutable { - ref mut sources, .. - } => { - sources.push(Box::new(source)); - } - - ConfigKind::Frozen => { - return Err(ConfigError::Frozen); - } - } - + self.sources.push(Box::new(source)); self.refresh() } @@ -88,18 +53,7 @@ impl Config { T: 'static, T: Source + Send + Sync, { - match self.kind { - ConfigKind::Mutable { - ref mut sources, .. - } => { - sources.push(Box::new(source)); - } - - ConfigKind::Frozen => { - return Err(ConfigError::Frozen); - } - } - + self.sources.push(Box::new(source)); self.refresh()?; Ok(self) } @@ -110,34 +64,23 @@ impl Config { /// Configuration is automatically refreshed after a mutation /// operation (`set`, `merge`, `set_default`, etc.). pub fn refresh(&mut self) -> Result<&mut Config> { - self.cache = match self.kind { - // TODO: We need to actually merge in all the stuff - ConfigKind::Mutable { - ref overrides, - ref sources, - ref defaults, - } => { - let mut cache: Value = HashMap::::new().into(); - - // Add defaults - for (key, val) in defaults { - key.set(&mut cache, val.clone()); - } - - // Add sources - sources.collect_to(&mut cache)?; - - // Add overrides - for (key, val) in overrides { - key.set(&mut cache, val.clone()); - } - - cache + self.cache = { + let mut cache: Value = HashMap::::new().into(); + + // Add defaults + for (key, val) in self.defaults.iter() { + key.set(&mut cache, val.clone()); } - ConfigKind::Frozen => { - return Err(ConfigError::Frozen); + // Add sources + self.sources.collect_to(&mut cache)?; + + // Add overrides + for (key, val) in self.overrides.iter() { + key.set(&mut cache, val.clone()); } + + cache }; Ok(self) @@ -148,16 +91,7 @@ impl Config { where T: Into, { - match self.kind { - ConfigKind::Mutable { - ref mut defaults, .. - } => { - defaults.insert(key.parse()?, value.into()); - } - - ConfigKind::Frozen => return Err(ConfigError::Frozen), - }; - + self.defaults.insert(key.parse()?, value.into()); self.refresh() } @@ -173,16 +107,7 @@ impl Config { where T: Into, { - match self.kind { - ConfigKind::Mutable { - ref mut overrides, .. - } => { - overrides.insert(key.parse()?, value.into()); - } - - ConfigKind::Frozen => return Err(ConfigError::Frozen), - }; - + self.overrides.insert(key.parse()?, value.into()); self.refresh() } -- cgit v1.2.3