Skip to main content

cbor_core/
macros.rs

1/// Construct a CBOR array from a list of expressions.
2///
3/// Each element is converted into a [`Value`](crate::Value).
4///
5/// ```
6/// # use cbor_core::{array, Value};
7///
8/// let empty = array![];
9/// assert_eq!(empty.len(), Some(0));
10///
11/// let v = array![1, "hello", true];
12/// assert!(v.data_type().is_array());
13/// ```
14#[macro_export]
15macro_rules! array {
16    () => {
17        $crate::Value::Array(::std::vec::Vec::new())
18    };
19    ($($x:expr),+ $(,)?) => {
20        $crate::Value::array([$($crate::Value::from($x)),+])
21    };
22}
23
24/// Construct a CBOR map from a list of `key => value` pairs.
25///
26/// Keys and values are converted into [`Value`](crate::Value).
27///
28/// ```
29/// # use cbor_core::{map, Value};
30///
31/// let empty = map!{};
32/// assert_eq!(empty.len(), Some(0));
33///
34/// let m = map! { "x" => 1, "y" => 2 };
35/// assert!(m.data_type().is_map());
36/// ```
37#[macro_export]
38macro_rules! map {
39    () => {
40        $crate::Value::Map(::std::collections::BTreeMap::new())
41    };
42    ($($k:expr => $v:expr),+ $(,)?) => {{
43        let mut map = ::std::collections::BTreeMap::new();
44        $(map.insert($crate::Value::from($k), $crate::Value::from($v));)+
45        $crate::Value::Map(map)
46    }};
47}