@@ -170,22 +170,7 @@ fn print_toml_env(config: &Config, env: &[(&String, &String)]) {
170
170
171
171
fn print_json ( config : & Config , key : & ConfigKey , cv : & CV , include_key : bool ) {
172
172
let json_value = if key. is_root ( ) || !include_key {
173
- match cv {
174
- CV :: Boolean ( val, _def) => json ! ( val) ,
175
- CV :: Integer ( val, _def) => json ! ( val) ,
176
- CV :: String ( val, _def) => json ! ( val) ,
177
- CV :: List ( vals, _def) => {
178
- let jvals: Vec < _ > = vals. into_iter ( ) . map ( |( val, _def) | json ! ( val) ) . collect ( ) ;
179
- json ! ( jvals)
180
- }
181
- CV :: Table ( map, _def) => {
182
- let mut root_table = json ! ( { } ) ;
183
- for ( key, val) in map {
184
- json_add ( & mut root_table, key, val) ;
185
- }
186
- root_table
187
- }
188
- }
173
+ cv_to_json ( cv)
189
174
} else {
190
175
let mut parts: Vec < _ > = key. parts ( ) . collect ( ) ;
191
176
let last_part = parts. pop ( ) . unwrap ( ) ;
@@ -196,30 +181,27 @@ fn print_json(config: &Config, key: &ConfigKey, cv: &CV, include_key: bool) {
196
181
table[ part] = json ! ( { } ) ;
197
182
table = table. get_mut ( part) . unwrap ( ) ;
198
183
}
199
- json_add ( table, last_part, cv) ;
184
+ table[ last_part] = cv_to_json ( cv) ;
200
185
root_table
201
186
} ;
202
187
drop_println ! ( config, "{}" , serde_json:: to_string( & json_value) . unwrap( ) ) ;
203
188
204
189
// Helper for recursively converting a CV to JSON.
205
- fn json_add ( table : & mut serde_json:: Value , key : & str , cv : & CV ) {
190
+ fn cv_to_json ( cv : & CV ) -> serde_json:: Value {
206
191
match cv {
207
- CV :: Boolean ( val, _def) => table [ key ] = json ! ( val) ,
208
- CV :: Integer ( val, _def) => table [ key ] = json ! ( val) ,
209
- CV :: String ( val, _def) => table [ key ] = json ! ( val) ,
192
+ CV :: Boolean ( val, _def) => json ! ( val) ,
193
+ CV :: Integer ( val, _def) => json ! ( val) ,
194
+ CV :: String ( val, _def) => json ! ( val) ,
210
195
CV :: List ( vals, _def) => {
211
196
let jvals: Vec < _ > = vals. into_iter ( ) . map ( |( val, _def) | json ! ( val) ) . collect ( ) ;
212
- table [ key ] = json ! ( jvals) ;
197
+ json ! ( jvals)
213
198
}
214
- CV :: Table ( val, _def) => {
215
- table
216
- . as_object_mut ( )
217
- . unwrap ( )
218
- . insert ( key. to_string ( ) , json ! ( { } ) ) ;
219
- let inner_table = & mut table[ & key] ;
220
- for ( t_key, t_cv) in val {
221
- json_add ( inner_table, t_key, t_cv) ;
199
+ CV :: Table ( map, _def) => {
200
+ let mut table = json ! ( { } ) ;
201
+ for ( key, val) in map {
202
+ table[ key] = cv_to_json ( val) ;
222
203
}
204
+ table
223
205
}
224
206
}
225
207
}
0 commit comments