Skip to content

Commit eb133f0

Browse files
saghmkyeah
authored andcommitted
Add Macros
1 parent 5e2f575 commit eb133f0

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

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+
}

tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[macro_use(add_to_doc, doc)]
12
extern crate bson;
23
extern crate rustc_serialize;
34

tests/modules/macros.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use bson::{self, Bson, Document};
2+
3+
fn print_doc_with_indent_level(doc: &Document, n: i32) {
4+
macro_rules! print_spaces {
5+
( $n:expr ) => {
6+
for _ in 0..$n {
7+
print!(" ");
8+
}
9+
};
10+
}
11+
12+
println!("{{");
13+
14+
for (key, value) in doc.iter() {
15+
print_spaces!(n + 4);
16+
print!("{}: ", key);
17+
18+
print_bson_with_indent_level(value, n);
19+
println!("");
20+
}
21+
22+
print_spaces!(n);
23+
println!("}}");
24+
}
25+
26+
#[macro_export]
27+
macro_rules! print_doc {
28+
( $doc:expr ) => {
29+
print_doc_with_indent_level($doc, 0)
30+
};
31+
}
32+
33+
fn print_bson_with_indent_level(bson: &Bson, n: i32) {
34+
match bson {
35+
&Bson::Document(ref d) => print_doc_with_indent_level(&d, n + 4),
36+
&Bson::String(ref s) => print!("\"{}\",", s),
37+
&Bson::Boolean(b) => print!("{},", b),
38+
&Bson::Array(ref v) => {
39+
print!("[ ");
40+
41+
for b in v {
42+
print_bson_with_indent_level(b, n);
43+
print!(" ");
44+
}
45+
46+
print!("]");
47+
},
48+
ref bson => print!("{:?},", bson)
49+
};
50+
}
51+
52+
53+
#[test]
54+
fn recursive_macro() {
55+
let doc = doc! {
56+
"a" => ("foo"),
57+
"b" => {
58+
"bar" => {
59+
"harbor" => ["seal", false],
60+
"jelly" => (42.0)
61+
},
62+
"grape" => (27)
63+
},
64+
"c" => [-7]
65+
};
66+
67+
print_doc!(&doc);
68+
}

tests/modules/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
mod encoder;
2+
mod macros;
23
mod oid;
34
mod ordered;

0 commit comments

Comments
 (0)