Skip to content

Commit 0c25c25

Browse files
committed
Some cleanup to encoding and elsewhere.
1 parent 56cb98a commit 0c25c25

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

src/bson.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl Bson {
9090
let mut obj = json::Object::new();
9191
let tval: u8 = From::from(t);
9292
obj.insert("type".to_owned(), json::Json::I64(tval as i64));
93-
obj.insert("data".to_owned(), json::Json::String(v[..].to_hex()));
93+
obj.insert("data".to_owned(), json::Json::String(v.to_hex()));
9494

9595
json::Json::Object(obj)
9696
},

src/decoder.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub enum DecoderError {
3535
IoError(io::Error),
3636
Utf8Error(str::Utf8Error),
3737
UnrecognizedElementType(u8),
38-
InvalidArrayKey(String, String)
38+
InvalidArrayKey(usize, String)
3939
}
4040

4141
impl From<io::Error> for DecoderError {
@@ -80,6 +80,13 @@ impl error::Error for DecoderError {
8080
&DecoderError::InvalidArrayKey(_, _) => "invalid array key"
8181
}
8282
}
83+
fn cause(&self) -> Option<&error::Error> {
84+
match self {
85+
&DecoderError::IoError(ref inner) => Some(inner),
86+
&DecoderError::Utf8Error(ref inner) => Some(inner),
87+
_ => None
88+
}
89+
}
8390
}
8491

8592
pub type DecoderResult<T> = Result<T, DecoderError>;
@@ -219,9 +226,8 @@ impl<'a> Decoder<'a> {
219226
}
220227

221228
let k = try!(self.read_cstring());
222-
let want = format!("{}", arr.len());
223-
if k != want {
224-
return Err(DecoderError::InvalidArrayKey(want, k));
229+
if k != &arr.len().to_string()[..] {
230+
return Err(DecoderError::InvalidArrayKey(arr.len(), k));
225231
}
226232
let v = try!(self.decode_bson(t));
227233

src/encoder.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
//! Encoder
2323
2424
use std::io::{self, Write};
25-
use std::convert::From;
26-
use std::mem;
25+
use std::{mem, error, fmt};
2726

2827
use byteorder::{self, LittleEndian, WriteBytesExt};
2928
use chrono::{DateTime, UTC};
3029

3130
use spec::{ElementType, BinarySubtype};
32-
use bson;
31+
use bson::{Array, Document, Bson};
3332

3433
#[derive(Debug)]
3534
pub enum EncoderError {
@@ -48,6 +47,27 @@ impl From<byteorder::Error> for EncoderError {
4847
}
4948
}
5049

50+
impl fmt::Display for EncoderError {
51+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
52+
match self {
53+
&EncoderError::IoError(ref inner) => inner.fmt(fmt)
54+
}
55+
}
56+
}
57+
58+
impl error::Error for EncoderError {
59+
fn description(&self) -> &str {
60+
match self {
61+
&EncoderError::IoError(ref inner) => inner.description(),
62+
}
63+
}
64+
fn cause(&self) -> Option<&error::Error> {
65+
match self {
66+
&EncoderError::IoError(ref inner) => Some(inner)
67+
}
68+
}
69+
}
70+
5171
pub type EncoderResult<T> = Result<T, EncoderError>;
5272

5373
pub struct Encoder<'a> {
@@ -159,7 +179,7 @@ impl<'a> Encoder<'a> {
159179
Ok(())
160180
}
161181

162-
pub fn encode_javascript_code_with_scope(&mut self, key: &str, code: &str, scope: &bson::Document)
182+
pub fn encode_javascript_code_with_scope(&mut self, key: &str, code: &str, scope: &Document)
163183
-> Result<(), EncoderError> {
164184
try!(self.writer.write_u8(ElementType::JavaScriptCodeWithScope as u8));
165185
try!(self.write_cstring(key));
@@ -213,21 +233,21 @@ impl<'a> Encoder<'a> {
213233
Ok(())
214234
}
215235

216-
pub fn encode_embedded_document(&mut self, key: &str, doc: &bson::Document) -> Result<(), EncoderError> {
236+
pub fn encode_embedded_document(&mut self, key: &str, doc: &Document) -> Result<(), EncoderError> {
217237
try!(self.writer.write_u8(ElementType::EmbeddedDocument as u8));
218238
try!(self.write_cstring(key));
219239

220240
self.encode_document(doc)
221241
}
222242

223-
pub fn encode_embedded_array(&mut self, key: &str, arr: &bson::Array) -> Result<(), EncoderError> {
243+
pub fn encode_embedded_array(&mut self, key: &str, arr: &Array) -> Result<(), EncoderError> {
224244
try!(self.writer.write_u8(ElementType::Array as u8));
225245
try!(self.write_cstring(key));
226246

227247
self.encode_array(arr)
228248
}
229249

230-
pub fn encode_document(&mut self, doc: &bson::Document) -> Result<(), EncoderError> {
250+
pub fn encode_document(&mut self, doc: &Document) -> Result<(), EncoderError> {
231251
let mut buf = Vec::new();
232252

233253
{
@@ -244,13 +264,13 @@ impl<'a> Encoder<'a> {
244264
Ok(())
245265
}
246266

247-
pub fn encode_array(&mut self, arr: &bson::Array) -> Result<(), EncoderError> {
267+
pub fn encode_array(&mut self, arr: &Array) -> Result<(), EncoderError> {
248268
let mut buf = Vec::new();
249269

250270
{
251271
let mut enc = Encoder::new(&mut buf);
252272
for (key, val) in arr.iter().enumerate() {
253-
try!(enc.encode_bson(&key.to_string()[..], val));
273+
try!(enc.encode_bson(&key.to_string(), val));
254274
}
255275
}
256276

@@ -261,9 +281,7 @@ impl<'a> Encoder<'a> {
261281
Ok(())
262282
}
263283

264-
fn encode_bson(&mut self, key: &str, val: &bson::Bson) -> Result<(), EncoderError> {
265-
use bson::Bson;
266-
284+
fn encode_bson(&mut self, key: &str, val: &Bson) -> Result<(), EncoderError> {
267285
match val {
268286
&Bson::FloatingPoint(v) => self.encode_floating_point(&key[..], v),
269287
&Bson::String(ref v) => self.encode_utf8_string(&key[..], &v[..]),
@@ -289,7 +307,7 @@ impl<'a> Encoder<'a> {
289307
#[cfg(test)]
290308
mod test {
291309
use super::Encoder;
292-
use bson;
310+
use bson::{Document, Bson};
293311

294312
#[test]
295313
fn test_encode_floating_point() {
@@ -300,8 +318,8 @@ mod test {
300318
{
301319
let mut enc = Encoder::new(&mut buf);
302320

303-
let mut doc = bson::Document::new();
304-
doc.insert("key".to_string(), bson::Bson::FloatingPoint(src));
321+
let mut doc = Document::new();
322+
doc.insert("key".to_owned(), Bson::FloatingPoint(src));
305323
enc.encode_document(&doc).unwrap();
306324
}
307325

@@ -310,32 +328,32 @@ mod test {
310328

311329
#[test]
312330
fn test_encode_utf8_string() {
313-
let src = "test你好吗".to_string();
331+
let src = "test你好吗".to_owned();
314332
let dst = [28, 0, 0, 0, 2, 107, 101, 121, 0, 14, 0, 0, 0, 116, 101, 115, 116, 228, 189, 160, 229, 165, 189, 229, 144, 151, 0, 0];
315333

316334
let mut buf = Vec::new();
317335
{
318336
let mut enc = Encoder::new(&mut buf);
319337

320-
let mut doc = bson::Document::new();
321-
doc.insert("key".to_string(), bson::Bson::String(src));
338+
let mut doc = Document::new();
339+
doc.insert("key".to_owned(), Bson::String(src));
322340
enc.encode_document(&doc).unwrap();
323341
}
324342

325-
assert_eq!(&buf[..], &dst[..]);
343+
assert_eq!(&buf, &dst);
326344
}
327345

328346
#[test]
329347
fn test_encode_array() {
330-
let src = vec![bson::Bson::FloatingPoint(1.01), bson::Bson::String("xyz".to_string())];
348+
let src = vec![Bson::FloatingPoint(1.01), Bson::String("xyz".to_owned())];
331349
let dst = [37, 0, 0, 0, 4, 107, 101, 121, 0, 27, 0, 0, 0, 1, 48, 0, 41, 92, 143, 194, 245, 40, 240, 63, 2, 49, 0, 4, 0, 0, 0, 120, 121, 122, 0, 0, 0];
332350

333351
let mut buf = Vec::new();
334352
{
335353
let mut enc = Encoder::new(&mut buf);
336354

337-
let mut doc = bson::Document::new();
338-
doc.insert("key".to_string(), bson::Bson::Array(src));
355+
let mut doc = Document::new();
356+
doc.insert("key".to_owned(), Bson::Array(src));
339357
enc.encode_document(&doc).unwrap();
340358
}
341359

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//!
3434
//! fn main() {
3535
//! let mut doc = Document::new();
36-
//! doc.insert("foo".to_string(), Bson::String("bar".to_string()));
36+
//! doc.insert("foo".to_owned(), Bson::String("bar".to_owned()));
3737
//!
3838
//! let mut buf = Vec::new();
3939
//! {

0 commit comments

Comments
 (0)