-
Notifications
You must be signed in to change notification settings - Fork 137
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
RUST-2019 Respect pretty printing flag for Document and Bson #501
Changes from 6 commits
9f6060c
1368c16
a53ac28
e5bf23e
d18f06c
96a03bf
4251658
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,21 +80,56 @@ impl Hash for Document { | |
|
||
impl Display for Document { | ||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { | ||
fmt.write_str("{")?; | ||
let indent_str; | ||
if let Some(width) = fmt.width() { | ||
indent_str = " ".repeat(width); | ||
} else { | ||
indent_str = "".to_string(); | ||
} | ||
write!(fmt, "{{")?; | ||
if fmt.alternate() && !self.inner.is_empty() { | ||
fmt.write_str("\n")?; | ||
} | ||
|
||
let mut first = true; | ||
for (k, v) in self { | ||
if first { | ||
first = false; | ||
fmt.write_str(" ")?; | ||
write!(fmt, " ")?; | ||
} else { | ||
fmt.write_str(", ")?; | ||
fmt.write_str(if fmt.alternate() { ",\n " } else { ", " })?; | ||
} | ||
|
||
write!(fmt, "\"{}\": {}", k, v)?; | ||
if fmt.alternate() { | ||
let mut indent; | ||
if let Some(width) = fmt.width() { | ||
indent = width; | ||
} else { | ||
indent = 0; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest using let mut indent = fmt.width().unwrap_or(0); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for bringing this method to my attention! |
||
match v { | ||
Bson::Document(ref doc) => { | ||
indent += 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason to indent by 1 space? I think 2 spaces would be more readable, but not sure if there's a convention we want to follow here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no real reason, I agree that 2 spaces would be more readable and I updated this |
||
write!(fmt, "{indent_str}\"{}\": {doc:#indent$}", k)?; | ||
} | ||
Bson::Array(_arr) => { | ||
indent += 1; | ||
write!(fmt, "{indent_str}\"{}\": {v:#indent$}", k)?; | ||
} | ||
_ => { | ||
write!(fmt, "{indent_str}\"{}\": {}", k, v)?; | ||
} | ||
} | ||
} else { | ||
write!(fmt, "{indent_str}\"{}\": {}", k, v)?; | ||
} | ||
} | ||
|
||
write!(fmt, "{}}}", if !first { " " } else { "" }) | ||
if fmt.alternate() && !self.inner.is_empty() { | ||
write!(fmt, "\n{indent_str}}}") | ||
} else { | ||
write!(fmt, "{}}}", if !first { " " } else { "" }) | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be made a bit more concise with:
(ditto elsewhere with a similar pattern)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this to use unwrap_or to be more concise