Skip to content

Commit fd332ca

Browse files
committed
Merge pull request #6 from kyeah/master
BSON Macro + Moved tests to separate module
2 parents ac53b24 + e3f5e3c commit fd332ca

File tree

13 files changed

+221
-95
lines changed

13 files changed

+221
-95
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
language: rust
22

3+
env:
4+
- RUST_TEST_THREADS=1
5+
36
script:
47
- cargo build -v
58
- cargo test -v

src/bson.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ pub type Array = Vec<Bson>;
5454
/// Alias for `OrderedDocument`.
5555
pub type Document = OrderedDocument;
5656

57+
impl From<f32> for Bson {
58+
fn from(a: f32) -> Bson {
59+
Bson::FloatingPoint(a as f64)
60+
}
61+
}
62+
5763
impl From<f64> for Bson {
5864
fn from(a: f64) -> Bson {
5965
Bson::FloatingPoint(a)
@@ -123,6 +129,18 @@ impl From<i64> for Bson {
123129
}
124130
}
125131

132+
impl From<u32> for Bson {
133+
fn from(a: u32) -> Bson {
134+
Bson::I32(a as i32)
135+
}
136+
}
137+
138+
impl From<u64> for Bson {
139+
fn from(a: u64) -> Bson {
140+
Bson::I64(a as i64)
141+
}
142+
}
143+
126144
impl From<[u8; 12]> for Bson {
127145
fn from(a: [u8; 12]) -> Bson {
128146
Bson::ObjectId(oid::ObjectId::with_bytes(a))

src/encoder.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -166,51 +166,3 @@ fn encode_bson<W: Write + ?Sized>(writer: &mut W, key: &str, val: &Bson) -> Enco
166166
&Bson::Null => Ok(())
167167
}
168168
}
169-
170-
#[cfg(test)]
171-
mod test {
172-
use super::encode_document;
173-
use bson::{Document, Bson};
174-
175-
#[test]
176-
fn test_encode_floating_point() {
177-
let src = 1020.123;
178-
let dst = [18, 0, 0, 0, 1, 107, 101, 121, 0, 68, 139, 108, 231, 251, 224, 143, 64, 0];
179-
180-
let mut doc = Document::new();
181-
doc.insert("key".to_owned(), Bson::FloatingPoint(src));
182-
183-
let mut buf = Vec::new();
184-
encode_document(&mut buf, &doc).unwrap();
185-
186-
assert_eq!(&buf, &dst);
187-
}
188-
189-
#[test]
190-
fn test_encode_utf8_string() {
191-
let src = "test你好吗".to_owned();
192-
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];
193-
194-
let mut doc = Document::new();
195-
doc.insert("key".to_owned(), Bson::String(src));
196-
197-
let mut buf = Vec::new();
198-
encode_document(&mut buf, &doc).unwrap();
199-
200-
assert_eq!(&buf, &dst);
201-
}
202-
203-
#[test]
204-
fn test_encode_array() {
205-
let src = vec![Bson::FloatingPoint(1.01), Bson::String("xyz".to_owned())];
206-
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];
207-
208-
let mut doc = Document::new();
209-
doc.insert("key".to_owned(), Bson::Array(src));
210-
211-
let mut buf = Vec::new();
212-
encode_document(&mut buf, &doc).unwrap();
213-
214-
assert_eq!(&buf[..], &dst[..]);
215-
}
216-
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ pub use self::bson::{Bson, Document, Array};
5454
pub use self::encoder::{encode_document, EncoderResult, EncoderError};
5555
pub use self::decoder::{decode_document, DecoderResult, DecoderError};
5656

57-
pub mod spec;
57+
pub mod macros;
5858
pub mod oid;
59+
pub mod spec;
5960
mod bson;
6061
mod encoder;
6162
mod decoder;

src/macros.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[macro_export]
2+
macro_rules! add_to_doc {
3+
($doc:expr, $key:expr => ($val:expr)) => {{
4+
$doc.insert($key.to_owned(), ::std::convert::From::from($val));
5+
}};
6+
7+
($doc:expr, $key:expr => [$($val:expr),*]) => {{
8+
let vec = vec![$(::std::convert::From::from($val)),*];
9+
$doc.insert($key.to_owned(), $crate::Bson::Array(vec));
10+
}};
11+
12+
($doc:expr, $key:expr => { $($k:expr => $v:tt),* }) => {{
13+
$doc.insert($key.to_owned(), $crate::Bson::Document(doc! {
14+
$(
15+
$k => $v
16+
),*
17+
}));
18+
}};
19+
}
20+
21+
#[macro_export]
22+
macro_rules! doc {
23+
( $($key:expr => $val:tt),* ) => {{
24+
let mut document = $crate::Document::new();
25+
26+
$(
27+
add_to_doc!(document, $key => $val);
28+
)*
29+
30+
document
31+
}};
32+
}

src/oid.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern {
3030
fn gethostname(name: *mut libc::c_char, size: libc::size_t) -> libc::c_int;
3131
}
3232

33+
/// Errors that can occur during OID construction and generation.
3334
#[derive(Debug)]
3435
pub enum Error {
3536
ArgumentError(String),
@@ -50,6 +51,7 @@ impl From<io::Error> for Error {
5051
}
5152
}
5253

54+
/// Alias for Result<T, oid::Error>.
5355
pub type Result<T> = result::Result<T, Error>;
5456

5557
impl fmt::Display for Error {
@@ -83,6 +85,7 @@ impl error::Error for Error {
8385
}
8486
}
8587

88+
/// A wrapper around raw 12-byte ObjectId representations.
8689
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
8790
pub struct ObjectId {
8891
id: [u8; 12],
@@ -107,6 +110,7 @@ impl ObjectId {
107110
Ok(ObjectId::with_bytes(buf))
108111
}
109112

113+
/// Constructs a new ObjectId wrapper around the raw byte representation.
110114
pub fn with_bytes(bytes: [u8; 12]) -> ObjectId {
111115
ObjectId {
112116
id: bytes,
@@ -136,6 +140,7 @@ impl ObjectId {
136140
ObjectId::with_bytes(buf)
137141
}
138142

143+
/// Returns the raw byte representation of an ObjectId.
139144
pub fn bytes(&self) -> [u8; 12] {
140145
self.id
141146
}

src/ordered.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -193,45 +193,3 @@ impl OrderedDocument {
193193
self.document.remove(key)
194194
}
195195
}
196-
197-
#[cfg(test)]
198-
mod test {
199-
use super::OrderedDocument;
200-
use bson::Bson;
201-
202-
#[test]
203-
fn ordered_insert() {
204-
let mut doc = OrderedDocument::new();
205-
doc.insert("first".to_owned(), Bson::I32(1));
206-
doc.insert("second".to_owned(), Bson::String("foo".to_owned()));
207-
doc.insert("alphanumeric".to_owned(), Bson::String("bar".to_owned()));
208-
209-
let expected_keys = vec!(
210-
"first".to_owned(),
211-
"second".to_owned(),
212-
"alphanumeric".to_owned(),
213-
);
214-
215-
let keys: Vec<_> = doc.iter().map(|(key, _)| key.to_owned()).collect();
216-
assert_eq!(expected_keys, keys);
217-
}
218-
219-
#[test]
220-
fn remove() {
221-
let mut doc = OrderedDocument::new();
222-
doc.insert("first".to_owned(), Bson::I32(1));
223-
doc.insert("second".to_owned(), Bson::String("foo".to_owned()));
224-
doc.insert("alphanumeric".to_owned(), Bson::String("bar".to_owned()));
225-
226-
assert!(doc.remove("second").is_some());
227-
assert!(doc.remove("none").is_none());
228-
229-
let expected_keys = vec!(
230-
"first",
231-
"alphanumeric",
232-
);
233-
234-
let keys: Vec<_> = doc.iter().map(|(key, _)| key.to_owned()).collect();
235-
assert_eq!(expected_keys, keys);
236-
}
237-
}

tests/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
#[macro_use(add_to_doc, doc)]
12
extern crate bson;
23
extern crate rustc_serialize;
34

4-
mod oid;
5+
mod modules;

tests/modules/encoder.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
extern crate bson;
2+
3+
use bson::{Document, Bson, encode_document};
4+
5+
#[test]
6+
fn test_encode_floating_point() {
7+
let src = 1020.123;
8+
let dst = [18, 0, 0, 0, 1, 107, 101, 121, 0, 68, 139, 108, 231, 251, 224, 143, 64, 0];
9+
10+
let mut doc = Document::new();
11+
doc.insert("key".to_owned(), Bson::FloatingPoint(src));
12+
13+
let mut buf = Vec::new();
14+
encode_document(&mut buf, &doc).unwrap();
15+
16+
assert_eq!(&buf, &dst);
17+
}
18+
19+
#[test]
20+
fn test_encode_utf8_string() {
21+
let src = "test你好吗".to_owned();
22+
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];
23+
24+
let mut doc = Document::new();
25+
doc.insert("key".to_owned(), Bson::String(src));
26+
27+
let mut buf = Vec::new();
28+
encode_document(&mut buf, &doc).unwrap();
29+
30+
assert_eq!(&buf, &dst);
31+
}
32+
33+
#[test]
34+
fn test_encode_array() {
35+
let src = vec![Bson::FloatingPoint(1.01), Bson::String("xyz".to_owned())];
36+
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];
37+
38+
let mut doc = Document::new();
39+
doc.insert("key".to_owned(), Bson::Array(src));
40+
41+
let mut buf = Vec::new();
42+
encode_document(&mut buf, &doc).unwrap();
43+
44+
assert_eq!(&buf[..], &dst[..]);
45+
}

tests/modules/macros.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use bson::Bson;
2+
3+
#[test]
4+
fn recursive_macro() {
5+
let doc = doc! {
6+
"a" => ("foo"),
7+
"b" => {
8+
"bar" => {
9+
"harbor" => ["seal", false],
10+
"jelly" => (42.0)
11+
},
12+
"grape" => (27)
13+
},
14+
"c" => [-7]
15+
};
16+
17+
match doc.get("a") {
18+
Some(&Bson::String(ref s)) => assert_eq!("foo", s),
19+
_ => panic!("String 'foo' was not inserted correctly."),
20+
}
21+
22+
// Inner Doc 1
23+
match doc.get("b") {
24+
Some(&Bson::Document(ref doc)) => {
25+
26+
// Inner doc 2
27+
match doc.get("bar") {
28+
Some(&Bson::Document(ref inner_doc)) => {
29+
30+
// Inner array
31+
match inner_doc.get("harbor") {
32+
Some(&Bson::Array(ref arr)) => {
33+
assert_eq!(2, arr.len());
34+
35+
// Match array items
36+
match arr[0] {
37+
Bson::String(ref s) => assert_eq!("seal", s),
38+
_ => panic!("String 'seal' was not inserted into inner array correctly."),
39+
}
40+
match arr[1] {
41+
Bson::Boolean(ref b) => assert!(!b),
42+
_ => panic!("Boolean 'false' was not inserted into inner array correctly."),
43+
}
44+
},
45+
_ => panic!("Inner array was not inserted correctly."),
46+
}
47+
48+
// Inner floating point
49+
match inner_doc.get("jelly") {
50+
Some(&Bson::FloatingPoint(ref fp)) => assert_eq!(42.0, *fp),
51+
_ => panic!("Floating point 42.0 was not inserted correctly."),
52+
}
53+
},
54+
_ => panic!("Second inner document was not inserted correctly."),
55+
}
56+
},
57+
_ => panic!("Inner document was not inserted correctly."),
58+
}
59+
60+
// Outer array
61+
match doc.get("c") {
62+
Some(&Bson::Array(ref arr)) => {
63+
assert_eq!(1, arr.len());
64+
65+
// Integer type
66+
match arr[0] {
67+
Bson::I32(ref i) => assert_eq!(-7, *i),
68+
_ => panic!("I32 '-7' was not inserted correctly."),
69+
}
70+
},
71+
_ => panic!("Array was not inserted correctly."),
72+
}
73+
}

0 commit comments

Comments
 (0)