@@ -150,7 +150,7 @@ impl Config {
150
150
Ok ( ( ) )
151
151
}
152
152
153
- pub fn get < ' de , T : Deserialize < ' de > > ( & self , key : & str ) -> Result < T > {
153
+ fn get_value ( & self , key : & str ) -> Result < Value > {
154
154
let k = key. to_lowercase ( ) ;
155
155
let key = k. as_str ( ) ;
156
156
// Parse the key into a path expression
@@ -159,38 +159,44 @@ impl Config {
159
159
// Traverse the cache using the path to (possibly) retrieve a value
160
160
let value = expr. get ( & self . cache ) . cloned ( ) ;
161
161
162
- match value {
163
- Some ( value) => {
164
- // Deserialize the received value into the requested type
165
- T :: deserialize ( value) . map_err ( |e| e. extend_with_key ( key) )
166
- }
162
+ value. ok_or_else ( || ConfigError :: NotFound ( key. into ( ) ) )
163
+ }
167
164
168
- None => Err ( ConfigError :: NotFound ( key. into ( ) ) ) ,
169
- }
165
+ pub fn get < ' de , T : Deserialize < ' de > > ( & self , key : & str ) -> Result < T > {
166
+ self . get_value ( key) . and_then ( |value| {
167
+ // Deserialize the received value into the requested type
168
+ T :: deserialize ( value) . map_err ( |e| e. extend_with_key ( key) )
169
+ } )
170
170
}
171
171
172
172
pub fn get_string ( & self , key : & str ) -> Result < String > {
173
- self . get ( key) . and_then ( Value :: into_string)
173
+ self . get_value ( key)
174
+ . and_then ( |value| value. into_string ( ) . map_err ( |e| e. extend_with_key ( key) ) )
174
175
}
175
176
176
177
pub fn get_int ( & self , key : & str ) -> Result < i64 > {
177
- self . get ( key) . and_then ( Value :: into_int)
178
+ self . get_value ( key)
179
+ . and_then ( |value| value. into_int ( ) . map_err ( |e| e. extend_with_key ( key) ) )
178
180
}
179
181
180
182
pub fn get_float ( & self , key : & str ) -> Result < f64 > {
181
- self . get ( key) . and_then ( Value :: into_float)
183
+ self . get_value ( key)
184
+ . and_then ( |value| value. into_float ( ) . map_err ( |e| e. extend_with_key ( key) ) )
182
185
}
183
186
184
187
pub fn get_bool ( & self , key : & str ) -> Result < bool > {
185
- self . get ( key) . and_then ( Value :: into_bool)
188
+ self . get_value ( key)
189
+ . and_then ( |value| value. into_bool ( ) . map_err ( |e| e. extend_with_key ( key) ) )
186
190
}
187
191
188
192
pub fn get_table ( & self , key : & str ) -> Result < Map < String , Value > > {
189
- self . get ( key) . and_then ( Value :: into_table)
193
+ self . get_value ( key)
194
+ . and_then ( |value| value. into_table ( ) . map_err ( |e| e. extend_with_key ( key) ) )
190
195
}
191
196
192
197
pub fn get_array ( & self , key : & str ) -> Result < Vec < Value > > {
193
- self . get ( key) . and_then ( Value :: into_array)
198
+ self . get_value ( key)
199
+ . and_then ( |value| value. into_array ( ) . map_err ( |e| e. extend_with_key ( key) ) )
194
200
}
195
201
196
202
/// Attempt to deserialize the entire configuration into the requested type.
0 commit comments