mongodb / bson-rust

Encoding and decoding support for BSON in Rust
MIT License
405 stars 134 forks source link

RUST-2019 Formatter for `Bson` ignores pretty printing `{:#}` #491

Closed mahmoud-moursy closed 1 month ago

mahmoud-moursy commented 3 months ago

Versions/Environment

  1. What version of Rust are you using?

rustc 1.82.0-nightly (506052d49 2024-08-16)

  1. What operating system are you using?

Linux

  1. What versions of the driver and its dependencies are you using? (Run cargo pkgid mongodb & cargo pkgid bson)
  1. What version of MongoDB are you using? (Check with the MongoDB shell using db.version()) N/A
  2. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)? N/A

Describe the bug

A clear and concise description of what the bug is.

BE SPECIFIC:

Bson formatting outputs the following:

{
  "key": "Hello world!"
}

What actually happens:

Bson formatting outputs the following:

{ "key": "Hello world!" }

If you implemented Display manually, then you need to manually check if the user passed in pretty-printing as a parameter and then change your formatting based on that.

You can check it using formatter.alternate() in your Display implementation.

let bson_document = bson::to_document(&my_hashmap);

// Incorrect formatting: println!("{bson_document:#}");

// This, however, is correctly formatted: println!("{bson_document:#?}"); // It is probably correctly formatted because the derive(Debug) implementation was used.



Thank you.
abr-egn commented 3 months ago

Thank you for the well-written ticket!

You're quite right that we're not respecting that flag currently; I've filed RUST-2019 to track that. This won't be high priority in our plan for this quarter but we'd certainly welcome a contribution to add it 🙂