Skip to content

Commit a62d744

Browse files
committed
Refactor bson macro
Remove root parens Add support for arrays with documents
1 parent fd332ca commit a62d744

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

src/macros.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
#[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));
2+
macro_rules! bson {
3+
([$($val:tt),*]) => {{
4+
let mut array = Vec::new();
5+
6+
$(
7+
array.push(bson!($val));
8+
)*
9+
10+
$crate::Bson::Array(array)
511
}};
612

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));
13+
([$val:expr]) => {{
14+
$crate::Bson::Array(vec!(::std::convert::From::from($val)))
1015
}};
1116

12-
($doc:expr, $key:expr => { $($k:expr => $v:tt),* }) => {{
13-
$doc.insert($key.to_owned(), $crate::Bson::Document(doc! {
17+
({ $($k:expr => $v:tt),* }) => {{
18+
$crate::Bson::Document(doc! {
1419
$(
1520
$k => $v
1621
),*
17-
}));
22+
})
23+
}};
24+
25+
($val:expr) => {{
26+
::std::convert::From::from($val)
1827
}};
1928
}
2029

@@ -24,7 +33,7 @@ macro_rules! doc {
2433
let mut document = $crate::Document::new();
2534

2635
$(
27-
add_to_doc!(document, $key => $val);
36+
document.insert($key.to_owned(), bson!($val));
2837
)*
2938

3039
document

tests/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[macro_use(add_to_doc, doc)]
1+
#[macro_use(bson, doc)]
22
extern crate bson;
33
extern crate rustc_serialize;
44

tests/modules/macros.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ use bson::Bson;
33
#[test]
44
fn recursive_macro() {
55
let doc = doc! {
6-
"a" => ("foo"),
6+
"a" => "foo",
77
"b" => {
88
"bar" => {
99
"harbor" => ["seal", false],
10-
"jelly" => (42.0)
10+
"jelly" => 42.0
1111
},
12-
"grape" => (27)
12+
"grape" => 27
1313
},
14-
"c" => [-7]
14+
"c" => [-7],
15+
"d" => [
16+
{
17+
"apple" => "ripe"
18+
}
19+
],
20+
"e" => { "single" => "test" }
1521
};
1622

1723
match doc.get("a") {
@@ -57,7 +63,7 @@ fn recursive_macro() {
5763
_ => panic!("Inner document was not inserted correctly."),
5864
}
5965

60-
// Outer array
66+
// Single-item array
6167
match doc.get("c") {
6268
Some(&Bson::Array(ref arr)) => {
6369
assert_eq!(1, arr.len());
@@ -68,6 +74,40 @@ fn recursive_macro() {
6874
_ => panic!("I32 '-7' was not inserted correctly."),
6975
}
7076
},
77+
_ => panic!("Single-item array was not inserted correctly."),
78+
}
79+
80+
// Document nested in array
81+
match doc.get("d") {
82+
Some(&Bson::Array(ref arr)) => {
83+
assert_eq!(1, arr.len());
84+
85+
// Nested document
86+
match arr[0] {
87+
Bson::Document(ref doc) => {
88+
89+
// String
90+
match doc.get("apple") {
91+
Some(&Bson::String(ref s)) => assert_eq!("ripe", s),
92+
_ => panic!("String 'ripe' was not inserted correctly."),
93+
}
94+
},
95+
_ => panic!("Document was not inserted into array correctly."),
96+
}
97+
},
7198
_ => panic!("Array was not inserted correctly."),
7299
}
100+
101+
// Single-item document
102+
match doc.get("e") {
103+
Some(&Bson::Document(ref bdoc)) => {
104+
105+
// String
106+
match bdoc.get("single") {
107+
Some(&Bson::String(ref s)) => assert_eq!("test", s),
108+
_ => panic!("String 'test' was not inserted correctly."),
109+
}
110+
},
111+
_ => panic!("Single-item document was not inserted correctly."),
112+
}
73113
}

0 commit comments

Comments
 (0)