Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Write trailing newline after yaml document
Browse files Browse the repository at this point in the history
Unlike with JSON, there is never really a use case for the caller to
write more stuff on the same line after the last character of YAML
output.
  • Loading branch information
dtolnay committed Feb 2, 2021
1 parent 299c568 commit ef99075
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//!
//! // Serialize it to a YAML string.
//! let s = serde_yaml::to_string(&map)?;
//! assert_eq!(s, "---\nx: 1.0\ny: 2.0");
//! assert_eq!(s, "---\nx: 1.0\ny: 2.0\n");
//!
//! // Deserialize it back to a Rust type.
//! let deserialized_map: BTreeMap<String, f64> = serde_yaml::from_str(&s)?;
Expand Down Expand Up @@ -57,7 +57,7 @@
//! let point = Point { x: 1.0, y: 2.0 };
//!
//! let s = serde_yaml::to_string(&point)?;
//! assert_eq!(s, "---\nx: 1.0\ny: 2.0");
//! assert_eq!(s, "---\nx: 1.0\ny: 2.0\n");
//!
//! let deserialized_point: Point = serde_yaml::from_str(&s)?;
//! assert_eq!(point, deserialized_point);
Expand Down
1 change: 1 addition & 0 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ where
YamlEmitter::new(&mut writer_adapter)
.dump(&doc)
.map_err(error::emitter)?;
writer_adapter.writer.write_all(b"\n").map_err(error::io)?;
Ok(())
}

Expand Down
1 change: 0 additions & 1 deletion tests/test_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ fn test_serde<T>(thing: &T, yaml: &str)
where
T: serde::Serialize + serde::de::DeserializeOwned + PartialEq + Debug,
{
let yaml = yaml.trim_end_matches('\n');
let serialized = serde_yaml::to_string(&thing).unwrap();
assert_eq!(yaml, serialized);

Expand Down

0 comments on commit ef99075

Please sign in to comment.