-
Notifications
You must be signed in to change notification settings - Fork 136
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
Conversation
You'll need some additional logic in there to handle nested documents, those should have the flag passed in and then be indented for the nesting.
I do think these need some tests. Happily, As a side note, we usually do development on our own forks of the repository so the main repo doesn't accumulate dev branches (and to make accidental main-branch-clobbering less likely, not that yours truly would have done that on his first day). No need to switch for this PR, just to know for the future. |
@abr-egn Added some logic for nested documents and added some tests, let me know what you think. also thought it would be good to add some logics for arrays as well:
noted about the forks, will adhere to that in the future |
Good thought on the array formatting! It looks like there's some extra spacing on initial indented lines - I tried this test: #[test]
fn test_pretty_printing() {
let d = doc! { "hello": "world!", "world": "hello", "key": "val" };
let expected = r#"{ "hello": "world!", "world": "hello", "key": "val" }"#;
let formatted = format!("{d}");
assert_eq!(
expected, formatted,
"expected:\n{expected}\ngot:\n{formatted}"
);
let d = doc! { "hello": "world!", "nested": { "key": "val", "double": { "a": "thing" } } };
let expected = r#"{
"hello": "world",
"nested": {
"key": "val",
"double": {
"a": "thing"
}
}
}"#;
let formatted = format!("{d:#}");
assert_eq!(
expected, formatted,
"expected:\n{expected}\ngot:\n{formatted}"
);
} and the output was
|
@abr-egn thanks for pointing that out, I fixed that error and added some more tests which cover that case |
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.
LGTM! Tagging in Isabel.
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.
just a few style suggestions and a question!
src/bson.rs
Outdated
let indent_str; | ||
if let Some(width) = fmt.width() { | ||
indent_str = " ".repeat(width); | ||
} else { | ||
indent_str = "".to_string(); | ||
} |
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:
let indent_str = if let Some(width) = fmt.width() {
" ".repeat(width)
} else {
"".to_string()
};
(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
src/document.rs
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
suggest using unwrap_or
here:
let mut indent = fmt.width().unwrap_or(0);
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.
Thanks for bringing this method to my attention!
src/document.rs
Outdated
} | ||
match v { | ||
Bson::Document(ref doc) => { | ||
indent += 1; |
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.
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 comment
The 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
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.
lgtm!
RUST-2019
Didn't add a unit test because
Display
outputs to the console, let me know if this should be tested thoughI did test manually with this code
which outputted
did the same test switching to_document instead of to_bson and got the same output.