Skip to content

Commit a064c34

Browse files
committed
changed binary values to print out as numbers
1 parent a61bad8 commit a064c34

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/bson.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Display for Bson {
7575

7676
string.push_str("]");
7777
string
78-
},
78+
}
7979
&Bson::Document(ref doc) => format!("{}", doc),
8080
&Bson::Boolean(b) => format!("{}", b),
8181
&Bson::Null => "null".to_owned(),
@@ -89,11 +89,8 @@ impl Display for Bson {
8989
let inc = (i & 0xFFFFFFFF) as i32;
9090

9191
format!("Timestamp({}, {})", time, inc)
92-
},
93-
&Bson::Binary(t, ref vec) => {
94-
let string = unsafe { str::from_utf8_unchecked(vec) };
95-
format!("BinData({}, \"{}\")", u8::from(t), string)
9692
}
93+
&Bson::Binary(t, ref vec) => format!("BinData({}, {})", u8::from(t), vec.to_hex()),
9794
&Bson::ObjectId(ref id) => {
9895
let mut vec = vec![];
9996

tests/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use bson::Bson;
99
use bson::spec::BinarySubtype;
1010
use bson::oid::ObjectId;
1111
use chrono::offset::utc::UTC;
12+
use rustc_serialize::hex::ToHex;
1213

1314
#[test]
1415
fn test_format() {
@@ -44,7 +45,7 @@ fn test_format() {
4445
"date" => (Bson::UtcDatetime(date))
4546
};
4647

47-
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, \"thingies\"), _id: ObjectId(\"{}\"), date: Date(\"{}\") }}", 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, {}), _id: ObjectId(\"{}\"), date: Date(\"{}\") }}", "thingies".as_bytes().to_hex(), id_string, date);
4849

4950
assert_eq!(expected, format!("{}", doc));
5051
}

0 commit comments

Comments
 (0)