From 1ddf5778c3ae5dca4b0c75a0015fac1b95b56397 Mon Sep 17 00:00:00 2001 From: Andrew Witten Date: Mon, 13 Sep 2021 14:19:02 -0400 Subject: [PATCH 1/3] RUST-787 impl Display for various Bson types --- src/bson.rs | 28 ++++++++++++++++++++++++++++ src/tests/modules/bson.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/bson.rs b/src/bson.rs index 5acfbf1f..c488d5e2 100644 --- a/src/bson.rs +++ b/src/bson.rs @@ -984,6 +984,13 @@ pub struct Timestamp { pub increment: u32, } +impl Display for Timestamp { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let x: Bson = self.into(); + write!(fmt, "{}", x) + } +} + impl Timestamp { pub(crate) fn to_le_i64(self) -> i64 { let upper = (self.time.to_le() as u64) << 32; @@ -1018,6 +1025,13 @@ pub struct Regex { pub options: String, } +impl Display for Regex { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let x: Bson = self.into(); + write!(fmt, "{}", x) + } +} + /// Represents a BSON code with scope value. #[derive(Debug, Clone, PartialEq)] pub struct JavaScriptCodeWithScope { @@ -1025,6 +1039,13 @@ pub struct JavaScriptCodeWithScope { pub scope: Document, } +impl Display for JavaScriptCodeWithScope { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let x: Bson = self.into(); + write!(fmt, "{}", x) + } +} + /// Represents a BSON binary value. #[derive(Debug, Clone, PartialEq)] pub struct Binary { @@ -1035,6 +1056,13 @@ pub struct Binary { pub bytes: Vec, } +impl Display for Binary { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let x: Bson = self.into(); + write!(fmt, "{}", x) + } +} + impl Binary { fn from_extended_doc(doc: &Document) -> Option { let binary = doc.get_document("$binary").ok()?; diff --git a/src/tests/modules/bson.rs b/src/tests/modules/bson.rs index 333e1e89..a51b0ffb 100644 --- a/src/tests/modules/bson.rs +++ b/src/tests/modules/bson.rs @@ -60,6 +60,43 @@ fn bson_default() { assert_eq!(bson1, Bson::Null); } +#[test] +fn test_display_timestamp_type() { + let x = Timestamp { + time: 100, + increment: 200, + }; + assert_eq!(format!("{}", x), "Timestamp(100, 200)"); +} + +#[test] +fn test_display_regex_type() { + let x = Regex { + pattern: String::from("pattern"), + options: String::from("options"), + }; + assert_eq!(format!("{}", x), "/pattern/options"); +} + +#[test] +fn test_display_jscodewithcontext_type() { + let x = JavaScriptCodeWithScope { + code: String::from("code"), + scope: doc! {"x": 2}, + }; + assert_eq!(format!("{}", x), "code"); +} + +#[test] +fn test_display_binary_type() { + let bytes = base64::decode("aGVsbG8gd29ybGQ=").unwrap(); + let x = Binary { + subtype: BinarySubtype::Generic, + bytes, + }; + assert_eq!(format!("{}", x), "Binary(0x0, aGVsbG8gd29ybGQ=)"); +} + #[test] fn document_default() { let _guard = LOCK.run_concurrently(); From 87236c6c83f3e0ba7ee8e13985a9149d3b277a69 Mon Sep 17 00:00:00 2001 From: Andrew Witten Date: Mon, 13 Sep 2021 14:25:32 -0400 Subject: [PATCH 2/3] save encoded bytes in var --- src/tests/modules/bson.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/tests/modules/bson.rs b/src/tests/modules/bson.rs index a51b0ffb..6bca6fdf 100644 --- a/src/tests/modules/bson.rs +++ b/src/tests/modules/bson.rs @@ -89,12 +89,13 @@ fn test_display_jscodewithcontext_type() { #[test] fn test_display_binary_type() { - let bytes = base64::decode("aGVsbG8gd29ybGQ=").unwrap(); + let encoded_bytes = "aGVsbG8gd29ybGQ="; + let bytes = base64::decode(encoded_bytes).unwrap(); let x = Binary { subtype: BinarySubtype::Generic, bytes, }; - assert_eq!(format!("{}", x), "Binary(0x0, aGVsbG8gd29ybGQ=)"); + assert_eq!(format!("{}", x), format!("Binary(0x0, {})", encoded_bytes)); } #[test] From b74d5c35d7b1161f8fd22bc4a248db257796d26e Mon Sep 17 00:00:00 2001 From: Andrew Witten Date: Mon, 13 Sep 2021 22:32:10 -0400 Subject: [PATCH 3/3] move display impls to struct methods --- src/bson.rs | 33 ++++++++++++--------------------- src/tests/modules/bson.rs | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/bson.rs b/src/bson.rs index c488d5e2..78e709af 100644 --- a/src/bson.rs +++ b/src/bson.rs @@ -115,25 +115,15 @@ impl Display for Bson { Bson::Document(ref doc) => write!(fmt, "{}", doc), Bson::Boolean(b) => write!(fmt, "{}", b), Bson::Null => write!(fmt, "null"), - Bson::RegularExpression(Regex { - ref pattern, - ref options, - }) => write!(fmt, "/{}/{}", pattern, options), + Bson::RegularExpression(ref x) => write!(fmt, "{}", x), Bson::JavaScriptCode(ref code) | Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { ref code, .. }) => { fmt.write_str(code) } Bson::Int32(i) => write!(fmt, "{}", i), Bson::Int64(i) => write!(fmt, "{}", i), - Bson::Timestamp(Timestamp { time, increment }) => { - write!(fmt, "Timestamp({}, {})", time, increment) - } - Bson::Binary(Binary { subtype, ref bytes }) => write!( - fmt, - "Binary({:#x}, {})", - u8::from(subtype), - base64::encode(bytes) - ), + Bson::Timestamp(ref x) => write!(fmt, "{}", x), + Bson::Binary(ref x) => write!(fmt, "{}", x), Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id), Bson::DateTime(date_time) => write!(fmt, "DateTime(\"{}\")", date_time), Bson::Symbol(ref sym) => write!(fmt, "Symbol(\"{}\")", sym), @@ -986,8 +976,7 @@ pub struct Timestamp { impl Display for Timestamp { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let x: Bson = self.into(); - write!(fmt, "{}", x) + write!(fmt, "Timestamp({}, {})", self.time, self.increment) } } @@ -1027,8 +1016,7 @@ pub struct Regex { impl Display for Regex { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let x: Bson = self.into(); - write!(fmt, "{}", x) + write!(fmt, "/{}/{}", self.pattern, self.options) } } @@ -1041,8 +1029,7 @@ pub struct JavaScriptCodeWithScope { impl Display for JavaScriptCodeWithScope { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let x: Bson = self.into(); - write!(fmt, "{}", x) + fmt.write_str(&self.code) } } @@ -1058,8 +1045,12 @@ pub struct Binary { impl Display for Binary { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let x: Bson = self.into(); - write!(fmt, "{}", x) + write!( + fmt, + "Binary({:#x}, {})", + u8::from(self.subtype), + base64::encode(&self.bytes) + ) } } diff --git a/src/tests/modules/bson.rs b/src/tests/modules/bson.rs index 6bca6fdf..2f48863c 100644 --- a/src/tests/modules/bson.rs +++ b/src/tests/modules/bson.rs @@ -66,7 +66,9 @@ fn test_display_timestamp_type() { time: 100, increment: 200, }; - assert_eq!(format!("{}", x), "Timestamp(100, 200)"); + let output = "Timestamp(100, 200)"; + assert_eq!(format!("{}", x), output); + assert_eq!(format!("{}", Bson::from(x)), output); } #[test] @@ -75,7 +77,9 @@ fn test_display_regex_type() { pattern: String::from("pattern"), options: String::from("options"), }; - assert_eq!(format!("{}", x), "/pattern/options"); + let output = "/pattern/options"; + assert_eq!(format!("{}", x), output); + assert_eq!(format!("{}", Bson::from(x)), output); } #[test] @@ -84,7 +88,9 @@ fn test_display_jscodewithcontext_type() { code: String::from("code"), scope: doc! {"x": 2}, }; - assert_eq!(format!("{}", x), "code"); + let output = "code"; + assert_eq!(format!("{}", x), output); + assert_eq!(format!("{}", Bson::from(x)), output); } #[test] @@ -95,7 +101,9 @@ fn test_display_binary_type() { subtype: BinarySubtype::Generic, bytes, }; - assert_eq!(format!("{}", x), format!("Binary(0x0, {})", encoded_bytes)); + let output = format!("Binary(0x0, {})", encoded_bytes); + assert_eq!(format!("{}", x), output); + assert_eq!(format!("{}", Bson::from(x)), output); } #[test]