Skip to content

Commit ac53b24

Browse files
committed
Merge pull request #5 from kyeah/master
ObjectId container and generator
2 parents 216b906 + 1dab409 commit ac53b24

File tree

9 files changed

+432
-12
lines changed

9 files changed

+432
-12
lines changed

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ homepage = "https://github.com/zonyitoo/bson-rs"
1111
name = "bson"
1212

1313
[dependencies]
14-
chrono = "*"
15-
byteorder = "*"
16-
rustc-serialize = "*"
14+
byteorder = "0.3"
15+
chrono = "0.2"
16+
libc = "0.1"
17+
rand = "0.3"
18+
rust-crypto = "0.2.31"
19+
rustc-serialize = "0.3"
20+
time = "0.1"

examples/encode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate bson;
22
extern crate chrono;
33

44
use std::io::Cursor;
5-
use bson::{Bson, Document, Array, encode_document, decode_document};
5+
use bson::{Bson, Document, Array, encode_document, decode_document, oid};
66

77
fn main() {
88
let mut doc = Document::new();
@@ -11,7 +11,7 @@ fn main() {
1111
let mut arr = Array::new();
1212
arr.push(Bson::String("blah".to_string()));
1313
arr.push(Bson::UtcDatetime(chrono::UTC::now()));
14-
arr.push(Bson::ObjectId([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]));
14+
arr.push(Bson::ObjectId(oid::ObjectId::with_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])));
1515

1616
doc.insert("array".to_string(), Bson::Array(arr));
1717

src/bson.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_serialize::hex::ToHex;
2727

2828
use ordered::OrderedDocument;
2929
use spec::{ElementType, BinarySubtype};
30+
use oid;
3031

3132
/// Possible BSON value types.
3233
#[derive(Debug, Clone)]
@@ -44,7 +45,7 @@ pub enum Bson {
4445
I64(i64),
4546
TimeStamp(i64),
4647
Binary(BinarySubtype, Vec<u8>),
47-
ObjectId([u8; 12]),
48+
ObjectId(oid::ObjectId),
4849
UtcDatetime(DateTime<UTC>),
4950
}
5051

@@ -124,7 +125,13 @@ impl From<i64> for Bson {
124125

125126
impl From<[u8; 12]> for Bson {
126127
fn from(a: [u8; 12]) -> Bson {
127-
Bson::ObjectId(a)
128+
Bson::ObjectId(oid::ObjectId::with_bytes(a))
129+
}
130+
}
131+
132+
impl From<oid::ObjectId> for Bson {
133+
fn from(a: oid::ObjectId) -> Bson {
134+
Bson::ObjectId(a.to_owned())
128135
}
129136
}
130137

@@ -197,7 +204,7 @@ impl Bson {
197204

198205
json::Json::Object(obj)
199206
},
200-
&Bson::ObjectId(v) => json::Json::String(v.to_hex()),
207+
&Bson::ObjectId(ref v) => json::Json::String(v.bytes().to_hex()),
201208
&Bson::UtcDatetime(ref v) => json::Json::String(v.to_string()),
202209
}
203210
}

src/decoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use chrono::{DateTime, NaiveDateTime, UTC};
2929

3030
use spec::{self, BinarySubtype};
3131
use bson::{Bson, Array, Document};
32+
use oid;
3233

3334
/// Possible errors that can arise during decoding.
3435
#[derive(Debug)]
@@ -194,7 +195,7 @@ fn decode_bson<R: Read + ?Sized>(reader: &mut R, tag: u8) -> DecoderResult<Bson>
194195
for x in &mut objid {
195196
*x = try!(reader.read_u8());
196197
}
197-
Ok(Bson::ObjectId(objid))
198+
Ok(Bson::ObjectId(oid::ObjectId::with_bytes(objid)))
198199
}
199200
Some(Boolean) => Ok(Bson::Boolean(try!(reader.read_u8()) != 0)),
200201
Some(NullValue) => Ok(Bson::Null),

src/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn encode_bson<W: Write + ?Sized>(writer: &mut W, key: &str, val: &Bson) -> Enco
145145
write_cstring(writer, opt)
146146
},
147147
&Bson::JavaScriptCode(ref code) => write_string(writer, &code),
148-
&Bson::ObjectId(id) => writer.write_all(&id).map_err(From::from),
148+
&Bson::ObjectId(ref id) => writer.write_all(&id.bytes()).map_err(From::from),
149149
&Bson::JavaScriptCodeWithScope(ref code, ref scope) => {
150150
let mut buf = Vec::new();
151151
try!(write_string(&mut buf, code));

src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@
4242
//! }
4343
//! ```
4444
45-
extern crate rustc_serialize;
46-
extern crate chrono;
4745
extern crate byteorder;
46+
extern crate chrono;
47+
extern crate crypto;
48+
extern crate libc;
49+
extern crate rand;
50+
extern crate rustc_serialize;
51+
extern crate time;
4852

4953
pub use self::bson::{Bson, Document, Array};
5054
pub use self::encoder::{encode_document, EncoderResult, EncoderError};
5155
pub use self::decoder::{decode_document, DecoderResult, DecoderError};
5256

5357
pub mod spec;
58+
pub mod oid;
5459
mod bson;
5560
mod encoder;
5661
mod decoder;

0 commit comments

Comments
 (0)