Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiline string emit. #32

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,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 @@ -217,15 +217,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 @@ -258,6 +250,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 {
write!(self.writer, "|")?;
} else {
write!(self.writer, "|-")?;
}

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.
write!(self.writer, "{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 @@ -13,6 +13,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 = YamlLoader::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 = YamlLoader::load_from_str(original).unwrap();

Expand Down Expand Up @@ -80,3 +93,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);
}
Loading