Skip to content

Commit e0c5ca5

Browse files
authored
RUST-2019 Respect pretty printing flag for Document and Bson (#501)
RUST-2019 Respect pretty printing for Bson and Document
1 parent 31c92ad commit e0c5ca5

File tree

3 files changed

+200
-13
lines changed

3 files changed

+200
-13
lines changed

src/bson.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,48 @@ impl Display for Bson {
134134
Bson::Array(ref vec) => {
135135
fmt.write_str("[")?;
136136

137+
let indent = fmt.width().unwrap_or(2);
138+
let indent_str = " ".repeat(indent);
139+
137140
let mut first = true;
138141
for bson in vec {
139142
if !first {
140143
fmt.write_str(", ")?;
141144
}
142-
143-
write!(fmt, "{}", bson)?;
145+
if fmt.alternate() {
146+
write!(fmt, "\n{indent_str}")?;
147+
match bson {
148+
Bson::Array(_arr) => {
149+
let new_indent = indent + 2;
150+
write!(fmt, "{bson:#new_indent$}")?;
151+
}
152+
Bson::Document(ref doc) => {
153+
let new_indent = indent + 2;
154+
write!(fmt, "{doc:#new_indent$}")?;
155+
}
156+
_ => {
157+
write!(fmt, "{}", bson)?;
158+
}
159+
}
160+
} else {
161+
write!(fmt, "{}", bson)?;
162+
}
144163
first = false;
145164
}
146-
147-
fmt.write_str("]")
165+
if fmt.alternate() && !vec.is_empty() {
166+
let closing_bracket_indent_str = " ".repeat(indent - 2);
167+
write!(fmt, "\n{closing_bracket_indent_str}]")
168+
} else {
169+
fmt.write_str("]")
170+
}
171+
}
172+
Bson::Document(ref doc) => {
173+
if fmt.alternate() {
174+
write!(fmt, "{doc:#}")
175+
} else {
176+
write!(fmt, "{doc}")
177+
}
148178
}
149-
Bson::Document(ref doc) => write!(fmt, "{}", doc),
150179
Bson::Boolean(b) => write!(fmt, "{}", b),
151180
Bson::Null => write!(fmt, "null"),
152181
Bson::RegularExpression(ref x) => write!(fmt, "{}", x),

src/document.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,54 @@ impl Hash for Document {
8080

8181
impl Display for Document {
8282
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
83-
fmt.write_str("{")?;
83+
let indent = fmt.width().unwrap_or(2);
84+
let indent_str = " ".repeat(indent);
85+
86+
write!(fmt, "{{")?;
87+
if fmt.alternate() && !self.inner.is_empty() {
88+
fmt.write_str("\n")?;
89+
}
8490

8591
let mut first = true;
8692
for (k, v) in self {
87-
if first {
88-
first = false;
89-
fmt.write_str(" ")?;
90-
} else {
91-
fmt.write_str(", ")?;
93+
if !fmt.alternate() {
94+
if first {
95+
first = false;
96+
write!(fmt, " ")?;
97+
} else {
98+
fmt.write_str(", ")?;
99+
}
100+
write!(fmt, "\"{}\": {}", k, v)?;
92101
}
93102

94-
write!(fmt, "\"{}\": {}", k, v)?;
103+
if fmt.alternate() {
104+
if first {
105+
first = false;
106+
} else {
107+
fmt.write_str(",\n")?;
108+
}
109+
match v {
110+
Bson::Document(ref doc) => {
111+
let new_indent = indent + 2;
112+
write!(fmt, "{indent_str}\"{}\": {doc:#new_indent$}", k)?;
113+
}
114+
Bson::Array(_arr) => {
115+
let new_indent = indent + 2;
116+
write!(fmt, "{indent_str}\"{}\": {v:#new_indent$}", k)?;
117+
}
118+
_ => {
119+
write!(fmt, "{indent_str}\"{}\": {}", k, v)?;
120+
}
121+
}
122+
}
95123
}
96124

97-
write!(fmt, "{}}}", if !first { " " } else { "" })
125+
let closing_bracket_indent_str = " ".repeat(indent - 2);
126+
if fmt.alternate() && !self.inner.is_empty() {
127+
write!(fmt, "\n{closing_bracket_indent_str}}}")
128+
} else {
129+
write!(fmt, "{}}}", if !first { " " } else { "" })
130+
}
98131
}
99132
}
100133

src/tests/modules/document.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,128 @@ fn extend() {
245245
},
246246
);
247247
}
248+
249+
#[test]
250+
fn test_display_empty_doc() {
251+
let empty_expectation = "{}";
252+
let doc = doc! {};
253+
let doc_display = format!("{doc}");
254+
assert_eq!(empty_expectation, doc_display);
255+
256+
let doc_display_pretty = format!("{doc:#}");
257+
assert_eq!(empty_expectation, doc_display_pretty);
258+
}
259+
260+
#[test]
261+
fn test_display_doc() {
262+
let doc = doc! {
263+
"hello": "world"
264+
};
265+
266+
let doc_display_expectation = "{ \"hello\": \"world\" }";
267+
assert_eq!(doc_display_expectation, format!("{doc}"));
268+
269+
let doc_display_pretty_expectation = r#"{
270+
"hello": "world"
271+
}"#;
272+
assert_eq!(doc_display_pretty_expectation, format!("{doc:#}"));
273+
}
274+
275+
#[test]
276+
fn test_display_nested_doc() {
277+
let doc = doc! {
278+
"hello": {
279+
"hello": 2
280+
}
281+
};
282+
283+
let doc_display_expectation = "{ \"hello\": { \"hello\": 2 } }";
284+
assert_eq!(doc_display_expectation, format!("{doc}"));
285+
286+
let doc_display_pretty_expectation = r#"{
287+
"hello": {
288+
"hello": 2
289+
}
290+
}"#;
291+
let formatted = format!("{doc:#}");
292+
assert_eq!(doc_display_pretty_expectation, formatted);
293+
}
294+
295+
#[test]
296+
fn test_display_doc_with_array() {
297+
let doc = doc! {
298+
"hello": [1, 2, 3]
299+
};
300+
301+
let doc_display_expectation = "{ \"hello\": [1, 2, 3] }";
302+
assert_eq!(doc_display_expectation, format!("{doc}"));
303+
304+
let doc_display_pretty_expectation = r#"{
305+
"hello": [
306+
1,
307+
2,
308+
3
309+
]
310+
}"#;
311+
let formatted = format!("{doc:#}");
312+
assert_eq!(doc_display_pretty_expectation, formatted);
313+
314+
let nested_array_doc = doc! {
315+
"a": [1, [1, 2]]
316+
};
317+
318+
let expectation = r#"{
319+
"a": [
320+
1,
321+
[
322+
1,
323+
2
324+
]
325+
]
326+
}"#;
327+
assert_eq!(expectation, format!("{nested_array_doc:#}"));
328+
}
329+
330+
#[test]
331+
fn test_pretty_printing() {
332+
let d = doc! { "hello": "world!", "world": "hello", "key": "val" };
333+
let expected = r#"{ "hello": "world!", "world": "hello", "key": "val" }"#;
334+
let formatted = format!("{d}");
335+
assert_eq!(
336+
expected, formatted,
337+
"expected:\n{expected}\ngot:\n{formatted}"
338+
);
339+
340+
let d = doc! { "hello": "world!", "nested": { "key": "val", "double": { "a": "thing" } } };
341+
let expected = r#"{
342+
"hello": "world!",
343+
"nested": {
344+
"key": "val",
345+
"double": {
346+
"a": "thing"
347+
}
348+
}
349+
}"#;
350+
let formatted = format!("{d:#}");
351+
assert_eq!(
352+
expected, formatted,
353+
"expected:\n{expected}\ngot:\n{formatted}"
354+
);
355+
356+
let d =
357+
doc! { "hello": "world!", "nested": { "key": "val", "double": { "a": [1, 2], "c": "d"} } };
358+
let expected = r#"{
359+
"hello": "world!",
360+
"nested": {
361+
"key": "val",
362+
"double": {
363+
"a": [
364+
1,
365+
2
366+
],
367+
"c": "d"
368+
}
369+
}
370+
}"#;
371+
assert_eq!(expected, format!("{d:#}"));
372+
}

0 commit comments

Comments
 (0)