Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,38 @@ impl Display for Bson {
Bson::Array(ref vec) => {
fmt.write_str("[")?;

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


let mut first = true;
for bson in vec {
if !first {
fmt.write_str(", ")?;
}

write!(fmt, "{}", bson)?;
if fmt.alternate() {
write!(fmt, "\n {indent_str}{}", bson)?;
} else {
write!(fmt, "{}", bson)?;
}
first = false;
}

fmt.write_str("]")
if fmt.alternate() && !vec.is_empty() {
write!(fmt, "\n{indent_str}]")
} else {
fmt.write_str("]")
}
}
Bson::Document(ref doc) => {
if fmt.alternate() {
write!(fmt, "{doc:#}")
} else {
write!(fmt, "{doc}")
}
}
Bson::Document(ref doc) => write!(fmt, "{}", doc),
Bson::Boolean(b) => write!(fmt, "{}", b),
Bson::Null => write!(fmt, "null"),
Bson::RegularExpression(ref x) => write!(fmt, "{}", x),
Expand Down
45 changes: 40 additions & 5 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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!

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

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 { "" })
}
}
}

Expand Down
110 changes: 110 additions & 0 deletions src/tests/modules/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,113 @@ fn extend() {
},
);
}

#[test]
fn test_display_empty_doc() {
let empty_expectation = "{}";
let doc = doc! {};
let doc_display = format!("{doc}");
assert_eq!(empty_expectation, doc_display);

let doc_display_pretty = format!("{doc:#}");
assert_eq!(empty_expectation, doc_display_pretty);
}

#[test]
fn test_display_doc() {
let doc = doc! {
"hello": "world"
};

let doc_display_expectation = "{ \"hello\": \"world\" }";
assert_eq!(doc_display_expectation, format!("{doc}"));

let doc_display_pretty_expectation = r#"{
"hello": "world"
}"#;
assert_eq!(doc_display_pretty_expectation, format!("{doc:#}"));
}

#[test]
fn test_display_nested_doc() {
let doc = doc! {
"hello": {
"hello": 2
}
};

let doc_display_expectation = "{ \"hello\": { \"hello\": 2 } }";
assert_eq!(doc_display_expectation, format!("{doc}"));

let doc_display_pretty_expectation = r#"{
"hello": {
"hello": 2
}
}"#;
let formatted = format!("{doc:#}");
assert_eq!(doc_display_pretty_expectation, formatted);
}

#[test]
fn test_display_doc_with_array() {
let doc = doc! {
"hello": [1, 2, 3]
};

let doc_display_expectation = "{ \"hello\": [1, 2, 3] }";
assert_eq!(doc_display_expectation, format!("{doc}"));

let doc_display_pretty_expectation = r#"{
"hello": [
1,
2,
3
]
}"#;
let formatted = format!("{doc:#}");
assert_eq!(doc_display_pretty_expectation, formatted);
}

#[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}"
);

let d =
doc! { "hello": "world!", "nested": { "key": "val", "double": { "a": [1, 2], "c": "d"} } };
let expected = r#"{
"hello": "world!",
"nested": {
"key": "val",
"double": {
"a": [
1,
2
],
"c": "d"
}
}
}"#;
assert_eq!(expected, format!("{d:#}"));
}