Skip to content

Commit 69b8570

Browse files
committed
Remove duplicate code.
1 parent 96a5642 commit 69b8570

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

src/cargo/ops/cargo_config.rs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,7 @@ fn print_toml_env(config: &Config, env: &[(&String, &String)]) {
170170

171171
fn print_json(config: &Config, key: &ConfigKey, cv: &CV, include_key: bool) {
172172
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)
189174
} else {
190175
let mut parts: Vec<_> = key.parts().collect();
191176
let last_part = parts.pop().unwrap();
@@ -196,30 +181,27 @@ fn print_json(config: &Config, key: &ConfigKey, cv: &CV, include_key: bool) {
196181
table[part] = json!({});
197182
table = table.get_mut(part).unwrap();
198183
}
199-
json_add(table, last_part, cv);
184+
table[last_part] = cv_to_json(cv);
200185
root_table
201186
};
202187
drop_println!(config, "{}", serde_json::to_string(&json_value).unwrap());
203188

204189
// 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 {
206191
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),
210195
CV::List(vals, _def) => {
211196
let jvals: Vec<_> = vals.into_iter().map(|(val, _def)| json!(val)).collect();
212-
table[key] = json!(jvals);
197+
json!(jvals)
213198
}
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);
222203
}
204+
table
223205
}
224206
}
225207
}

0 commit comments

Comments
 (0)