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

RUST-2019 Respect pretty printing flag for Document and Bson #501

Merged
merged 7 commits into from
Oct 22, 2024

Conversation

jadalilleboe
Copy link
Contributor

RUST-2019

Didn't add a unit test because Display outputs to the console, let me know if this should be tested though
I did test manually with this code

let my_hashmap = HashMap::from([
        ("hello", "world!"), ("world", "hello"), ("key", "val")
      ]);
      
    let bson_document = to_bson(&my_hashmap).unwrap();
    println!("{bson_document}");
    println!("{bson_document:#}");

    let hm: HashMap<&str, &str> = HashMap::from([]);
    let md = to_document(&hm).unwrap();
    println!("{md}");
    println!("{md:#}");

which outputted

{ "hello": "world!", "world": "hello", "key": "val" }
{
 "hello": "world!", 
 "world": "hello", 
 "key": "val"
}
{}
{}

did the same test switching to_document instead of to_bson and got the same output.

@jadalilleboe jadalilleboe requested a review from abr-egn October 16, 2024 21:41
@abr-egn
Copy link
Contributor

abr-egn commented Oct 17, 2024

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.

Didn't add a unit test because Display outputs to the console, let me know if this should be tested though

I do think these need some tests. Happily, Display and Debug are actually agnostic to where they're being written to :) You can use the format! macro to get a String output. Also, you can construct a BSON Document more conveniently with the doc! macro.

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.

@jadalilleboe
Copy link
Contributor Author

@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:

{
 "hello": [
  1, 
  2, 
  3
 ]
}

noted about the forks, will adhere to that in the future

@abr-egn
Copy link
Contributor

abr-egn commented Oct 21, 2024

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

assertion `left == right` failed: expected:
{
 "hello": "world",
 "nested": {
  "key": "val",
  "double": {
   "a": "thing"
  }
 }
}
got:
{
 "hello": "world!", 
 "nested": {
   "key": "val", 
  "double": {
     "a": "thing"
  }
 }
}
  left: "{\n \"hello\": \"world\",\n \"nested\": {\n  \"key\": \"val\",\n  \"double\": {\n   \"a\": \"thing\"\n  }\n }\n}"
 right: "{\n \"hello\": \"world!\", \n \"nested\": {\n   \"key\": \"val\", \n  \"double\": {\n     \"a\": \"thing\"\n  }\n }\n}"

@jadalilleboe
Copy link
Contributor Author

@abr-egn thanks for pointing that out, I fixed that error and added some more tests which cover that case

Copy link
Contributor

@abr-egn abr-egn left a 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.

@jadalilleboe jadalilleboe linked an issue Oct 21, 2024 that may be closed by this pull request
Copy link
Contributor

@isabelatkinson isabelatkinson left a 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
Comment on lines 137 to 142
let indent_str;
if let Some(width) = fmt.width() {
indent_str = " ".repeat(width);
} else {
indent_str = "".to_string();
}
Copy link
Contributor

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)

Copy link
Contributor Author

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
Comment on lines 104 to 109
let mut indent;
if let Some(width) = fmt.width() {
indent = width;
} else {
indent = 0;
}
Copy link
Contributor

@isabelatkinson isabelatkinson Oct 21, 2024

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);

Copy link
Contributor Author

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;
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@jadalilleboe jadalilleboe merged commit e0c5ca5 into main Oct 22, 2024
9 of 11 checks passed
@jadalilleboe jadalilleboe deleted the RUST-2019/respect-pretty-printing branch October 22, 2024 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RUST-2019 Formatter for Bson ignores pretty printing {:#}
3 participants