Skip to content

Commit c461a6f

Browse files
committed
Simplify impl of from_json5_value()
Signed-off-by: Matthias Beyer <[email protected]>
1 parent 935d3a3 commit c461a6f

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/file/format/json5.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,30 @@ pub fn parse(
3737
}
3838

3939
fn from_json5_value(uri: Option<&String>, value: Val) -> Value {
40-
match value {
41-
Val::String(v) => Value::new(uri, ValueKind::String(v)),
42-
43-
Val::Integer(v) => Value::new(uri, ValueKind::Integer(v)),
44-
45-
Val::Float(v) => Value::new(uri, ValueKind::Float(v)),
46-
47-
Val::Boolean(v) => Value::new(uri, ValueKind::Boolean(v)),
48-
40+
let vk = match value {
41+
Val::Null => ValueKind::Nil,
42+
Val::String(v) => ValueKind::String(v),
43+
Val::Integer(v) => ValueKind::Integer(v),
44+
Val::Float(v) => ValueKind::Float(v),
45+
Val::Boolean(v) => ValueKind::Boolean(v),
4946
Val::Object(table) => {
50-
let mut m = HashMap::new();
47+
let m = table
48+
.into_iter()
49+
.map(|(k, v)| (k, from_json5_value(uri, v)))
50+
.collect();
5151

52-
for (key, value) in table {
53-
m.insert(key, from_json5_value(uri, value));
54-
}
55-
56-
Value::new(uri, ValueKind::Table(m))
52+
ValueKind::Table(m)
5753
}
5854

5955
Val::Array(array) => {
60-
let mut l = Vec::new();
61-
62-
for value in array {
63-
l.push(from_json5_value(uri, value));
64-
}
56+
let l = array
57+
.into_iter()
58+
.map(|v| from_json5_value(uri, v))
59+
.collect();
6560

66-
Value::new(uri, ValueKind::Array(l))
61+
ValueKind::Array(l)
6762
}
63+
};
6864

69-
Val::Null => Value::new(uri, ValueKind::Nil),
70-
}
65+
Value::new(uri, vk)
7166
}

0 commit comments

Comments
 (0)