Skip to content

Commit 7f31f5f

Browse files
committed
Bump version to v0.12.0, updated dependencies
1 parent 9c367b9 commit 7f31f5f

File tree

6 files changed

+60
-62
lines changed

6 files changed

+60
-62
lines changed

Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bson"
3-
version = "0.11.1"
3+
version = "0.12.0"
44
authors = [
55
"Y. T. Chung <[email protected]>",
66
"Kevin Yeh <[email protected]>"
@@ -15,17 +15,17 @@ documentation = "https://docs.rs/crate/bson"
1515
name = "bson"
1616

1717
[dependencies]
18-
byteorder = "1.0"
18+
byteorder = "1"
1919
chrono = "0.4"
2020
libc = "0.2"
21-
rand = "0.3"
22-
rust-crypto = "0.2"
21+
rand = "0.5"
2322
serde = "1.0"
24-
serde_json = { version = "1.0.0", features = ["preserve_order"] }
23+
serde_json = { version = "1.0", features = ["preserve_order"] }
2524
time = "0.1"
2625
linked-hash-map = "0.5"
27-
hostname = "^0.1"
28-
hex = "^0.2"
26+
hostname = "0.1"
27+
hex = "0.3"
28+
md5 = "0.3"
2929

3030
[dev-dependencies]
3131
serde_derive = "1.0"

src/bson.rs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
use std::fmt::{self, Debug, Display};
2525
use std::ops::{Deref, DerefMut};
2626

27-
use chrono::{DateTime, Timelike, Utc};
2827
use chrono::offset::TimeZone;
29-
use hex::{FromHex, ToHex};
28+
use chrono::{DateTime, Timelike, Utc};
29+
use hex;
3030
use serde_json::Value;
3131

3232
use oid;
@@ -107,7 +107,7 @@ impl Debug for Bson {
107107

108108
write!(f, "TimeStamp({}, {})", time, inc)
109109
}
110-
&Bson::Binary(t, ref vec) => write!(f, "BinData({}, 0x{})", u8::from(t), vec.to_hex()),
110+
&Bson::Binary(t, ref vec) => write!(f, "BinData({}, 0x{})", u8::from(t), hex::encode(vec)),
111111
&Bson::ObjectId(ref id) => write!(f, "ObjectId({:?})", id),
112112
&Bson::UtcDatetime(date_time) => write!(f, "UtcDatetime({:?})", date_time),
113113
&Bson::Symbol(ref sym) => write!(f, "Symbol({:?})", sym),
@@ -148,7 +148,7 @@ impl Display for Bson {
148148

149149
write!(fmt, "Timestamp({}, {})", time, inc)
150150
}
151-
&Bson::Binary(t, ref vec) => write!(fmt, "BinData({}, 0x{})", u8::from(t), vec.to_hex()),
151+
&Bson::Binary(t, ref vec) => write!(fmt, "BinData({}, 0x{})", u8::from(t), hex::encode(vec)),
152152
&Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
153153
&Bson::UtcDatetime(date_time) => write!(fmt, "Date(\"{}\")", date_time),
154154
&Bson::Symbol(ref sym) => write!(fmt, "Symbol(\"{}\")", sym),
@@ -270,12 +270,11 @@ impl From<DateTime<Utc>> for Bson {
270270
impl From<Value> for Bson {
271271
fn from(a: Value) -> Bson {
272272
match a {
273-
Value::Number(x) => {
274-
x.as_i64().map(Bson::from)
275-
.or_else(|| x.as_u64().map(Bson::from))
276-
.or_else(|| x.as_f64().map(Bson::from))
277-
.unwrap_or_else(|| panic!("Invalid number value: {}", x))
278-
}
273+
Value::Number(x) => x.as_i64()
274+
.map(Bson::from)
275+
.or_else(|| x.as_u64().map(Bson::from))
276+
.or_else(|| x.as_f64().map(Bson::from))
277+
.unwrap_or_else(|| panic!("Invalid number value: {}", x)),
279278
Value::String(x) => x.into(),
280279
Value::Bool(x) => x.into(),
281280
Value::Array(x) => Bson::Array(x.into_iter().map(Bson::from).collect()),
@@ -296,19 +295,15 @@ impl Into<Value> for Bson {
296295
Bson::Document(v) => json!(v),
297296
Bson::Boolean(v) => json!(v),
298297
Bson::Null => Value::Null,
299-
Bson::RegExp(pat, opt) => {
300-
json!({
298+
Bson::RegExp(pat, opt) => json!({
301299
"$regex": pat,
302300
"$options": opt
303-
})
304-
}
301+
}),
305302
Bson::JavaScriptCode(code) => json!({ "$code": code }),
306-
Bson::JavaScriptCodeWithScope(code, scope) => {
307-
json!({
303+
Bson::JavaScriptCodeWithScope(code, scope) => json!({
308304
"$code": code,
309305
"scope": scope
310-
})
311-
}
306+
}),
312307
Bson::I32(v) => v.into(),
313308
Bson::I64(v) => v.into(),
314309
Bson::TimeStamp(v) => {
@@ -323,17 +318,15 @@ impl Into<Value> for Bson {
323318
let tval: u8 = From::from(t);
324319
json!({
325320
"type": tval,
326-
"$binary": v.to_hex()
321+
"$binary": hex::encode(v),
327322
})
328323
}
329324
Bson::ObjectId(v) => json!({"$oid": v.to_string()}),
330-
Bson::UtcDatetime(v) => {
331-
json!({
325+
Bson::UtcDatetime(v) => json!({
332326
"$date": {
333327
"$numberLong": (v.timestamp() * 1000) + ((v.nanosecond() / 1000000) as i64)
334328
}
335-
})
336-
}
329+
}),
337330
// FIXME: Don't know what is the best way to encode Symbol type
338331
Bson::Symbol(v) => json!({ "$symbol": v }),
339332
}
@@ -418,7 +411,7 @@ impl Bson {
418411
Bson::Binary(t, ref v) => {
419412
let tval: u8 = From::from(t);
420413
doc! {
421-
"$binary": v.to_hex(),
414+
"$binary": hex::encode(v),
422415
"type": tval as i64,
423416
}
424417
}
@@ -460,8 +453,7 @@ impl Bson {
460453
return Bson::TimeStamp(timestamp);
461454
} else if let (Ok(hex), Ok(t)) = (values.get_str("$binary"), values.get_i64("type")) {
462455
let ttype = t as u8;
463-
return Bson::Binary(From::from(ttype),
464-
FromHex::from_hex(hex.as_bytes()).unwrap());
456+
return Bson::Binary(From::from(ttype), hex::decode(hex.as_bytes()).expect("$binary value is not a valid Hex encoded bytes"));
465457
}
466458
} else if values.len() == 1 {
467459
if let Ok(code) = values.get_str("$code") {

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
4545
extern crate byteorder;
4646
extern crate chrono;
47-
extern crate crypto;
4847
extern crate hex;
4948
extern crate hostname;
5049
extern crate libc;
@@ -53,19 +52,20 @@ extern crate rand;
5352
extern crate serde;
5453
#[macro_use]
5554
extern crate serde_json;
55+
extern crate md5;
5656
extern crate time;
5757

5858
pub use self::bson::{Array, Bson, Document, TimeStamp, UtcDateTime};
59-
pub use self::decoder::{decode_document, from_bson, Decoder, DecoderError, DecoderResult, decode_document_utf8_lossy};
59+
pub use self::decoder::{decode_document, decode_document_utf8_lossy, from_bson, Decoder, DecoderError, DecoderResult};
6060
pub use self::encoder::{encode_document, to_bson, Encoder, EncoderError, EncoderResult};
6161
pub use self::ordered::{ValueAccessError, ValueAccessResult};
6262

6363
#[macro_use]
6464
pub mod macros;
65-
pub mod oid;
66-
pub mod spec;
6765
mod bson;
68-
mod encoder;
66+
pub mod compat;
6967
mod decoder;
68+
mod encoder;
69+
pub mod oid;
7070
pub mod ordered;
71-
pub mod compat;
71+
pub mod spec;

src/oid.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
33
use libc;
44

5-
use std::{error, fmt, io, result};
65
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
6+
use std::{error, fmt, io, result};
77

88
use byteorder::{BigEndian, ByteOrder, LittleEndian};
9-
use crypto::digest::Digest;
10-
use crypto::md5::Md5;
9+
use md5;
1110

12-
use hex::{FromHex, FromHexError, ToHex};
11+
use hex::{self, FromHexError};
1312

14-
use rand::{OsRng, Rng};
13+
use rand::{thread_rng, Rng};
1514

1615
use hostname::get_hostname;
1716
use time;
@@ -126,7 +125,7 @@ impl ObjectId {
126125

127126
/// Creates an ObjectID using a 12-byte (24-char) hexadecimal string.
128127
pub fn with_string(s: &str) -> Result<ObjectId> {
129-
let bytes: Vec<u8> = FromHex::from_hex(s.as_bytes())?;
128+
let bytes: Vec<u8> = hex::decode(s.as_bytes())?;
130129
if bytes.len() != 12 {
131130
Err(Error::ArgumentError("Provided string must be a 12-byte hexadecimal string.".to_owned()))
132131
} else {
@@ -182,7 +181,7 @@ impl ObjectId {
182181

183182
/// Convert the objectId to hex representation.
184183
pub fn to_hex(&self) -> String {
185-
self.id.to_hex()
184+
hex::encode(self.id)
186185
}
187186

188187
// Generates a new timestamp representing the current seconds since epoch.
@@ -214,9 +213,8 @@ impl ObjectId {
214213
}
215214

216215
// Hash hostname string
217-
let mut md5 = Md5::new();
218-
md5.input_str(hostname.unwrap().as_str());
219-
let hash = md5.result_str();
216+
let digest = md5::compute(hostname.unwrap().as_str());
217+
let hash = format!("{:x}", digest);
220218

221219
// Re-convert string to bytes and grab first three
222220
let mut bytes = hash.bytes();
@@ -246,8 +244,7 @@ impl ObjectId {
246244
fn gen_count() -> Result<[u8; 3]> {
247245
// Init oid counter
248246
if OID_COUNTER.load(Ordering::SeqCst) == 0 {
249-
let mut rng = OsRng::new()?;
250-
let start = rng.gen_range(0, MAX_U24 + 1);
247+
let start = thread_rng().gen_range(0, MAX_U24 + 1);
251248
OID_COUNTER.store(start, Ordering::SeqCst);
252249
}
253250

tests/modules/macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use bson::Bson;
21
use bson::oid::ObjectId;
32
use bson::spec::BinarySubtype;
3+
use bson::Bson;
44
use chrono::offset::Utc;
5-
use hex::ToHex;
5+
use hex;
66

77
#[test]
88
fn standard_format() {
@@ -44,8 +44,8 @@ fn standard_format() {
4444
regexp: /s[ao]d/i, with_wrapped_parens: -20, code: function(x) {{ return x._id; }}, i32: 12, \
4545
i64: -55, timestamp: Timestamp(0, 229999444), binary: BinData(5, \
4646
0x{}), _id: ObjectId(\"{}\"), date: Date(\"{}\") }}",
47-
"thingies".to_hex(),
48-
id_string.to_hex(),
47+
hex::encode("thingies"),
48+
hex::encode(id_string),
4949
date);
5050

5151
assert_eq!(expected, format!("{}", doc));
@@ -92,8 +92,8 @@ fn rocket_format() {
9292
regexp: /s[ao]d/i, with_wrapped_parens: -20, code: function(x) {{ return x._id; }}, i32: 12, \
9393
i64: -55, timestamp: Timestamp(0, 229999444), binary: BinData(5, \
9494
0x{}), _id: ObjectId(\"{}\"), date: Date(\"{}\") }}",
95-
"thingies".to_hex(),
96-
id_string.to_hex(),
95+
hex::encode("thingies"),
96+
hex::encode(id_string),
9797
date);
9898

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

tests/modules/oid.rs

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

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

1019
let oid = ObjectId::with_bytes(bytes);
1120
assert_eq!(3735928559 as u32, oid.timestamp());
@@ -37,7 +46,7 @@ fn string_oid() {
3746
let s = "123456789012123456789012";
3847
let oid_res = ObjectId::with_string(s);
3948
assert!(oid_res.is_ok());
40-
let actual_s = oid_res.unwrap().bytes().to_hex();
49+
let actual_s = hex::encode(oid_res.unwrap().bytes());
4150
assert_eq!(s.to_owned(), actual_s);
4251
}
4352

0 commit comments

Comments
 (0)