1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// Construct a BSON value
#[macro_export]
macro_rules! bson {
    ([]) => {{ $crate::bson::Bson::Array(Vec::new()) }};

    ([$($val:tt),*]) => {{
        let mut array = Vec::new();

        $(
            array.push(bson!($val));
        )*

        $crate::bson::Bson::Array(array)
    }};

    ([$val:expr]) => {{
        $crate::bson::Bson::Array(vec!(::std::convert::From::from($val)))
    }};

    ({ $($k:expr => $v:tt),* }) => {{
        $crate::bson::Bson::Document(doc! {
            $(
                $k => $v
            ),*
        })
    }};

    ($val:expr) => {{
        ::std::convert::From::from($val)
    }};
}

/// Construct a BSON Document
#[macro_export]
macro_rules! doc {
    () => {{ $crate::bson::Document::new() }};

    ( $($key:expr => $val:tt),* ) => {{
        let mut document = $crate::bson::Document::new();

        $(
            document.insert_bson($key.to_owned(), bson!($val));
        )*

        document
    }};
}