From ef990758a19d4d845cf19a8943e7d905909cafd8 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 1 Feb 2021 18:35:53 -0800 Subject: [PATCH] Write trailing newline after yaml document 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. --- src/lib.rs | 4 ++-- src/ser.rs | 1 + tests/test_serde.rs | 1 - 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8ce94cba..433a5839 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 = serde_yaml::from_str(&s)?; @@ -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); diff --git a/src/ser.rs b/src/ser.rs index 194b387a..71815a64 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -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(()) } diff --git a/tests/test_serde.rs b/tests/test_serde.rs index ba80c5a0..3f66f388 100644 --- a/tests/test_serde.rs +++ b/tests/test_serde.rs @@ -16,7 +16,6 @@ fn test_serde(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);