Skip to content

Commit

Permalink
Fix multiline string emit.
Browse files Browse the repository at this point in the history
Use `|-` instead of `|` when there is not a trailing newline
in the string value.
  • Loading branch information
cwize1 authored Jul 9, 2024
1 parent 17b4ae4 commit d9d0f90
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<'a> YamlEmitter<'a> {
/// emitter.dump(&parsed[0]).unwrap();
/// assert_eq!(output.as_str(), "\
/// ---
/// foo: |
/// foo: |-
/// bar!
/// bar!
/// baz: 42");
Expand Down Expand Up @@ -219,15 +219,7 @@ impl<'a> YamlEmitter<'a> {
&& v.contains('\n')
&& char_traits::is_valid_literal_block_scalar(v)
{
write!(self.writer, "|")?;
self.level += 1;
for line in v.lines() {
writeln!(self.writer)?;
self.write_indent()?;
// It's literal text, so don't escape special chars.
write!(self.writer, "{line}")?;
}
self.level -= 1;
self.emit_literal_block(v)?;
} else if need_quotes(v) {
escape_str(self.writer, v)?;
} else {
Expand Down Expand Up @@ -260,6 +252,26 @@ impl<'a> YamlEmitter<'a> {
}
}

fn emit_literal_block(&mut self, v: &str) -> EmitResult {
let ends_with_newline = v.ends_with('\n');
if ends_with_newline {
self.writer.write_str("|")?;
} else {
self.writer.write_str("|-")?;
}

self.level += 1;
// lines() will omit the last line if it is empty.
for line in v.lines() {
writeln!(self.writer)?;
self.write_indent()?;
// It's literal text, so don't escape special chars.
self.writer.write_str(line)?;
}
self.level -= 1;
Ok(())
}

fn emit_array(&mut self, v: &[Yaml]) -> EmitResult {
if v.is_empty() {
write!(self.writer, "[]")?;
Expand Down
37 changes: 37 additions & 0 deletions tests/test_round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ fn roundtrip(original: &Yaml) {
assert_eq!(documents[0], *original);
}

fn roundtrip_multiline(original: &Yaml) {
let mut emitted = String::new();
let mut emitter = YamlEmitter::new(&mut emitted);
emitter.multiline_strings(true);
emitter.dump(original).unwrap();

let documents = Yaml::load_from_str(&emitted).unwrap();
println!("emitted {emitted}");

assert_eq!(documents.len(), 1);
assert_eq!(documents[0], *original);
}

fn double_roundtrip(original: &str) {
let parsed = Yaml::load_from_str(original).unwrap();

Expand Down Expand Up @@ -75,3 +88,27 @@ fn test_crlf() {
let y = Yaml::Array(vec![Yaml::String("\r\n".to_owned())]);
roundtrip(&y);
}

#[test]
fn test_multiline_noline() {
let y = Yaml::Array(vec![Yaml::String("a".to_owned())]);
roundtrip_multiline(&y);
}

#[test]
fn test_multiline_inner_newline() {
let y = Yaml::Array(vec![Yaml::String("a\nb".to_owned())]);
roundtrip_multiline(&y);
}

#[test]
fn test_multiline_trailing_newline() {
let y = Yaml::Array(vec![Yaml::String("a\n".to_owned())]);
roundtrip_multiline(&y);
}

#[test]
fn test_multiline_leading_newline() {
let y = Yaml::Array(vec![Yaml::String("\na".to_owned())]);
roundtrip_multiline(&y);
}

0 comments on commit d9d0f90

Please sign in to comment.