Don’t pretty print newlines in objects and arrays if empty

This commit is contained in:
Tobie Morgan Hitchcock 2023-04-15 16:59:55 +01:00
parent 40e75f3f35
commit 7413e93a48
2 changed files with 21 additions and 17 deletions

View file

@ -153,9 +153,11 @@ impl Display for Array {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut f = Pretty::from(f);
f.write_char('[')?;
let indent = pretty_indent();
write!(f, "{}", Fmt::pretty_comma_separated(self.as_slice()))?;
drop(indent);
if !self.is_empty() {
let indent = pretty_indent();
write!(f, "{}", Fmt::pretty_comma_separated(self.as_slice()))?;
drop(indent);
}
f.write_char(']')
}
}

View file

@ -144,20 +144,22 @@ impl Display for Object {
} else {
f.write_str("{ ")?;
}
let indent = pretty_indent();
write!(
f,
"{}",
Fmt::pretty_comma_separated(
self.0.iter().map(|args| Fmt::new(args, |(k, v), f| write!(
f,
"{}: {}",
escape_key(k),
v
))),
)
)?;
drop(indent);
if !self.is_empty() {
let indent = pretty_indent();
write!(
f,
"{}",
Fmt::pretty_comma_separated(
self.0.iter().map(|args| Fmt::new(args, |(k, v), f| write!(
f,
"{}: {}",
escape_key(k),
v
))),
)
)?;
drop(indent);
}
if is_pretty() {
f.write_char('}')
} else {