@@ -12,36 +12,14 @@ use source::Source;
12
12
use path;
13
13
use value:: { Table , Value , ValueKind } ;
14
14
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
-
39
15
/// A prioritized configuration repository. It maintains a set of
40
16
/// configuration sources, fetches values to populate those, and provides
41
17
/// them according to the source's priority.
42
18
#[ derive( Clone , Debug ) ]
43
19
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 > > ,
45
23
46
24
/// Root of the cached configuration.
47
25
pub cache : Value ,
@@ -50,7 +28,9 @@ pub struct Config {
50
28
impl Default for Config {
51
29
fn default ( ) -> Self {
52
30
Config {
53
- kind : ConfigKind :: default ( ) ,
31
+ defaults : Default :: default ( ) ,
32
+ overrides : Default :: default ( ) ,
33
+ sources : Default :: default ( ) ,
54
34
cache : Value :: new ( None , Table :: new ( ) ) ,
55
35
}
56
36
}
@@ -63,18 +43,7 @@ impl Config {
63
43
T : ' static ,
64
44
T : Source + Send + Sync ,
65
45
{
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) ) ;
78
47
self . refresh ( )
79
48
}
80
49
@@ -84,18 +53,7 @@ impl Config {
84
53
T : ' static ,
85
54
T : Source + Send + Sync ,
86
55
{
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) ) ;
99
57
self . refresh ( ) ?;
100
58
Ok ( self )
101
59
}
@@ -106,34 +64,23 @@ impl Config {
106
64
/// Configuration is automatically refreshed after a mutation
107
65
/// operation (`set`, `merge`, `set_default`, etc.).
108
66
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 ( ) ) ;
132
73
}
133
74
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 ( ) ) ;
136
81
}
82
+
83
+ cache
137
84
} ;
138
85
139
86
Ok ( self )
@@ -144,16 +91,7 @@ impl Config {
144
91
where
145
92
T : Into < Value > ,
146
93
{
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 ( ) ) ;
157
95
self . refresh ( )
158
96
}
159
97
@@ -169,16 +107,7 @@ impl Config {
169
107
where
170
108
T : Into < Value > ,
171
109
{
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 ( ) ) ;
182
111
self . refresh ( )
183
112
}
184
113
0 commit comments