Skip to content

Commit 81b2e23

Browse files
committed
Use oid objects
1 parent 4110055 commit 81b2e23

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

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/oid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl error::Error for OIDError {
7575

7676
fn cause(&self) -> Option<&error::Error> {
7777
match self {
78-
&OIDError::ArgumentError(ref inner) => None,
78+
&OIDError::ArgumentError(_) => None,
7979
&OIDError::FromHexError(ref inner) => Some(inner),
8080
&OIDError::IoError(ref inner) => Some(inner),
8181
&OIDError::HostnameError => None,

0 commit comments

Comments
 (0)