Skip to content

Commit cb27a47

Browse files
committed
fix: Change YAML crate to serde_yaml
- `yaml-rust` is unmaintained for over 2 years. - Like with `yaml-rust`, requires special handling for table keys. Signed-off-by: Brennan Kinney <[email protected]>
1 parent 55c464e commit cb27a47

File tree

6 files changed

+48
-91
lines changed

6 files changed

+48
-91
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ maintenance = { status = "actively-developed" }
1717
[features]
1818
default = ["toml", "json", "yaml", "ini", "ron", "json5", "convert-case", "async"]
1919
json = ["serde_json"]
20-
yaml = ["yaml-rust"]
20+
yaml = ["serde_yaml"]
2121
ini = ["rust-ini"]
2222
json5 = ["json5_rs", "serde/derive"]
2323
convert-case = ["convert_case"]
@@ -32,7 +32,7 @@ nom = "7"
3232
async-trait = { version = "0.1.50", optional = true }
3333
toml = { version = "0.8", optional = true }
3434
serde_json = { version = "1.0.2", optional = true }
35-
yaml-rust = { version = "0.4", optional = true }
35+
serde_yaml = { version = "0.9", optional = true }
3636
rust-ini = { version = "0.19", optional = true }
3737
ron = { version = "0.8", optional = true }
3838
json5_rs = { version = "0.4", optional = true, package = "json5" }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
[JSON]: https://github.com/serde-rs/json
2020
[TOML]: https://github.com/toml-lang/toml
21-
[YAML]: https://github.com/chyh1990/yaml-rust
21+
[YAML]: https://github.com/dtolnay/serde-yaml
2222
[INI]: https://github.com/zonyitoo/rust-ini
2323
[RON]: https://github.com/ron-rs/ron
2424
[JSON5]: https://github.com/callum-oakley/json5-rs

src/file/format/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub enum FileFormat {
4040
#[cfg(feature = "json")]
4141
Json,
4242

43-
/// YAML (parsed with yaml_rust)
43+
/// YAML (parsed with serde_yaml)
4444
#[cfg(feature = "yaml")]
4545
Yaml,
4646

src/file/format/yaml.rs

Lines changed: 38 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
use std::error::Error;
2-
use std::fmt;
3-
use std::mem;
4-
5-
use yaml_rust as yaml;
62

73
use crate::format;
84
use crate::map::Map;
@@ -12,94 +8,53 @@ pub fn parse(
128
uri: Option<&String>,
139
text: &str,
1410
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
15-
// Parse a YAML object from file
16-
let mut docs = yaml::YamlLoader::load_from_str(text)?;
17-
let root = match docs.len() {
18-
0 => yaml::Yaml::Hash(yaml::yaml::Hash::new()),
19-
1 => mem::replace(&mut docs[0], yaml::Yaml::Null),
20-
n => {
21-
return Err(Box::new(MultipleDocumentsError(n)));
22-
}
23-
};
24-
25-
let value = from_yaml_value(uri, &root)?;
11+
// Parse a YAML input from the provided text
12+
let value = from_yaml_value(uri, serde_yaml::from_str(text)?);
2613
format::extract_root_table(uri, value)
2714
}
2815

29-
fn from_yaml_value(
30-
uri: Option<&String>,
31-
value: &yaml::Yaml,
32-
) -> Result<Value, Box<dyn Error + Send + Sync>> {
33-
match *value {
34-
yaml::Yaml::String(ref value) => Ok(Value::new(uri, ValueKind::String(value.clone()))),
35-
yaml::Yaml::Real(ref value) => {
36-
// TODO: Figure out in what cases this can panic?
37-
value
38-
.parse::<f64>()
39-
.map_err(|_| {
40-
Box::new(FloatParsingError(value.to_string())) as Box<(dyn Error + Send + Sync)>
41-
})
42-
.map(ValueKind::Float)
43-
.map(|f| Value::new(uri, f))
44-
}
45-
yaml::Yaml::Integer(value) => Ok(Value::new(uri, ValueKind::I64(value))),
46-
yaml::Yaml::Boolean(value) => Ok(Value::new(uri, ValueKind::Boolean(value))),
47-
yaml::Yaml::Hash(ref table) => {
48-
let mut m = Map::new();
49-
for (key, value) in table {
50-
match key {
51-
yaml::Yaml::String(k) => m.insert(k.to_owned(), from_yaml_value(uri, value)?),
52-
yaml::Yaml::Integer(k) => m.insert(k.to_string(), from_yaml_value(uri, value)?),
53-
_ => unreachable!(),
54-
};
16+
pub fn from_yaml_value(uri: Option<&String>, value: serde_yaml::Value) -> Value {
17+
let vk = match value {
18+
serde_yaml::Value::Tagged(_) | serde_yaml::Value::Null => ValueKind::Nil,
19+
serde_yaml::Value::Bool(v) => ValueKind::Boolean(v),
20+
serde_yaml::Value::String(v) => ValueKind::String(v),
21+
22+
serde_yaml::Value::Number(v) => {
23+
if v.is_i64() {
24+
ValueKind::I64(v.as_i64().expect("i64"))
25+
} else if v.is_u64() {
26+
ValueKind::U64(v.as_u64().expect("u64"))
27+
} else if v.is_f64() {
28+
ValueKind::Float(v.as_f64().expect("f64"))
29+
} else {
30+
ValueKind::Nil
5531
}
56-
Ok(Value::new(uri, ValueKind::Table(m)))
5732
}
58-
yaml::Yaml::Array(ref array) => {
59-
let mut l = Vec::new();
6033

61-
for value in array {
62-
l.push(from_yaml_value(uri, value)?);
63-
}
34+
serde_yaml::Value::Mapping(table) => {
35+
let m = table
36+
.into_iter()
37+
.map(|(k, v)| {
38+
let key = match k {
39+
serde_yaml::Value::Number(v) => v.to_string(),
40+
serde_yaml::Value::String(v) => v,
41+
42+
_ => unreachable!(),
43+
};
44+
let value = from_yaml_value(uri, v);
45+
(key, value)
46+
})
47+
.collect();
6448

65-
Ok(Value::new(uri, ValueKind::Array(l)))
49+
ValueKind::Table(m)
6650
}
6751

68-
// 1. Yaml NULL
69-
// 2. BadValue – It shouldn't be possible to hit BadValue as this only happens when
70-
// using the index trait badly or on a type error but we send back nil.
71-
// 3. Alias – No idea what to do with this and there is a note in the lib that its
72-
// not fully supported yet anyway
73-
_ => Ok(Value::new(uri, ValueKind::Nil)),
74-
}
75-
}
52+
serde_yaml::Value::Sequence(array) => {
53+
let l = array.into_iter().map(|v| from_yaml_value(uri, v)).collect();
7654

77-
#[derive(Debug, Copy, Clone)]
78-
struct MultipleDocumentsError(usize);
79-
80-
impl fmt::Display for MultipleDocumentsError {
81-
fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result {
82-
write!(format, "Got {} YAML documents, expected 1", self.0)
83-
}
84-
}
85-
86-
impl Error for MultipleDocumentsError {
87-
fn description(&self) -> &str {
88-
"More than one YAML document provided"
89-
}
90-
}
91-
92-
#[derive(Debug, Clone)]
93-
struct FloatParsingError(String);
94-
95-
impl fmt::Display for FloatParsingError {
96-
fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result {
97-
write!(format, "Parsing {} as floating point number failed", self.0)
98-
}
99-
}
55+
ValueKind::Array(l)
56+
}
57+
};
10058

101-
impl Error for FloatParsingError {
102-
fn description(&self) -> &str {
103-
"Floating point number parsing failed"
104-
}
59+
Value::new(uri, vk)
10560
}

tests/file_yaml.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ fn test_error_parse() {
8181

8282
let path_with_extension: PathBuf = ["tests", "Settings-invalid.yaml"].iter().collect();
8383

84+
// Should fail to parse block mapping as no `:` exists to identify a key
8485
assert!(res.is_err());
8586
assert_eq!(
8687
res.unwrap_err().to_string(),
8788
format!(
88-
"while parsing a block mapping, did not find expected key at \
89-
line 2 column 1 in {}",
89+
"could not find expected ':' at line 3 column 1, \
90+
while scanning a simple key at line 2 column 1 in {}",
9091
path_with_extension.display()
9192
)
9293
);

tests/legacy/file_yaml.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ fn test_error_parse() {
8181

8282
let path_with_extension: PathBuf = ["tests", "Settings-invalid.yaml"].iter().collect();
8383

84+
// Should fail to parse block mapping as no `:` exists to identify a key
8485
assert!(res.is_err());
8586
assert_eq!(
8687
res.unwrap_err().to_string(),
8788
format!(
88-
"while parsing a block mapping, did not find expected key at \
89-
line 2 column 1 in {}",
89+
"could not find expected ':' at line 3 column 1, \
90+
while scanning a simple key at line 2 column 1 in {}",
9091
path_with_extension.display()
9192
)
9293
);

0 commit comments

Comments
 (0)