diff --git a/src/bson.rs b/src/bson.rs index 5acfbf1f..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), @@ -984,6 +974,12 @@ pub struct Timestamp { pub increment: u32, } +impl Display for Timestamp { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "Timestamp({}, {})", self.time, self.increment) + } +} + impl Timestamp { pub(crate) fn to_le_i64(self) -> i64 { let upper = (self.time.to_le() as u64) << 32; @@ -1018,6 +1014,12 @@ pub struct Regex { pub options: String, } +impl Display for Regex { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "/{}/{}", self.pattern, self.options) + } +} + /// Represents a BSON code with scope value. #[derive(Debug, Clone, PartialEq)] pub struct JavaScriptCodeWithScope { @@ -1025,6 +1027,12 @@ pub struct JavaScriptCodeWithScope { pub scope: Document, } +impl Display for JavaScriptCodeWithScope { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.write_str(&self.code) + } +} + /// Represents a BSON binary value. #[derive(Debug, Clone, PartialEq)] pub struct Binary { @@ -1035,6 +1043,17 @@ pub struct Binary { pub bytes: Vec, } +impl Display for Binary { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!( + fmt, + "Binary({:#x}, {})", + u8::from(self.subtype), + base64::encode(&self.bytes) + ) + } +} + 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..2f48863c 100644 --- a/src/tests/modules/bson.rs +++ b/src/tests/modules/bson.rs @@ -60,6 +60,52 @@ fn bson_default() { assert_eq!(bson1, Bson::Null); } +#[test] +fn test_display_timestamp_type() { + let x = Timestamp { + time: 100, + increment: 200, + }; + let output = "Timestamp(100, 200)"; + assert_eq!(format!("{}", x), output); + assert_eq!(format!("{}", Bson::from(x)), output); +} + +#[test] +fn test_display_regex_type() { + let x = Regex { + pattern: String::from("pattern"), + options: String::from("options"), + }; + let output = "/pattern/options"; + assert_eq!(format!("{}", x), output); + assert_eq!(format!("{}", Bson::from(x)), output); +} + +#[test] +fn test_display_jscodewithcontext_type() { + let x = JavaScriptCodeWithScope { + code: String::from("code"), + scope: doc! {"x": 2}, + }; + let output = "code"; + assert_eq!(format!("{}", x), output); + assert_eq!(format!("{}", Bson::from(x)), output); +} + +#[test] +fn test_display_binary_type() { + let encoded_bytes = "aGVsbG8gd29ybGQ="; + let bytes = base64::decode(encoded_bytes).unwrap(); + let x = Binary { + subtype: BinarySubtype::Generic, + bytes, + }; + let output = format!("Binary(0x0, {})", encoded_bytes); + assert_eq!(format!("{}", x), output); + assert_eq!(format!("{}", Bson::from(x)), output); +} + #[test] fn document_default() { let _guard = LOCK.run_concurrently();