Skip to content

Commit 41a87ad

Browse files
Merge pull request #190 from matthiasbeyer/remove-dead-code
Remove dead code
2 parents e8dccf2 + 1cb9269 commit 41a87ad

File tree

3 files changed

+24
-102
lines changed

3 files changed

+24
-102
lines changed

src/config.rs

Lines changed: 24 additions & 95 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,7 +28,9 @@ 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
}
@@ -63,18 +43,7 @@ impl Config {
6343
T: 'static,
6444
T: Source + Send + Sync,
6545
{
66-
match self.kind {
67-
ConfigKind::Mutable {
68-
ref mut sources, ..
69-
} => {
70-
sources.push(Box::new(source));
71-
}
72-
73-
ConfigKind::Frozen => {
74-
return Err(ConfigError::Frozen);
75-
}
76-
}
77-
46+
self.sources.push(Box::new(source));
7847
self.refresh()
7948
}
8049

@@ -84,18 +53,7 @@ impl Config {
8453
T: 'static,
8554
T: Source + Send + Sync,
8655
{
87-
match self.kind {
88-
ConfigKind::Mutable {
89-
ref mut sources, ..
90-
} => {
91-
sources.push(Box::new(source));
92-
}
93-
94-
ConfigKind::Frozen => {
95-
return Err(ConfigError::Frozen);
96-
}
97-
}
98-
56+
self.sources.push(Box::new(source));
9957
self.refresh()?;
10058
Ok(self)
10159
}
@@ -106,34 +64,23 @@ impl Config {
10664
/// Configuration is automatically refreshed after a mutation
10765
/// operation (`set`, `merge`, `set_default`, etc.).
10866
pub fn refresh(&mut self) -> Result<&mut Config> {
109-
self.cache = match self.kind {
110-
// TODO: We need to actually merge in all the stuff
111-
ConfigKind::Mutable {
112-
ref overrides,
113-
ref sources,
114-
ref defaults,
115-
} => {
116-
let mut cache: Value = HashMap::<String, Value>::new().into();
117-
118-
// Add defaults
119-
for (key, val) in defaults {
120-
key.set(&mut cache, val.clone());
121-
}
122-
123-
// Add sources
124-
sources.collect_to(&mut cache)?;
125-
126-
// Add overrides
127-
for (key, val) in overrides {
128-
key.set(&mut cache, val.clone());
129-
}
130-
131-
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());
13273
}
13374

134-
ConfigKind::Frozen => {
135-
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());
13681
}
82+
83+
cache
13784
};
13885

13986
Ok(self)
@@ -144,16 +91,7 @@ impl Config {
14491
where
14592
T: Into<Value>,
14693
{
147-
match self.kind {
148-
ConfigKind::Mutable {
149-
ref mut defaults, ..
150-
} => {
151-
defaults.insert(key.parse()?, value.into());
152-
}
153-
154-
ConfigKind::Frozen => return Err(ConfigError::Frozen),
155-
};
156-
94+
self.defaults.insert(key.parse()?, value.into());
15795
self.refresh()
15896
}
15997

@@ -169,16 +107,7 @@ impl Config {
169107
where
170108
T: Into<Value>,
171109
{
172-
match self.kind {
173-
ConfigKind::Mutable {
174-
ref mut overrides, ..
175-
} => {
176-
overrides.insert(key.parse()?, value.into());
177-
}
178-
179-
ConfigKind::Frozen => return Err(ConfigError::Frozen),
180-
};
181-
110+
self.overrides.insert(key.parse()?, value.into());
182111
self.refresh()
183112
}
184113

src/de.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ impl<'de> de::Deserializer<'de> for Value {
141141

142142
struct StrDeserializer<'a>(&'a str);
143143

144-
impl<'a> StrDeserializer<'a> {
145-
fn new(key: &'a str) -> Self {
146-
StrDeserializer(key)
147-
}
148-
}
149-
150144
impl<'de, 'a> de::Deserializer<'de> for StrDeserializer<'a> {
151145
type Error = ConfigError;
152146

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
//!
1818
//! See the [examples](https://github.com/mehcode/config-rs/tree/master/examples) for
1919
//! general usage information.
20-
#![allow(dead_code)]
2120
#![allow(unused_imports)]
2221
#![allow(unused_variables)]
2322
#![allow(unknown_lints)]

0 commit comments

Comments
 (0)