From 9f6060cea7af5bf96ab36ce795e16d8b68dbdf4c Mon Sep 17 00:00:00 2001 From: Jada Date: Wed, 16 Oct 2024 17:12:32 -0400 Subject: [PATCH 1/7] RUST-2019 Respect pretty printing for Bson and Document --- src/bson.rs | 2 +- src/document.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/bson.rs b/src/bson.rs index 01ce1bb3..a0d7075e 100644 --- a/src/bson.rs +++ b/src/bson.rs @@ -146,7 +146,7 @@ impl Display for Bson { fmt.write_str("]") } - Bson::Document(ref doc) => write!(fmt, "{}", doc), + Bson::Document(ref doc) => if fmt.alternate() {write!(fmt, "{doc:#}")} else {write!(fmt, "{}", doc)}, Bson::Boolean(b) => write!(fmt, "{}", b), Bson::Null => write!(fmt, "null"), Bson::RegularExpression(ref x) => write!(fmt, "{}", x), diff --git a/src/document.rs b/src/document.rs index 3a5f189f..61469ada 100644 --- a/src/document.rs +++ b/src/document.rs @@ -80,7 +80,7 @@ impl Hash for Document { impl Display for Document { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { - fmt.write_str("{")?; + fmt.write_str(if fmt.alternate() { "{\n" } else {"{"})?; let mut first = true; for (k, v) in self { @@ -88,13 +88,20 @@ impl Display for Document { first = false; fmt.write_str(" ")?; } else { - fmt.write_str(", ")?; + fmt.write_str(if fmt.alternate() { ", \n " } else {", "})?; } write!(fmt, "\"{}\": {}", k, v)?; } - write!(fmt, "{}}}", if !first { " " } else { "" }) + if fmt.alternate() { + write!(fmt, "\n}}") + } + else { + write!(fmt, "{}}}", if !first { " " } else { "" }) + + } + } } From 1368c16a3ac5b6b5d03cd5fb25814e0b22af7df1 Mon Sep 17 00:00:00 2001 From: Jada Date: Wed, 16 Oct 2024 17:22:10 -0400 Subject: [PATCH 2/7] fix linting --- src/bson.rs | 8 +++++++- src/document.rs | 9 +++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/bson.rs b/src/bson.rs index a0d7075e..f343a483 100644 --- a/src/bson.rs +++ b/src/bson.rs @@ -146,7 +146,13 @@ impl Display for Bson { fmt.write_str("]") } - Bson::Document(ref doc) => if fmt.alternate() {write!(fmt, "{doc:#}")} else {write!(fmt, "{}", doc)}, + Bson::Document(ref doc) => { + if fmt.alternate() { + write!(fmt, "{doc:#}") + } else { + write!(fmt, "{}", doc) + } + } Bson::Boolean(b) => write!(fmt, "{}", b), Bson::Null => write!(fmt, "null"), Bson::RegularExpression(ref x) => write!(fmt, "{}", x), diff --git a/src/document.rs b/src/document.rs index 61469ada..ee24365f 100644 --- a/src/document.rs +++ b/src/document.rs @@ -80,7 +80,7 @@ impl Hash for Document { impl Display for Document { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { - fmt.write_str(if fmt.alternate() { "{\n" } else {"{"})?; + fmt.write_str(if fmt.alternate() { "{\n" } else { "{" })?; let mut first = true; for (k, v) in self { @@ -88,7 +88,7 @@ impl Display for Document { first = false; fmt.write_str(" ")?; } else { - fmt.write_str(if fmt.alternate() { ", \n " } else {", "})?; + fmt.write_str(if fmt.alternate() { ", \n " } else { ", " })?; } write!(fmt, "\"{}\": {}", k, v)?; @@ -96,12 +96,9 @@ impl Display for Document { if fmt.alternate() { write!(fmt, "\n}}") - } - else { + } else { write!(fmt, "{}}}", if !first { " " } else { "" }) - } - } } From a53ac289243c4de229d523861a9f77faae16d1f9 Mon Sep 17 00:00:00 2001 From: Jada Date: Wed, 16 Oct 2024 17:40:25 -0400 Subject: [PATCH 3/7] consider empty document --- src/document.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/document.rs b/src/document.rs index ee24365f..87a3f69b 100644 --- a/src/document.rs +++ b/src/document.rs @@ -80,7 +80,10 @@ impl Hash for Document { impl Display for Document { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { - fmt.write_str(if fmt.alternate() { "{\n" } else { "{" })?; + fmt.write_str("{")?; + if fmt.alternate() && self.inner.len() > 0 { + fmt.write_str("\n")?; + } let mut first = true; for (k, v) in self { @@ -94,7 +97,7 @@ impl Display for Document { write!(fmt, "\"{}\": {}", k, v)?; } - if fmt.alternate() { + if fmt.alternate() && self.inner.len() > 0 { write!(fmt, "\n}}") } else { write!(fmt, "{}}}", if !first { " " } else { "" }) From e5bf23e5df23fa522cf7467b1ee1e98c5006d610 Mon Sep 17 00:00:00 2001 From: Jada Date: Wed, 16 Oct 2024 22:50:47 -0400 Subject: [PATCH 4/7] fix clippy --- src/document.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/document.rs b/src/document.rs index 87a3f69b..347e2efe 100644 --- a/src/document.rs +++ b/src/document.rs @@ -81,7 +81,7 @@ impl Hash for Document { impl Display for Document { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { fmt.write_str("{")?; - if fmt.alternate() && self.inner.len() > 0 { + if fmt.alternate() && self.inner.is_empty() { fmt.write_str("\n")?; } @@ -97,7 +97,7 @@ impl Display for Document { write!(fmt, "\"{}\": {}", k, v)?; } - if fmt.alternate() && self.inner.len() > 0 { + if fmt.alternate() && self.inner.is_empty() { write!(fmt, "\n}}") } else { write!(fmt, "{}}}", if !first { " " } else { "" }) From d18f06ce3e2dde9b8d3fcf2585534d063bb43ef2 Mon Sep 17 00:00:00 2001 From: Jada Date: Fri, 18 Oct 2024 15:30:02 -0400 Subject: [PATCH 5/7] Add logic for formatting nested documents and arrays --- src/bson.rs | 23 ++++++++++++---- src/document.rs | 40 +++++++++++++++++++++++---- src/tests/modules/document.rs | 52 +++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 11 deletions(-) diff --git a/src/bson.rs b/src/bson.rs index f343a483..13f9d377 100644 --- a/src/bson.rs +++ b/src/bson.rs @@ -134,23 +134,36 @@ 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(); + } + 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) + write!(fmt, "{doc}") } } Bson::Boolean(b) => write!(fmt, "{}", b), diff --git a/src/document.rs b/src/document.rs index 347e2efe..eed73534 100644 --- a/src/document.rs +++ b/src/document.rs @@ -80,8 +80,14 @@ impl Hash for Document { impl Display for Document { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { - fmt.write_str("{")?; - if fmt.alternate() && self.inner.is_empty() { + 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")?; } @@ -89,16 +95,38 @@ impl Display for Document { for (k, v) in self { if first { first = false; - fmt.write_str(" ")?; + write!(fmt, "{indent_str} ")?; } else { 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; + } + match v { + Bson::Document(ref doc) => { + indent += 1; + 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)?; + } } - if fmt.alternate() && self.inner.is_empty() { - write!(fmt, "\n}}") + if fmt.alternate() && !self.inner.is_empty() { + write!(fmt, "\n{indent_str}}}") } else { write!(fmt, "{}}}", if !first { " " } else { "" }) } diff --git a/src/tests/modules/document.rs b/src/tests/modules/document.rs index 85fc62ff..7276d630 100644 --- a/src/tests/modules/document.rs +++ b/src/tests/modules/document.rs @@ -245,3 +245,55 @@ fn extend() { }, ); } + +#[test] +fn test_display_empty_doc() { + let empty_expectation = "{}".to_string(); + 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\" }".to_string(); + assert_eq!(doc_display_expectation, format!("{doc}")); + + let doc_display_pretty_expectation = "{\n \"hello\": \"world\"\n}"; + 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 } }".to_string(); + assert_eq!(doc_display_expectation, format!("{doc}")); + + let doc_display_pretty_expectation = "{\n \"hello\": {\n \"hello\": 2\n }\n}"; + assert_eq!(doc_display_pretty_expectation, format!("{doc:#}")); +} + +#[test] +fn test_display_doc_with_array() { + let doc = doc! { + "hello": [1, 2, 3] + }; + + let doc_display_expectation = "{ \"hello\": [1, 2, 3] }".to_string(); + assert_eq!(doc_display_expectation, format!("{doc}")); + + let doc_display_pretty_expectation = "{\n \"hello\": [\n 1, \n 2, \n 3\n ]\n}"; + assert_eq!(doc_display_pretty_expectation, format!("{doc:#}")); +} From 96a03bf23dfb4a07331e028f3ba92124b6e227a7 Mon Sep 17 00:00:00 2001 From: Jada Date: Mon, 21 Oct 2024 11:45:53 -0400 Subject: [PATCH 6/7] fix indent error and add more tests --- src/document.rs | 4 +- src/tests/modules/document.rs | 76 ++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/src/document.rs b/src/document.rs index eed73534..a48e227c 100644 --- a/src/document.rs +++ b/src/document.rs @@ -95,9 +95,9 @@ impl Display for Document { for (k, v) in self { if first { first = false; - write!(fmt, "{indent_str} ")?; + write!(fmt, " ")?; } else { - fmt.write_str(if fmt.alternate() { ", \n " } else { ", " })?; + fmt.write_str(if fmt.alternate() { ",\n " } else { ", " })?; } if fmt.alternate() { diff --git a/src/tests/modules/document.rs b/src/tests/modules/document.rs index 7276d630..390d5ef4 100644 --- a/src/tests/modules/document.rs +++ b/src/tests/modules/document.rs @@ -248,7 +248,7 @@ fn extend() { #[test] fn test_display_empty_doc() { - let empty_expectation = "{}".to_string(); + let empty_expectation = "{}"; let doc = doc! {}; let doc_display = format!("{doc}"); assert_eq!(empty_expectation, doc_display); @@ -263,10 +263,12 @@ fn test_display_doc() { "hello": "world" }; - let doc_display_expectation = "{ \"hello\": \"world\" }".to_string(); + let doc_display_expectation = "{ \"hello\": \"world\" }"; assert_eq!(doc_display_expectation, format!("{doc}")); - let doc_display_pretty_expectation = "{\n \"hello\": \"world\"\n}"; + let doc_display_pretty_expectation = r#"{ + "hello": "world" +}"#; assert_eq!(doc_display_pretty_expectation, format!("{doc:#}")); } @@ -278,11 +280,16 @@ fn test_display_nested_doc() { } }; - let doc_display_expectation = "{ \"hello\": { \"hello\": 2 } }".to_string(); + let doc_display_expectation = "{ \"hello\": { \"hello\": 2 } }"; assert_eq!(doc_display_expectation, format!("{doc}")); - let doc_display_pretty_expectation = "{\n \"hello\": {\n \"hello\": 2\n }\n}"; - assert_eq!(doc_display_pretty_expectation, format!("{doc:#}")); + let doc_display_pretty_expectation = r#"{ + "hello": { + "hello": 2 + } +}"#; + let formatted = format!("{doc:#}"); + assert_eq!(doc_display_pretty_expectation, formatted); } #[test] @@ -291,9 +298,60 @@ fn test_display_doc_with_array() { "hello": [1, 2, 3] }; - let doc_display_expectation = "{ \"hello\": [1, 2, 3] }".to_string(); + let doc_display_expectation = "{ \"hello\": [1, 2, 3] }"; assert_eq!(doc_display_expectation, format!("{doc}")); - let doc_display_pretty_expectation = "{\n \"hello\": [\n 1, \n 2, \n 3\n ]\n}"; - assert_eq!(doc_display_pretty_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:#}")); } From 42516581e589d3f58a95b0a5134c744f8fe4fc7e Mon Sep 17 00:00:00 2001 From: Jada Date: Tue, 22 Oct 2024 12:37:34 -0400 Subject: [PATCH 7/7] add style suggestions and increase indent to 2 --- src/bson.rs | 26 +++++++++----- src/document.rs | 42 +++++++++++----------- src/tests/modules/document.rs | 65 +++++++++++++++++++++-------------- 3 files changed, 78 insertions(+), 55 deletions(-) diff --git a/src/bson.rs b/src/bson.rs index 13f9d377..9c732c50 100644 --- a/src/bson.rs +++ b/src/bson.rs @@ -134,12 +134,8 @@ 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(); - } + let indent = fmt.width().unwrap_or(2); + let indent_str = " ".repeat(indent); let mut first = true; for bson in vec { @@ -147,14 +143,28 @@ impl Display for Bson { fmt.write_str(", ")?; } if fmt.alternate() { - write!(fmt, "\n {indent_str}{}", bson)?; + write!(fmt, "\n{indent_str}")?; + match bson { + Bson::Array(_arr) => { + let new_indent = indent + 2; + write!(fmt, "{bson:#new_indent$}")?; + } + Bson::Document(ref doc) => { + let new_indent = indent + 2; + write!(fmt, "{doc:#new_indent$}")?; + } + _ => { + write!(fmt, "{}", bson)?; + } + } } else { write!(fmt, "{}", bson)?; } first = false; } if fmt.alternate() && !vec.is_empty() { - write!(fmt, "\n{indent_str}]") + let closing_bracket_indent_str = " ".repeat(indent - 2); + write!(fmt, "\n{closing_bracket_indent_str}]") } else { fmt.write_str("]") } diff --git a/src/document.rs b/src/document.rs index a48e227c..18b57851 100644 --- a/src/document.rs +++ b/src/document.rs @@ -80,12 +80,9 @@ impl Hash for Document { impl Display for Document { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { - let indent_str; - if let Some(width) = fmt.width() { - indent_str = " ".repeat(width); - } else { - indent_str = "".to_string(); - } + let indent = fmt.width().unwrap_or(2); + let indent_str = " ".repeat(indent); + write!(fmt, "{{")?; if fmt.alternate() && !self.inner.is_empty() { fmt.write_str("\n")?; @@ -93,40 +90,41 @@ impl Display for Document { let mut first = true; for (k, v) in self { - if first { - first = false; - write!(fmt, " ")?; - } else { - fmt.write_str(if fmt.alternate() { ",\n " } else { ", " })?; + if !fmt.alternate() { + if first { + first = false; + write!(fmt, " ")?; + } else { + fmt.write_str(", ")?; + } + write!(fmt, "\"{}\": {}", k, v)?; } if fmt.alternate() { - let mut indent; - if let Some(width) = fmt.width() { - indent = width; + if first { + first = false; } else { - indent = 0; + fmt.write_str(",\n")?; } match v { Bson::Document(ref doc) => { - indent += 1; - write!(fmt, "{indent_str}\"{}\": {doc:#indent$}", k)?; + let new_indent = indent + 2; + write!(fmt, "{indent_str}\"{}\": {doc:#new_indent$}", k)?; } Bson::Array(_arr) => { - indent += 1; - write!(fmt, "{indent_str}\"{}\": {v:#indent$}", k)?; + let new_indent = indent + 2; + write!(fmt, "{indent_str}\"{}\": {v:#new_indent$}", k)?; } _ => { write!(fmt, "{indent_str}\"{}\": {}", k, v)?; } } - } else { - write!(fmt, "{indent_str}\"{}\": {}", k, v)?; } } + let closing_bracket_indent_str = " ".repeat(indent - 2); if fmt.alternate() && !self.inner.is_empty() { - write!(fmt, "\n{indent_str}}}") + write!(fmt, "\n{closing_bracket_indent_str}}}") } else { write!(fmt, "{}}}", if !first { " " } else { "" }) } diff --git a/src/tests/modules/document.rs b/src/tests/modules/document.rs index 390d5ef4..e8be3199 100644 --- a/src/tests/modules/document.rs +++ b/src/tests/modules/document.rs @@ -267,7 +267,7 @@ fn test_display_doc() { assert_eq!(doc_display_expectation, format!("{doc}")); let doc_display_pretty_expectation = r#"{ - "hello": "world" + "hello": "world" }"#; assert_eq!(doc_display_pretty_expectation, format!("{doc:#}")); } @@ -284,9 +284,9 @@ fn test_display_nested_doc() { assert_eq!(doc_display_expectation, format!("{doc}")); let doc_display_pretty_expectation = r#"{ - "hello": { - "hello": 2 - } + "hello": { + "hello": 2 + } }"#; let formatted = format!("{doc:#}"); assert_eq!(doc_display_pretty_expectation, formatted); @@ -302,14 +302,29 @@ fn test_display_doc_with_array() { assert_eq!(doc_display_expectation, format!("{doc}")); let doc_display_pretty_expectation = r#"{ - "hello": [ - 1, - 2, - 3 - ] + "hello": [ + 1, + 2, + 3 + ] }"#; let formatted = format!("{doc:#}"); assert_eq!(doc_display_pretty_expectation, formatted); + + let nested_array_doc = doc! { + "a": [1, [1, 2]] + }; + + let expectation = r#"{ + "a": [ + 1, + [ + 1, + 2 + ] + ] +}"#; + assert_eq!(expectation, format!("{nested_array_doc:#}")); } #[test] @@ -324,13 +339,13 @@ fn test_pretty_printing() { let d = doc! { "hello": "world!", "nested": { "key": "val", "double": { "a": "thing" } } }; let expected = r#"{ - "hello": "world!", - "nested": { - "key": "val", - "double": { - "a": "thing" + "hello": "world!", + "nested": { + "key": "val", + "double": { + "a": "thing" + } } - } }"#; let formatted = format!("{d:#}"); assert_eq!( @@ -341,17 +356,17 @@ fn test_pretty_printing() { 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" + "hello": "world!", + "nested": { + "key": "val", + "double": { + "a": [ + 1, + 2 + ], + "c": "d" + } } - } }"#; assert_eq!(expected, format!("{d:#}")); }