Skip to content

Commit fcc7040

Browse files
committed
Revert "Revert "Merge branch 'master' of https://github.com/mongodb/bson-rust""
This reverts commit 1fff644. This puts back the commits that were accidentally reverted when the merge commit was reverted.
1 parent cd358b8 commit fcc7040

File tree

6 files changed

+92
-9
lines changed

6 files changed

+92
-9
lines changed

src/bson.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
//! BSON definition
2323
24-
use std::fmt::{self, Debug, Display};
24+
use std::fmt::{self, Debug, Display, Formatter};
2525

2626
use chrono::Datelike;
2727
use serde_json::{json, Value};
@@ -34,7 +34,7 @@ use crate::{
3434
};
3535

3636
/// Possible BSON value types.
37-
#[derive(Clone, Debug, PartialEq)]
37+
#[derive(Clone, PartialEq)]
3838
pub enum Bson {
3939
/// 64-bit binary floating point
4040
Double(f64),
@@ -146,6 +146,40 @@ impl Display for Bson {
146146
}
147147
}
148148

149+
impl Debug for Bson {
150+
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
151+
match *self {
152+
Bson::Double(f) => fmt.debug_tuple("Double").field(&f).finish(),
153+
Bson::String(ref s) => fmt.debug_tuple("String").field(s).finish(),
154+
Bson::Array(ref vec) => {
155+
write!(fmt, "Array(")?;
156+
Debug::fmt(vec, fmt)?;
157+
write!(fmt, ")")
158+
}
159+
Bson::Document(ref doc) => Debug::fmt(doc, fmt),
160+
Bson::Boolean(b) => fmt.debug_tuple("Boolean").field(&b).finish(),
161+
Bson::Null => write!(fmt, "Null"),
162+
Bson::RegularExpression(ref regex) => Debug::fmt(regex, fmt),
163+
Bson::JavaScriptCode(ref code) => {
164+
fmt.debug_tuple("JavaScriptCode").field(code).finish()
165+
}
166+
Bson::JavaScriptCodeWithScope(ref code) => Debug::fmt(code, fmt),
167+
Bson::Int32(i) => fmt.debug_tuple("Int32").field(&i).finish(),
168+
Bson::Int64(i) => fmt.debug_tuple("Int64").field(&i).finish(),
169+
Bson::Timestamp(ref t) => Debug::fmt(t, fmt),
170+
Bson::Binary(ref b) => Debug::fmt(b, fmt),
171+
Bson::ObjectId(ref id) => Debug::fmt(id, fmt),
172+
Bson::DateTime(ref date_time) => Debug::fmt(date_time, fmt),
173+
Bson::Symbol(ref sym) => fmt.debug_tuple("Symbol").field(sym).finish(),
174+
Bson::Decimal128(ref d) => Debug::fmt(d, fmt),
175+
Bson::Undefined => write!(fmt, "Undefined"),
176+
Bson::MinKey => write!(fmt, "MinKey"),
177+
Bson::MaxKey => write!(fmt, "MaxKey"),
178+
Bson::DbPointer(ref pointer) => Debug::fmt(pointer, fmt),
179+
}
180+
}
181+
}
182+
149183
impl From<f32> for Bson {
150184
fn from(a: f32) -> Bson {
151185
Bson::Double(a as f64)

src/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl crate::DateTime {
7070

7171
/// Returns a [`DateTime`] which corresponds to the current date and time.
7272
pub fn now() -> DateTime {
73-
Self::from_chrono(Utc::now())
73+
Self::from_system_time(SystemTime::now())
7474
}
7575

7676
#[cfg(not(feature = "chrono-0_4"))]

src/document.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ impl Display for Document {
9292
}
9393

9494
impl Debug for Document {
95-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
96-
write!(f, "Document({:?})", self.inner)
95+
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
96+
write!(fmt, "Document(")?;
97+
Debug::fmt(&self.inner, fmt)?;
98+
write!(fmt, ")")
9799
}
98100
}
99101

src/oid.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl ObjectId {
163163
}
164164

165165
/// Returns the raw byte representation of an ObjectId.
166-
pub fn bytes(&self) -> [u8; 12] {
166+
pub const fn bytes(&self) -> [u8; 12] {
167167
self.id
168168
}
169169

@@ -223,7 +223,7 @@ impl fmt::Display for ObjectId {
223223

224224
impl fmt::Debug for ObjectId {
225225
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
226-
f.write_str(&format!("ObjectId({})", self.to_hex()))
226+
f.debug_tuple("ObjectId").field(&self.to_hex()).finish()
227227
}
228228
}
229229

@@ -301,7 +301,14 @@ mod test {
301301
fn test_debug() {
302302
let id = super::ObjectId::parse_str("53e37d08776f724e42000000").unwrap();
303303

304-
assert_eq!(format!("{:?}", id), "ObjectId(53e37d08776f724e42000000)")
304+
assert_eq!(
305+
format!("{:?}", id),
306+
"ObjectId(\"53e37d08776f724e42000000\")"
307+
);
308+
assert_eq!(
309+
format!("{:#?}", id),
310+
"ObjectId(\n \"53e37d08776f724e42000000\",\n)"
311+
);
305312
}
306313

307314
#[test]

src/ser/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub enum Error {
1414
/// A key could not be serialized to a BSON string.
1515
InvalidDocumentKey(Bson),
1616

17-
/// A general error that ocurred during serialization.
17+
/// A general error that occurred during serialization.
1818
/// See: https://docs.rs/serde/1.0.110/serde/ser/trait.Error.html#tymethod.custom
1919
#[non_exhaustive]
2020
SerializationError {

src/tests/modules/bson.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,43 @@ fn system_time() {
306306
0
307307
);
308308
}
309+
310+
#[test]
311+
fn debug_print() {
312+
let oid = ObjectId::parse_str("000000000000000000000000").unwrap();
313+
314+
let doc = doc! {
315+
"oid": oid,
316+
"arr": Bson::Array(vec! [
317+
Bson::Null,
318+
Bson::Timestamp(Timestamp { time: 1, increment: 1 }),
319+
]),
320+
"doc": doc! { "a": 1, "b": "data"},
321+
};
322+
let normal_print = "Document({\"oid\": ObjectId(\"000000000000000000000000\"), \"arr\": \
323+
Array([Null, Timestamp { time: 1, increment: 1 }]), \"doc\": \
324+
Document({\"a\": Int32(1), \"b\": String(\"data\")})})";
325+
let pretty_print = "Document({
326+
\"oid\": ObjectId(
327+
\"000000000000000000000000\",
328+
),
329+
\"arr\": Array([
330+
Null,
331+
Timestamp {
332+
time: 1,
333+
increment: 1,
334+
},
335+
]),
336+
\"doc\": Document({
337+
\"a\": Int32(
338+
1,
339+
),
340+
\"b\": String(
341+
\"data\",
342+
),
343+
}),
344+
})";
345+
346+
assert_eq!(format!("{:?}", doc), normal_print);
347+
assert_eq!(format!("{:#?}", doc), pretty_print);
348+
}

0 commit comments

Comments
 (0)