@@ -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,35 +28,22 @@ 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
}
57
37
}
58
38
59
39
impl Config {
60
- pub fn freeze ( & mut self ) {
61
- self . kind = ConfigKind :: Frozen
62
- }
63
-
64
40
/// Merge in a configuration property source.
65
41
pub fn merge < T > ( & mut self , source : T ) -> Result < & mut Config >
66
42
where
67
43
T : ' static ,
68
44
T : Source + Send + Sync ,
69
45
{
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) ) ;
82
47
self . refresh ( )
83
48
}
84
49
@@ -88,18 +53,7 @@ impl Config {
88
53
T : ' static ,
89
54
T : Source + Send + Sync ,
90
55
{
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) ) ;
103
57
self . refresh ( ) ?;
104
58
Ok ( self )
105
59
}
@@ -110,34 +64,23 @@ impl Config {
110
64
/// Configuration is automatically refreshed after a mutation
111
65
/// operation (`set`, `merge`, `set_default`, etc.).
112
66
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 ( ) ) ;
136
73
}
137
74
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 ( ) ) ;
140
81
}
82
+
83
+ cache
141
84
} ;
142
85
143
86
Ok ( self )
@@ -148,16 +91,7 @@ impl Config {
148
91
where
149
92
T : Into < Value > ,
150
93
{
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 ( ) ) ;
161
95
self . refresh ( )
162
96
}
163
97
@@ -173,16 +107,7 @@ impl Config {
173
107
where
174
108
T : Into < Value > ,
175
109
{
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 ( ) ) ;
186
111
self . refresh ( )
187
112
}
188
113
0 commit comments