Skip to content

Commit 00c69f8

Browse files
committed
Merge pull request #1 from SpaceManiac/updates
Removed `Encoder` and `Decoder` structs, replacing them with `encode_document` and `decode_document`. Removed `ToBson` Trait. Add documents. Removed incorrectly-implemented Deprecated element type.
2 parents bb5402a + e874b2a commit 00c69f8

File tree

7 files changed

+348
-549
lines changed

7 files changed

+348
-549
lines changed

examples/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fs::File;
55
fn main() {
66
let mut f = File::open("examples/test.bson").unwrap();
77

8-
while let Ok(decoded) = bson::Decoder::new(&mut f).decode_document() {
8+
while let Ok(decoded) = bson::decode_document(&mut f) {
99
println!("{:?}", decoded);
1010
}
1111
}

examples/encode.rs

Lines changed: 4 additions & 12 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, Encoder, Decoder};
5+
use bson::{Bson, Document, Array, encode_document, decode_document};
66

77
fn main() {
88
let mut doc = Document::new();
@@ -16,18 +16,10 @@ fn main() {
1616
doc.insert("array".to_string(), Bson::Array(arr));
1717

1818
let mut buf = Vec::new();
19-
{
20-
let mut enc = Encoder::new(&mut buf);
21-
enc.encode_document(&doc).unwrap();
22-
}
19+
encode_document(&mut buf, &doc).unwrap();
2320

2421
println!("Encoded: {:?}", buf);
2522

26-
let mut r = Cursor::new(&buf[..]);
27-
{
28-
let mut dec = Decoder::new(&mut r);
29-
let doc = dec.decode_document().unwrap();
30-
31-
println!("Decoded: {:?}", doc);
32-
}
23+
let doc = decode_document(&mut Cursor::new(&buf[..])).unwrap();
24+
println!("Decoded: {:?}", doc);
3325
}

src/bson.rs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,25 @@
2222
//! BSON definition
2323
2424
use std::collections::BTreeMap;
25-
use std::string;
2625

2726
use chrono::{DateTime, UTC};
2827
use rustc_serialize::json;
2928
use rustc_serialize::hex::ToHex;
3029

31-
use spec::BinarySubtype;
30+
use spec::{ElementType, BinarySubtype};
3231

32+
/// Possible BSON value types.
3333
#[derive(Debug, Clone)]
3434
pub enum Bson {
3535
FloatingPoint(f64),
3636
String(String),
37-
Array(self::Array),
38-
Document(self::Document),
37+
Array(Array),
38+
Document(Document),
3939
Boolean(bool),
4040
Null,
41-
RegExp(string::String, string::String),
42-
JavaScriptCode(string::String),
43-
JavaScriptCodeWithScope(string::String, self::Document),
44-
Deprecated,
41+
RegExp(String, String),
42+
JavaScriptCode(String),
43+
JavaScriptCodeWithScope(String, Document),
4544
I32(i32),
4645
I64(i64),
4746
TimeStamp(i64),
@@ -50,10 +49,34 @@ pub enum Bson {
5049
UtcDatetime(DateTime<UTC>),
5150
}
5251

52+
/// Alias for `Vec<Bson>`.
5353
pub type Array = Vec<Bson>;
54+
/// Alias for `BTreeMap<String, Bson>`.
5455
pub type Document = BTreeMap<String, Bson>;
5556

5657
impl Bson {
58+
/// Get the `ElementType` of this value.
59+
pub fn element_type(&self) -> ElementType {
60+
match self {
61+
&Bson::FloatingPoint(..) => ElementType::FloatingPoint,
62+
&Bson::String(..) => ElementType::Utf8String,
63+
&Bson::Array(..) => ElementType::Array,
64+
&Bson::Document(..) => ElementType::EmbeddedDocument,
65+
&Bson::Boolean(..) => ElementType::Boolean,
66+
&Bson::Null => ElementType::NullValue,
67+
&Bson::RegExp(..) => ElementType::RegularExpression,
68+
&Bson::JavaScriptCode(..) => ElementType::JavaScriptCode,
69+
&Bson::JavaScriptCodeWithScope(..) => ElementType::JavaScriptCodeWithScope,
70+
&Bson::I32(..) => ElementType::Integer32Bit,
71+
&Bson::I64(..) => ElementType::Integer64Bit,
72+
&Bson::TimeStamp(..) => ElementType::TimeStamp,
73+
&Bson::Binary(..) => ElementType::Binary,
74+
&Bson::ObjectId(..) => ElementType::ObjectId,
75+
&Bson::UtcDatetime(..) => ElementType::UtcDatetime,
76+
}
77+
}
78+
79+
/// Convert this value to the best approximate `Json`.
5780
pub fn to_json(&self) -> json::Json {
5881
match self {
5982
&Bson::FloatingPoint(v) => json::Json::F64(v),
@@ -66,32 +89,31 @@ impl Bson {
6689
&Bson::Null => json::Json::Null,
6790
&Bson::RegExp(ref pat, ref opt) => {
6891
let mut re = json::Object::new();
69-
re.insert("pattern".to_string(), json::Json::String(pat.clone()));
70-
re.insert("options".to_string(), json::Json::String(opt.clone()));
92+
re.insert("pattern".to_owned(), json::Json::String(pat.clone()));
93+
re.insert("options".to_owned(), json::Json::String(opt.clone()));
7194

7295
json::Json::Object(re)
7396
},
7497
&Bson::JavaScriptCode(ref code) => json::Json::String(code.clone()),
7598
&Bson::JavaScriptCodeWithScope(ref code, ref scope) => {
7699
let mut obj = json::Object::new();
77-
obj.insert("code".to_string(), json::Json::String(code.clone()));
100+
obj.insert("code".to_owned(), json::Json::String(code.clone()));
78101

79102
let scope_obj =
80103
scope.iter().map(|(k, v)| (k.clone(), v.to_json())).collect();
81104

82-
obj.insert("scope".to_string(), json::Json::Object(scope_obj));
105+
obj.insert("scope".to_owned(), json::Json::Object(scope_obj));
83106

84107
json::Json::Object(obj)
85108
},
86-
&Bson::Deprecated => json::Json::String("deprecated".to_string()),
87109
&Bson::I32(v) => json::Json::I64(v as i64),
88110
&Bson::I64(v) => json::Json::I64(v),
89111
&Bson::TimeStamp(v) => json::Json::I64(v),
90112
&Bson::Binary(t, ref v) => {
91113
let mut obj = json::Object::new();
92114
let tval: u8 = From::from(t);
93-
obj.insert("type".to_string(), json::Json::I64(tval as i64));
94-
obj.insert("data".to_string(), json::Json::String(v[..].to_hex()));
115+
obj.insert("type".to_owned(), json::Json::I64(tval as i64));
116+
obj.insert("data".to_owned(), json::Json::String(v.to_hex()));
95117

96118
json::Json::Object(obj)
97119
},
@@ -100,38 +122,17 @@ impl Bson {
100122
}
101123
}
102124

125+
/// Create a `Bson` from a `Json`.
103126
pub fn from_json(j: &json::Json) -> Bson {
104127
match j {
105128
&json::Json::I64(x) => Bson::I64(x),
106129
&json::Json::U64(x) => Bson::I64(x as i64),
107130
&json::Json::F64(x) => Bson::FloatingPoint(x),
108131
&json::Json::String(ref x) => Bson::String(x.clone()),
109132
&json::Json::Boolean(x) => Bson::Boolean(x),
110-
&json::Json::Array(ref x) => Bson::Array(x.iter().map(|x| Bson::from_json(x)).collect()),
133+
&json::Json::Array(ref x) => Bson::Array(x.iter().map(Bson::from_json).collect()),
111134
&json::Json::Object(ref x) => Bson::Document(x.iter().map(|(k, v)| (k.clone(), Bson::from_json(v))).collect()),
112135
&json::Json::Null => Bson::Null,
113136
}
114137
}
115138
}
116-
117-
pub trait ToBson {
118-
fn to_bson(&self) -> Bson;
119-
}
120-
121-
impl ToBson for str {
122-
fn to_bson(&self) -> Bson {
123-
Bson::String(self.to_string())
124-
}
125-
}
126-
127-
impl<T: ToBson> ToBson for [T] {
128-
fn to_bson(&self) -> Bson {
129-
Bson::Array(self.iter().map(|x| x.to_bson()).collect())
130-
}
131-
}
132-
133-
impl<T: ToBson> ToBson for BTreeMap<String, T> {
134-
fn to_bson(&self) -> Bson {
135-
Bson::Document(self.iter().map(|(k, v)| (k.clone(), v.to_bson())).collect())
136-
}
137-
}

0 commit comments

Comments
 (0)