Skip to content

Commit e7d4444

Browse files
committed
Merge pull request #30 from netvl/oid-display
Improved ObjectId and Bson display
2 parents c31480b + 3c2aa07 commit e7d4444

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

src/bson.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,52 +58,43 @@ pub type Document = OrderedDocument;
5858

5959
impl Display for Bson {
6060
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
61-
let bson_string = match self {
62-
&Bson::FloatingPoint(f) => format!("{}", f),
63-
&Bson::String(ref s) => format!("\"{}\"", s),
61+
match self {
62+
&Bson::FloatingPoint(f) => write!(fmt, "{}", f),
63+
&Bson::String(ref s) => write!(fmt, "\"{}\"", s),
6464
&Bson::Array(ref vec) => {
65-
let mut string = "[".to_owned();
65+
try!(write!(fmt, "["));
6666

67+
let mut first = true;
6768
for bson in vec.iter() {
68-
if !string.eq("[") {
69-
string.push_str(", ");
69+
if !first {
70+
try!(write!(fmt, ", "));
7071
}
7172

72-
string.push_str(&format!("{}", bson));
73+
try!(write!(fmt, "{}", bson));
74+
first = false;
7375
}
7476

75-
string.push_str("]");
76-
string
77+
write!(fmt, "]")
7778
}
78-
&Bson::Document(ref doc) => format!("{}", doc),
79-
&Bson::Boolean(b) => format!("{}", b),
80-
&Bson::Null => "null".to_owned(),
81-
&Bson::RegExp(ref pat, ref opt) => format!("/{}/{}", pat, opt),
79+
&Bson::Document(ref doc) => write!(fmt, "{}", doc),
80+
&Bson::Boolean(b) => write!(fmt, "{}", b),
81+
&Bson::Null => write!(fmt, "null"),
82+
&Bson::RegExp(ref pat, ref opt) => write!(fmt, "/{}/{}", pat, opt),
8283
&Bson::JavaScriptCode(ref s) |
83-
&Bson::JavaScriptCodeWithScope(ref s, _) => s.to_owned(),
84-
&Bson::I32(i) => format!("{}", i),
85-
&Bson::I64(i) => format!("{}", i),
84+
&Bson::JavaScriptCodeWithScope(ref s, _) => fmt.write_str(&s),
85+
&Bson::I32(i) => write!(fmt, "{}", i),
86+
&Bson::I64(i) => write!(fmt, "{}", i),
8687
&Bson::TimeStamp(i) => {
8788
let time = (i >> 32) as i32;
8889
let inc = (i & 0xFFFFFFFF) as i32;
8990

90-
format!("Timestamp({}, {})", time, inc)
91+
write!(fmt, "Timestamp({}, {})", time, inc)
9192
}
92-
&Bson::Binary(t, ref vec) => format!("BinData({}, 0x{})", u8::from(t), vec.to_hex()),
93-
&Bson::ObjectId(ref id) => {
94-
let mut vec = vec![];
95-
96-
for byte in id.bytes().iter() {
97-
vec.push(byte.to_owned());
98-
}
99-
100-
let string = unsafe { String::from_utf8_unchecked(vec) };
101-
format!("ObjectId(\"{}\")", string)
102-
}
103-
&Bson::UtcDatetime(date_time) => format!("Date(\"{}\")", date_time)
104-
};
105-
106-
fmt.write_str(&bson_string)
93+
&Bson::Binary(t, ref vec) =>
94+
write!(fmt, "BinData({}, 0x{})", u8::from(t), vec.to_hex()),
95+
&Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
96+
&Bson::UtcDatetime(date_time) => write!(fmt, "Date(\"{}\")", date_time)
97+
}
10798
}
10899
}
109100

src/oid.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ impl ObjectId {
146146
self.id
147147
}
148148

149-
/// Returns a 12-byte (24-char) hexadecimal string
150-
pub fn to_string(&self) -> String {
151-
self.id.to_hex()
152-
}
153-
154149
/// Retrieves the timestamp (seconds since epoch) from an ObjectId.
155150
pub fn timestamp(&self) -> u32 {
156151
BigEndian::read_u32(&self.id)
@@ -271,6 +266,18 @@ impl ObjectId {
271266
}
272267
}
273268

269+
impl ToHex for ObjectId {
270+
fn to_hex(&self) -> String {
271+
self.id.to_hex()
272+
}
273+
}
274+
275+
impl fmt::Display for ObjectId {
276+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
277+
f.write_str(&self.to_hex())
278+
}
279+
}
280+
274281
impl Decodable for ObjectId {
275282
fn decode<D: Decoder>(d: &mut D) -> result::Result<Self, D::Error> {
276283
let str = try!(d.read_str());

tests/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn test_format() {
4545
"date" => (Bson::UtcDatetime(date))
4646
};
4747

48-
let expected = format!("{{ float: 2.4, string: \"hello\", array: [\"testing\", 1], doc: {{ fish: \"in\", a: \"barrel\", !: 1 }}, bool: true, null: null, regexp: /s[ao]d/i, code: function(x) {{ return x._id; }}, i32: 12, i64: -55, timestamp: Timestamp(0, 229999444), binary: BinData(5, 0x{}), _id: ObjectId(\"{}\"), date: Date(\"{}\") }}", "thingies".as_bytes().to_hex(), id_string, date);
48+
let expected = format!("{{ float: 2.4, string: \"hello\", array: [\"testing\", 1], doc: {{ fish: \"in\", a: \"barrel\", !: 1 }}, bool: true, null: null, regexp: /s[ao]d/i, code: function(x) {{ return x._id; }}, i32: 12, i64: -55, timestamp: Timestamp(0, 229999444), binary: BinData(5, 0x{}), _id: ObjectId(\"{}\"), date: Date(\"{}\") }}", "thingies".as_bytes().to_hex(), id_string.as_bytes().to_hex(), date);
4949

5050
assert_eq!(expected, format!("{}", doc));
5151
}

0 commit comments

Comments
 (0)