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 48 49 50 51
/// This macro can be used when implementing the [`Serialize`] Trait
///
/// Example:
/// ```rust
/// use std::collections::HashMap;
/// use wjp::map;
/// use wjp::Values;
///
/// let mut map = HashMap::new();
/// map.insert("test".to_string(),Values::Null);
///
/// assert_eq!(
/// map!(("test",Values::Null)),
/// map
/// )
///
/// ```
///
/// [`Serialize`]: crate::serializer::Serialize
#[macro_export]
macro_rules! map (
() => {
std::collections::HashMap::new()
};
($(($key:expr,$value:expr)), + ) => {
{
let mut m = std::collections::HashMap::with_capacity(3);
$(
m.insert(String::from($key), Serialize::serialize($value));
)+
m
}
};
);
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::Serialize;
#[test]
pub fn with_empty_params() {
assert_eq!(map!(), HashMap::<&str, &str>::new())
}
#[test]
pub fn with_filled_params() {
let mut map = HashMap::new();
map.insert(String::from("test"), 123.serialize());
assert_eq!(map!(("test", &123)), map)
}
}