Skip to content

Commit ac63df3

Browse files
authored
Merge pull request #61 from zonyitoo/feature-lowecase-oid
Lowercase ObjectId string
2 parents e77ae3f + 89bd1e6 commit ac63df3

File tree

7 files changed

+39
-44
lines changed

7 files changed

+39
-44
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bson"
3-
version = "0.4.4"
3+
version = "0.5.0"
44
authors = [
55
"Y. T. Chung <[email protected]>",
66
"Kevin Yeh <[email protected]>"
@@ -17,7 +17,6 @@ name = "bson"
1717
[dependencies]
1818
byteorder = "1.0"
1919
chrono = "0.2"
20-
data-encoding = "1.1.2"
2120
libc = "0.2"
2221
rand = "0.3"
2322
rust-crypto = "0.2"
@@ -26,3 +25,4 @@ serde_json = { version = "0.9.5", features = ["preserve_order"] }
2625
time = "0.1"
2726
linked-hash-map = "0.3"
2827
hostname = "^0.1"
28+
hex = "^0.2"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This crate works with Cargo and can be found on
1616

1717
```toml
1818
[dependencies]
19-
bson = "0.4"
19+
bson = "0.5"
2020
```
2121
## Usage
2222
Link the library in _main.rs_:

src/bson.rs

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

2626
use chrono::{DateTime, Timelike, UTC};
2727
use chrono::offset::TimeZone;
28-
use data_encoding::hex;
2928
use serde_json::Value;
29+
use hex::{FromHex, ToHex};
3030

3131
use oid;
3232
use ordered::OrderedDocument;
@@ -100,9 +100,7 @@ impl Debug for Bson {
100100

101101
write!(f, "TimeStamp({}, {})", time, inc)
102102
}
103-
&Bson::Binary(t, ref vec) => {
104-
write!(f, "BinData({}, 0x{})", u8::from(t), hex::encode(vec))
105-
}
103+
&Bson::Binary(t, ref vec) => write!(f, "BinData({}, 0x{})", u8::from(t), vec.to_hex()),
106104
&Bson::ObjectId(ref id) => write!(f, "ObjectId({:?})", id),
107105
&Bson::UtcDatetime(date_time) => write!(f, "UtcDatetime({:?})", date_time),
108106
&Bson::Symbol(ref sym) => write!(f, "Symbol({:?})", sym),
@@ -145,7 +143,7 @@ impl Display for Bson {
145143
write!(fmt, "Timestamp({}, {})", time, inc)
146144
}
147145
&Bson::Binary(t, ref vec) => {
148-
write!(fmt, "BinData({}, 0x{})", u8::from(t), hex::encode(vec))
146+
write!(fmt, "BinData({}, 0x{})", u8::from(t), vec.to_hex())
149147
}
150148
&Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
151149
&Bson::UtcDatetime(date_time) => write!(fmt, "Date(\"{}\")", date_time),
@@ -325,7 +323,7 @@ impl Bson {
325323
let tval: u8 = From::from(t);
326324
json!({
327325
"type": tval,
328-
"$binary": hex::encode(v)
326+
"$binary": v.to_hex()
329327
})
330328
}
331329
&Bson::ObjectId(ref v) => json!({"$oid": v.to_string()}),
@@ -396,7 +394,7 @@ impl Bson {
396394
Bson::Binary(t, ref v) => {
397395
let tval: u8 = From::from(t);
398396
doc! {
399-
"$binary" => (hex::encode(v)),
397+
"$binary" => (v.to_hex()),
400398
"type" => (tval as i64)
401399
}
402400
}
@@ -444,7 +442,7 @@ impl Bson {
444442
} else if let (Ok(hex), Ok(t)) = (values.get_str("$binary"), values.get_i64("type")) {
445443
let ttype = t as u8;
446444
return Bson::Binary(From::from(ttype),
447-
hex::decode(hex.to_uppercase().as_bytes()).unwrap());
445+
FromHex::from_hex(hex.as_bytes()).unwrap());
448446
}
449447

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

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
extern crate byteorder;
4646
extern crate chrono;
4747
extern crate crypto;
48-
extern crate data_encoding;
4948
extern crate libc;
5049
extern crate rand;
5150
extern crate serde;
@@ -54,6 +53,7 @@ extern crate serde_json;
5453
extern crate time;
5554
extern crate linked_hash_map;
5655
extern crate hostname;
56+
extern crate hex;
5757

5858
pub use self::bson::{Bson, Document, Array};
5959
pub use self::encoder::{encode_document, to_bson, Encoder, EncoderResult, EncoderError};

src/oid.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
88
use byteorder::{ByteOrder, BigEndian, LittleEndian};
99
use crypto::digest::Digest;
1010
use crypto::md5::Md5;
11-
use data_encoding::{self, hex};
11+
12+
use hex::{ToHex, FromHex, FromHexError};
1213

1314
use rand::{Rng, OsRng};
1415

@@ -35,13 +36,13 @@ static mut MACHINE_BYTES: Option<[u8; 3]> = None;
3536
#[derive(Debug)]
3637
pub enum Error {
3738
ArgumentError(String),
38-
FromHexError(data_encoding::decode::Error),
39+
FromHexError(FromHexError),
3940
IoError(io::Error),
4041
HostnameError,
4142
}
4243

43-
impl From<data_encoding::decode::Error> for Error {
44-
fn from(err: data_encoding::decode::Error) -> Error {
44+
impl From<FromHexError> for Error {
45+
fn from(err: FromHexError) -> Error {
4546
Error::FromHexError(err)
4647
}
4748
}
@@ -126,7 +127,7 @@ impl ObjectId {
126127

127128
/// Creates an ObjectID using a 12-byte (24-char) hexadecimal string.
128129
pub fn with_string(s: &str) -> Result<ObjectId> {
129-
let bytes = try!(hex::decode(s.to_uppercase().as_bytes()));
130+
let bytes: Vec<u8> = try!(FromHex::from_hex(s.as_bytes()));
130131
if bytes.len() != 12 {
131132
Err(Error::ArgumentError("Provided string must be a 12-byte hexadecimal string."
132133
.to_owned()))
@@ -183,7 +184,7 @@ impl ObjectId {
183184

184185
/// Convert the objectId to hex representation.
185186
pub fn to_hex(&self) -> String {
186-
hex::encode(&self.id)
187+
self.id.to_hex()
187188
}
188189

189190
// Generates a new timestamp representing the current seconds since epoch.
@@ -321,12 +322,12 @@ fn count_generated_is_big_endian() {
321322
fn test_display() {
322323
let id = ObjectId::with_string("53e37d08776f724e42000000").unwrap();
323324

324-
assert_eq!(format!("{}", id), "53E37D08776F724E42000000")
325+
assert_eq!(format!("{}", id), "53e37d08776f724e42000000")
325326
}
326327

327328
#[test]
328329
fn test_debug() {
329330
let id = ObjectId::with_string("53e37d08776f724e42000000").unwrap();
330331

331-
assert_eq!(format!("{:?}", id), "ObjectId(53E37D08776F724E42000000)")
332+
assert_eq!(format!("{:?}", id), "ObjectId(53e37d08776f724e42000000)")
332333
}

tests/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#[macro_use(bson, doc)]
22
extern crate bson;
33
extern crate chrono;
4-
extern crate data_encoding;
4+
extern crate hex;
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 data_encoding::hex;
12+
use hex::ToHex;
1313

1414
#[test]
1515
fn test_format() {
1616
let id_string = "thisismyname";
17-
let string_bytes : Vec<_> = id_string.bytes().collect();
17+
let string_bytes: Vec<_> = id_string.bytes().collect();
1818
let mut bytes = [0; 12];
1919

2020
for i in 0..12 {
@@ -45,7 +45,14 @@ 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(\"{}\") }}", hex::encode("thingies".as_bytes()), hex::encode(id_string.as_bytes()), date);
48+
let expected = format!("{{ float: 2.4, string: \"hello\", array: [\"testing\", 1], doc: {{ \
49+
fish: \"in\", a: \"barrel\", !: 1 }}, bool: true, null: null, \
50+
regexp: /s[ao]d/i, code: function(x) {{ return x._id; }}, i32: 12, \
51+
i64: -55, timestamp: Timestamp(0, 229999444), binary: BinData(5, \
52+
0x{}), _id: ObjectId(\"{}\"), date: Date(\"{}\") }}",
53+
"thingies".to_hex(),
54+
id_string.to_hex(),
55+
date);
4956

5057
assert_eq!(expected, format!("{}", doc));
5158
}

tests/modules/oid.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
use bson::oid::ObjectId;
2-
use data_encoding::hex;
2+
use hex::ToHex;
33

44
#[test]
55
fn deserialize() {
6-
let bytes: [u8; 12] = [
7-
0xDEu8,
8-
0xADu8,
9-
0xBEu8,
10-
0xEFu8, // timestamp is 3735928559
11-
0xEFu8,
12-
0xCDu8,
13-
0xABu8, // machine_id is 11259375
14-
0xFAu8,
15-
0x29u8, // process_id is 10746
16-
0x11u8,
17-
0x22u8,
18-
0x33u8, // increment is 1122867
19-
];
6+
let bytes: [u8; 12] = [0xDEu8, 0xADu8, 0xBEu8, 0xEFu8 /* timestamp is 3735928559 */,
7+
0xEFu8, 0xCDu8, 0xABu8 /* machine_id is 11259375 */, 0xFAu8,
8+
0x29u8 /* process_id is 10746 */, 0x11u8, 0x22u8,
9+
0x33u8 /* increment is 1122867 */];
2010

2111
let oid = ObjectId::with_bytes(bytes);
2212
assert_eq!(3735928559 as u32, oid.timestamp());
@@ -48,7 +38,7 @@ fn string_oid() {
4838
let s = "123456789012123456789012";
4939
let oid_res = ObjectId::with_string(s);
5040
assert!(oid_res.is_ok());
51-
let actual_s = hex::encode(&oid_res.unwrap().bytes());
41+
let actual_s = oid_res.unwrap().bytes().to_hex();
5242
assert_eq!(s.to_owned(), actual_s);
5343
}
5444

@@ -58,12 +48,11 @@ fn byte_string_oid() {
5848
let oid_res = ObjectId::with_string(s);
5949
assert!(oid_res.is_ok());
6050
let oid = oid_res.unwrap();
61-
let bytes: [u8; 12] = [0x54u8, 0x1Bu8, 0x1Au8, 0x00u8,
62-
0xE8u8, 0xA2u8, 0x3Au8, 0xFAu8,
63-
0x83u8, 0x2Bu8, 0x21u8, 0x8Eu8];
51+
let bytes: [u8; 12] = [0x54u8, 0x1Bu8, 0x1Au8, 0x00u8, 0xE8u8, 0xA2u8, 0x3Au8, 0xFAu8, 0x83u8,
52+
0x2Bu8, 0x21u8, 0x8Eu8];
6453

6554
assert_eq!(bytes, oid.bytes());
66-
assert_eq!(s.to_uppercase(), oid.to_string());
55+
assert_eq!(s, oid.to_string());
6756
}
6857

6958
#[test]

0 commit comments

Comments
 (0)