summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-04-01 10:03:25 +0200
committerMatthias Beyer <mail@beyermatthias.de>2021-04-01 10:03:27 +0200
commit1cb926978e12d82bda2811e6edfbe28027a40da5 (patch)
treeeeedf9e395f825a9f2f2b7162673dedb928bfd26 /src/config.rs
parentdf2365e41f60ff7150cc6c18de263b178a6efbef (diff)
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 <mail@beyermatthias.de>
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs123
1 files changed, 24 insertions, 99 deletions
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<path::Expression, Value>,
- overrides: HashMap<path::Expression, Value>,
- sources: Vec<Box<dyn Source + Send + Sync>>,
- },
-
- // 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<path::Expression, Value>,
+ overrides: HashMap<path::Expression, Value>,
+ sources: Vec<Box<dyn Source + Send + Sync>>,
/// 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<T>(&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::<String, Value>::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::<String, Value>::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<Value>,
{
- 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<Value>,
{
- 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()
}