Skip to content

Commit 1cb9269

Browse files
committed
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 <[email protected]>
1 parent df2365e commit 1cb9269

File tree

1 file changed

+24
-99
lines changed

1 file changed

+24
-99
lines changed

src/config.rs

Lines changed: 24 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,14 @@ use source::Source;
1212
use path;
1313
use value::{Table, Value, ValueKind};
1414

15-
#[derive(Clone, Debug)]
16-
enum ConfigKind {
17-
// A mutable configuration. This is the default.
18-
Mutable {
19-
defaults: HashMap<path::Expression, Value>,
20-
overrides: HashMap<path::Expression, Value>,
21-
sources: Vec<Box<dyn Source + Send + Sync>>,
22-
},
23-
24-
// A frozen configuration.
25-
// Configuration can no longer be mutated.
26-
Frozen,
27-
}
28-
29-
impl Default for ConfigKind {
30-
fn default() -> Self {
31-
ConfigKind::Mutable {
32-
defaults: HashMap::new(),
33-
overrides: HashMap::new(),
34-
sources: Vec::new(),
35-
}
36-
}
37-
}
38-
3915
/// A prioritized configuration repository. It maintains a set of
4016
/// configuration sources, fetches values to populate those, and provides
4117
/// them according to the source's priority.
4218
#[derive(Clone, Debug)]
4319
pub struct Config {
44-
kind: ConfigKind,
20+
defaults: HashMap<path::Expression, Value>,
21+
overrides: HashMap<path::Expression, Value>,
22+
sources: Vec<Box<dyn Source + Send + Sync>>,
4523

4624
/// Root of the cached configuration.
4725
pub cache: Value,
@@ -50,35 +28,22 @@ pub struct Config {
5028
impl Default for Config {
5129
fn default() -> Self {
5230
Config {
53-
kind: ConfigKind::default(),
31+
defaults: Default::default(),
32+
overrides: Default::default(),
33+
sources: Default::default(),
5434
cache: Value::new(None, Table::new()),
5535
}
5636
}
5737
}
5838

5939
impl Config {
60-
pub fn freeze(&mut self) {
61-
self.kind = ConfigKind::Frozen
62-
}
63-
6440
/// Merge in a configuration property source.
6541
pub fn merge<T>(&mut self, source: T) -> Result<&mut Config>
6642
where
6743
T: 'static,
6844
T: Source + Send + Sync,
6945
{
70-
match self.kind {
71-
ConfigKind::Mutable {
72-
ref mut sources, ..
73-
} => {
74-
sources.push(Box::new(source));
75-
}
76-
77-
ConfigKind::Frozen => {
78-
return Err(ConfigError::Frozen);
79-
}
80-
}
81-
46+
self.sources.push(Box::new(source));
8247
self.refresh()
8348
}
8449

@@ -88,18 +53,7 @@ impl Config {
8853
T: 'static,
8954
T: Source + Send + Sync,
9055
{
91-
match self.kind {
92-
ConfigKind::Mutable {
93-
ref mut sources, ..
94-
} => {
95-
sources.push(Box::new(source));
96-
}
97-
98-
ConfigKind::Frozen => {
99-
return Err(ConfigError::Frozen);
100-
}
101-
}
102-
56+
self.sources.push(Box::new(source));
10357
self.refresh()?;
10458
Ok(self)
10559
}
@@ -110,34 +64,23 @@ impl Config {
11064
/// Configuration is automatically refreshed after a mutation
11165
/// operation (`set`, `merge`, `set_default`, etc.).
11266
pub fn refresh(&mut self) -> Result<&mut Config> {
113-
self.cache = match self.kind {
114-
// TODO: We need to actually merge in all the stuff
115-
ConfigKind::Mutable {
116-
ref overrides,
117-
ref sources,
118-
ref defaults,
119-
} => {
120-
let mut cache: Value = HashMap::<String, Value>::new().into();
121-
122-
// Add defaults
123-
for (key, val) in defaults {
124-
key.set(&mut cache, val.clone());
125-
}
126-
127-
// Add sources
128-
sources.collect_to(&mut cache)?;
129-
130-
// Add overrides
131-
for (key, val) in overrides {
132-
key.set(&mut cache, val.clone());
133-
}
134-
135-
cache
67+
self.cache = {
68+
let mut cache: Value = HashMap::<String, Value>::new().into();
69+
70+
// Add defaults
71+
for (key, val) in self.defaults.iter() {
72+
key.set(&mut cache, val.clone());
13673
}
13774

138-
ConfigKind::Frozen => {
139-
return Err(ConfigError::Frozen);
75+
// Add sources
76+
self.sources.collect_to(&mut cache)?;
77+
78+
// Add overrides
79+
for (key, val) in self.overrides.iter() {
80+
key.set(&mut cache, val.clone());
14081
}
82+
83+
cache
14184
};
14285

14386
Ok(self)
@@ -148,16 +91,7 @@ impl Config {
14891
where
14992
T: Into<Value>,
15093
{
151-
match self.kind {
152-
ConfigKind::Mutable {
153-
ref mut defaults, ..
154-
} => {
155-
defaults.insert(key.parse()?, value.into());
156-
}
157-
158-
ConfigKind::Frozen => return Err(ConfigError::Frozen),
159-
};
160-
94+
self.defaults.insert(key.parse()?, value.into());
16195
self.refresh()
16296
}
16397

@@ -173,16 +107,7 @@ impl Config {
173107
where
174108
T: Into<Value>,
175109
{
176-
match self.kind {
177-
ConfigKind::Mutable {
178-
ref mut overrides, ..
179-
} => {
180-
overrides.insert(key.parse()?, value.into());
181-
}
182-
183-
ConfigKind::Frozen => return Err(ConfigError::Frozen),
184-
};
185-
110+
self.overrides.insert(key.parse()?, value.into());
186111
self.refresh()
187112
}
188113

0 commit comments

Comments
 (0)