Skip to content

Commit 4cecd18

Browse files
committed
refactor: format/toml.rs
Explicit `ValueKind` mapping instead of direct conversion to `Value` type with inference. Matches the `format/json5.rs` logic. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
1 parent 55c464e commit 4cecd18

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/file/format/toml.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,43 @@ use std::error::Error;
22

33
use crate::format;
44
use crate::map::Map;
5-
use crate::value::Value;
5+
use crate::value::{Value, ValueKind};
66

77
pub fn parse(
88
uri: Option<&String>,
99
text: &str,
1010
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
11-
// Parse a TOML value from the provided text
12-
let value = from_toml_value(uri, &toml::from_str(text)?);
11+
// Parse a TOML input from the provided text
12+
let value = from_toml_value(uri, toml::from_str(text)?);
1313
format::extract_root_table(uri, value)
1414
}
1515

16-
fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value {
17-
match *value {
18-
toml::Value::String(ref value) => Value::new(uri, value.to_string()),
19-
toml::Value::Float(value) => Value::new(uri, value),
20-
toml::Value::Integer(value) => Value::new(uri, value),
21-
toml::Value::Boolean(value) => Value::new(uri, value),
22-
23-
toml::Value::Table(ref table) => {
24-
let mut m = Map::new();
25-
26-
for (key, value) in table {
27-
m.insert(key.clone(), from_toml_value(uri, value));
28-
}
29-
30-
Value::new(uri, m)
16+
fn from_toml_value(uri: Option<&String>, value: toml::Value) -> Value {
17+
let vk = match value {
18+
toml::Value::Datetime(v) => ValueKind::String(v.to_string()),
19+
toml::Value::String(v) => ValueKind::String(v),
20+
toml::Value::Float(v) => ValueKind::Float(v),
21+
toml::Value::Integer(v) => ValueKind::I64(v),
22+
toml::Value::Boolean(v) => ValueKind::Boolean(v),
23+
24+
toml::Value::Table(table) => {
25+
let m = table
26+
.into_iter()
27+
.map(|(k, v)| (k, from_toml_value(uri, v)))
28+
.collect();
29+
30+
ValueKind::Table(m)
3131
}
3232

33-
toml::Value::Array(ref array) => {
34-
let mut l = Vec::new();
35-
36-
for value in array {
37-
l.push(from_toml_value(uri, value));
38-
}
33+
toml::Value::Array(array) => {
34+
let l = array
35+
.into_iter()
36+
.map(|v| from_toml_value(uri, v))
37+
.collect();
3938

40-
Value::new(uri, l)
39+
ValueKind::Array(l)
4140
}
41+
};
4242

43-
toml::Value::Datetime(ref datetime) => Value::new(uri, datetime.to_string()),
44-
}
43+
Value::new(uri, vk)
4544
}

0 commit comments

Comments
 (0)