Skip to content

Commit

Permalink
fix: emit empty scalars as "" instead of "~"
Browse files Browse the repository at this point in the history
Fixes #36.
  • Loading branch information
not-my-profile authored and Ethiraric committed Sep 13, 2024
1 parent 9a22d39 commit 7641a10
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Upcoming

**Bug fixes**
- ([#37](https://github.com/Ethiraric/yaml-rust2/pull/37))
Parse empty scalars as `""` instead of `"~"`.

## v0.8.1

**Bug fixes**
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Event {
/// Create an empty scalar.
fn empty_scalar() -> Event {
// a null scalar
Event::Scalar("~".to_owned(), TScalarStyle::Plain, 0, None)
Event::Scalar(String::new(), TScalarStyle::Plain, 0, None)
}

/// Create an empty scalar with the given anchor.
Expand Down
2 changes: 1 addition & 1 deletion src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl Yaml {
}
}
match v {
"~" | "null" => Yaml::Null,
"" | "~" | "null" => Yaml::Null,
"true" => Yaml::Boolean(true),
"false" => Yaml::Boolean(false),
_ => {
Expand Down
2 changes: 1 addition & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn test_github_27() {
let s = "&a";
let out = YamlLoader::load_from_str(s).unwrap();
let doc = &out[0];
assert_eq!(doc.as_str().unwrap(), "");
assert_eq!(*doc, Yaml::Null);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions tests/spec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl EventReceiver for YamlChecker {
Event::SequenceEnd => TestEvent::OnSequenceEnd,
Event::MappingStart(..) => TestEvent::OnMapStart,
Event::MappingEnd => TestEvent::OnMapEnd,
Event::Scalar(ref v, style, _, _) => {
if v == "~" && style == TScalarStyle::Plain {
Event::Scalar(ref v, style, _, tag) => {
if (v == "~" || v.is_empty()) && style == TScalarStyle::Plain && tag.is_none() {
TestEvent::OnNull
} else {
TestEvent::OnScalar
Expand Down
1 change: 0 additions & 1 deletion tests/yaml-test-suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ fn expected_events(expected_tree: &str) -> Vec<String> {
"-DOC ..." => "-DOC".into(),
s if s.starts_with("+SEQ []") => s.replacen("+SEQ []", "+SEQ", 1),
s if s.starts_with("+MAP {}") => s.replacen("+MAP {}", "+MAP", 1),
"=VAL :" => "=VAL :~".into(), // FIXME: known bug
s => s.into(),
}
})
Expand Down

0 comments on commit 7641a10

Please sign in to comment.