Skip to content

Commit b0ebd60

Browse files
committed
replace rustc-serialize with data-encoding
1 parent 92f8fe4 commit b0ebd60

File tree

6 files changed

+26
-27
lines changed

6 files changed

+26
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ name = "bson"
1616
[dependencies]
1717
byteorder = "1.0"
1818
chrono = "0.2"
19+
data-encoding = "1.1.2"
1920
libc = "0.2"
2021
rand = "0.3"
2122
rust-crypto = "0.2"
22-
rustc-serialize = "0.3"
2323
serde = "0.9"
2424
serde_json = { version = "0.9.5", features = ["preserve_order"] }
2525
time = "0.1"

src/bson.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ use std::fmt::{self, Display, Debug};
2525

2626
use chrono::{DateTime, Timelike, UTC};
2727
use chrono::offset::TimeZone;
28+
use data_encoding::hex;
2829

2930
use oid;
3031
use ordered::OrderedDocument;
31-
use rustc_serialize::hex::{FromHex, ToHex};
3232
use serde_json:: Value;
3333
use spec::{ElementType, BinarySubtype};
3434

@@ -80,7 +80,7 @@ impl Debug for Bson {
8080

8181
write!(f, "TimeStamp({}, {})", time, inc)
8282
}
83-
&Bson::Binary(t, ref vec) => write!(f, "BinData({}, 0x{})", u8::from(t), vec.to_hex()),
83+
&Bson::Binary(t, ref vec) => write!(f, "BinData({}, 0x{})", u8::from(t), hex::encode(vec)),
8484
&Bson::ObjectId(ref id) => write!(f, "ObjectId({:?})", id),
8585
&Bson::UtcDatetime(date_time) => write!(f, "UtcDatetime({:?})", date_time),
8686
&Bson::Symbol(ref sym) => write!(f, "Symbol({:?})", sym),
@@ -123,7 +123,7 @@ impl Display for Bson {
123123
write!(fmt, "Timestamp({}, {})", time, inc)
124124
}
125125
&Bson::Binary(t, ref vec) => {
126-
write!(fmt, "BinData({}, 0x{})", u8::from(t), vec.to_hex())
126+
write!(fmt, "BinData({}, 0x{})", u8::from(t), hex::encode(vec))
127127
}
128128
&Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
129129
&Bson::UtcDatetime(date_time) => write!(fmt, "Date(\"{}\")", date_time),
@@ -301,7 +301,7 @@ impl Bson {
301301
let tval: u8 = From::from(t);
302302
json!({
303303
"type": tval,
304-
"$binary": v.to_hex()
304+
"$binary": hex::encode(v)
305305
})
306306
},
307307
&Bson::ObjectId(ref v) => json!({"$oid": v.to_string()}),
@@ -365,7 +365,7 @@ impl Bson {
365365
Bson::Binary(t, ref v) => {
366366
let tval: u8 = From::from(t);
367367
doc! {
368-
"$binary" => (v.to_hex()),
368+
"$binary" => (hex::encode(v)),
369369
"type" => (tval as i64)
370370
}
371371
}
@@ -409,7 +409,7 @@ impl Bson {
409409

410410
} else if let (Ok(hex), Ok(t)) = (values.get_str("$binary"), values.get_i64("type")) {
411411
let ttype = t as u8;
412-
return Bson::Binary(From::from(ttype), hex.from_hex().unwrap());
412+
return Bson::Binary(From::from(ttype), hex::decode(hex.to_uppercase().as_bytes()).unwrap());
413413
}
414414

415415
} else if values.len() == 1 {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
extern crate byteorder;
4646
extern crate chrono;
4747
extern crate crypto;
48+
extern crate data_encoding;
4849
extern crate libc;
4950
extern crate rand;
50-
extern crate rustc_serialize;
5151
extern crate serde;
5252
#[macro_use]
5353
extern crate serde_json;

src/oid.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
66
use byteorder::{ByteOrder, BigEndian, LittleEndian};
77
use crypto::digest::Digest;
88
use crypto::md5::Md5;
9+
use data_encoding::{self, hex};
910

1011
use rand::{Rng, OsRng};
11-
use rustc_serialize::hex::{self, FromHex, ToHex};
1212

1313
use time;
1414
use hostname::get_hostname;
@@ -33,13 +33,13 @@ static mut MACHINE_BYTES: Option<[u8; 3]> = None;
3333
#[derive(Debug)]
3434
pub enum Error {
3535
ArgumentError(String),
36-
FromHexError(hex::FromHexError),
36+
FromHexError(data_encoding::decode::Error),
3737
IoError(io::Error),
3838
HostnameError,
3939
}
4040

41-
impl From<hex::FromHexError> for Error {
42-
fn from(err: hex::FromHexError) -> Error {
41+
impl From<data_encoding::decode::Error> for Error {
42+
fn from(err: data_encoding::decode::Error) -> Error {
4343
Error::FromHexError(err)
4444
}
4545
}
@@ -124,7 +124,7 @@ impl ObjectId {
124124

125125
/// Creates an ObjectID using a 12-byte (24-char) hexadecimal string.
126126
pub fn with_string(s: &str) -> Result<ObjectId> {
127-
let bytes = try!(s.from_hex());
127+
let bytes = try!(hex::decode(s.to_uppercase().as_bytes()));
128128
if bytes.len() != 12 {
129129
Err(Error::ArgumentError("Provided string must be a 12-byte hexadecimal string."
130130
.to_owned()))
@@ -179,6 +179,11 @@ impl ObjectId {
179179
BigEndian::read_u32(&buf)
180180
}
181181

182+
/// Convert the objectId to hex representation.
183+
pub fn to_hex(&self) -> String {
184+
hex::encode(&self.id)
185+
}
186+
182187
// Generates a new timestamp representing the current seconds since epoch.
183188
// Represented in Big Endian.
184189
fn gen_timestamp() -> [u8; 4] {
@@ -263,12 +268,6 @@ impl ObjectId {
263268
}
264269
}
265270

266-
impl ToHex for ObjectId {
267-
fn to_hex(&self) -> String {
268-
self.id.to_hex()
269-
}
270-
}
271-
272271
impl fmt::Display for ObjectId {
273272
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
274273
f.write_str(&self.to_hex())
@@ -320,12 +319,12 @@ fn count_generated_is_big_endian() {
320319
fn test_display() {
321320
let id = ObjectId::with_string("53e37d08776f724e42000000").unwrap();
322321

323-
assert_eq!(format!("{}", id), "53e37d08776f724e42000000")
322+
assert_eq!(format!("{}", id), "53E37D08776F724E42000000")
324323
}
325324

326325
#[test]
327326
fn test_debug() {
328327
let id = ObjectId::with_string("53e37d08776f724e42000000").unwrap();
329328

330-
assert_eq!(format!("{:?}", id), "ObjectId(53e37d08776f724e42000000)")
329+
assert_eq!(format!("{:?}", id), "ObjectId(53E37D08776F724E42000000)")
331330
}

tests/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#[macro_use(bson, doc)]
22
extern crate bson;
33
extern crate chrono;
4-
extern crate rustc_serialize;
4+
extern crate data_encoding;
55

66
mod modules;
77

88
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;
12+
use data_encoding::hex;
1313

1414
#[test]
1515
fn test_format() {
@@ -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.as_bytes().to_hex(), 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(\"{}\") }}", hex::encode("thingies".as_bytes()), hex::encode(id_string.as_bytes()), date);
4949

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

tests/modules/oid.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bson::oid::ObjectId;
2-
use rustc_serialize::hex::ToHex;
2+
use data_encoding::hex;
33

44
#[test]
55
fn deserialize() {
@@ -48,7 +48,7 @@ fn string_oid() {
4848
let s = "123456789012123456789012";
4949
let oid_res = ObjectId::with_string(s);
5050
assert!(oid_res.is_ok());
51-
let actual_s = oid_res.unwrap().bytes().to_hex();
51+
let actual_s = hex::encode(&oid_res.unwrap().bytes());
5252
assert_eq!(s.to_owned(), actual_s);
5353
}
5454

@@ -63,7 +63,7 @@ fn byte_string_oid() {
6363
0x83u8, 0x2Bu8, 0x21u8, 0x8Eu8];
6464

6565
assert_eq!(bytes, oid.bytes());
66-
assert_eq!(s, oid.to_string());
66+
assert_eq!(s.to_uppercase(), oid.to_string());
6767
}
6868

6969
#[test]

0 commit comments

Comments
 (0)