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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/// A generic syntax for dict-like structures.
///
/// Works for HashMap but also for e.g. serde_json or serde_yaml maps.
///
/// ```rust
/// # #[macro_use] extern crate mx_tester;
/// # fn main() {
///
/// use std::collections::HashMap;
///
/// let empty: HashMap<u8, u8> = dict!(HashMap::new(), {});
/// assert_eq!(empty.len(), 0);
///
/// let map: HashMap<u8, u8> = dict!(HashMap::new(), {
/// 0 => 255,
/// 1 => 254,
/// 2 => 253,
/// });
/// assert_eq!(map.len(), 3);
/// assert!(matches!(map.get(&0), Some(255)));
/// assert!(matches!(map.get(&1), Some(254)));
/// assert!(matches!(map.get(&2), Some(253)));
///
/// # }
/// ```
#[macro_export]
macro_rules! dict {
// Empty
( $container: expr, {}) => {
$container
};
// Without trailing `,`.
( $container: expr, { $( $k:expr => $v:expr ),+ } ) => {
dict!($container, { $($k => $v,)* })
};
// With trailing `,`.
( $container: expr, { $( $k:expr => $v:expr ),+, } ) => {
{
let mut container = $container;
$(
container.insert($k.into(), $v.into());
)*
container
}
};
}
/// A generic syntax for seq-like structures.
///
/// Works for Vec but also for serde_json or serde_yaml arrays.
///
/// ```rust
/// # #[macro_use] extern crate mx_tester;
/// # fn main() {
///
/// use std::collections::HashMap;
///
/// let empty: Vec<u8> = seq!(Vec::new(), []);
/// assert_eq!(empty.len(), 0);
///
/// let vec: Vec<u8> = seq!(Vec::new(), [
/// 255,
/// 254,
/// 253,
/// ]);
/// assert_eq!(vec.len(), 3);
/// assert!(matches!(vec.get(0), Some(255)));
/// assert!(matches!(vec.get(1), Some(254)));
/// assert!(matches!(vec.get(2), Some(253)));
///
/// # }
/// ```
#[macro_export]
macro_rules! seq {
// Empty
( $container: expr, []) => {
$container
};
// Without trailing `,`.
( $container: expr, [ $( $v:expr ),+ ] ) => {
seq!($container, [$($v,)* ])
};
// With trailing `,`.
( $container: expr, [ $( $v:expr ),+, ] ) => {
{
let mut container = $container;
$(
container.push($v.into());
)*
container
}
};
}
/// A lightweight syntax for YAML.
///
/// ```rust
/// # #[macro_use] extern crate mx_tester;
/// # fn main() {
///
/// use serde_yaml;
///
/// let empty_map = yaml!({});
/// assert!(empty_map.as_mapping().is_some());
/// assert!(empty_map.as_mapping().unwrap().is_empty());
///
/// let empty_seq = yaml!([]);
/// assert!(empty_seq.as_sequence().is_some());
/// assert!(empty_seq.as_sequence().unwrap().is_empty());
///
/// let five = yaml!(5);
/// assert!(matches!(five.as_u64(), Some(5)));
///
/// let ten = yaml!(10);
///
/// let simple_map = yaml!({
/// 5 => 10 // No trailing comma
/// });
/// assert!(simple_map.as_mapping().is_some());
/// assert_eq!(simple_map.as_mapping().unwrap().len(), 1);
/// assert_eq!(simple_map.as_mapping().unwrap().get(&five).unwrap(), &ten);
///
/// let simple_map_2 = yaml!({
/// 5 => 10, // Trailing comma
/// });
/// assert_eq!(simple_map_2, simple_map);
///
/// let nested_map = yaml!({
/// 5 => 10,
/// 10 => yaml!({ }),
/// });
/// let nested_map_2 = yaml!({
/// 10 => yaml!({ }),
/// 5 => 10
/// });
/// assert_eq!(nested_map, nested_map_2);
///
/// let seq = yaml!([ 5, 5, 10 ]);
/// assert!(seq.as_sequence().is_some());
/// assert_eq!(seq[0], five);
/// assert_eq!(seq[1], five);
/// assert_eq!(seq[2], ten);
/// assert!(seq[3].is_null());
///
/// # }
/// ```
#[macro_export]
macro_rules! yaml {
// Map: empty
({}) => {
serde_yaml::Value::Mapping(dict!(serde_yaml::Mapping::new(), {}))
};
// Map: without trailing `,`.
({ $( $k:expr => $v:expr ),+ } ) => {
serde_yaml::Value::Mapping(dict!(serde_yaml::Mapping::new(), { $($k => $v,)* }))
};
// Map: with trailing `,`.
({ $( $k:expr => $v:expr ),+, } ) => {
serde_yaml::Value::Mapping(dict!(serde_yaml::Mapping::new(), { $($k => $v,)* }))
};
// Sequence: empty
([]) => {
serde_yaml::Value::Sequence(seq!(serde_yaml::Sequence::new(), []))
};
// Sequence: without trailing `,`.
( [ $( $v:expr ),+ ] ) => {
serde_yaml::Value::Sequence(seq!(serde_yaml::Sequence::new(), [$($v,)* ]))
};
// Sequence: with trailing `,`.
( [ $( $v:expr ),+, ] ) => {
serde_yaml::Value::Sequence(seq!(serde_yaml::Sequence::new(), [$($v,)* ]))
};
// Anything else: convert to YAML.
( $v:expr ) => {
serde_yaml::Value::from($v)
}
}
/// Utility extensions to manipulate yaml.
pub trait YamlExt {
/// Convert a yaml subtree into a sequence.
///
/// This works only if the yaml subtree is either null or already a sequence.
fn to_seq_mut(&mut self) -> Option<&mut serde_yaml::Sequence>;
}
impl YamlExt for serde_yaml::Value {
/// Convert a yaml subtree into a sequence.
///
/// This works only if the yaml subtree is either null or already a sequence.
fn to_seq_mut(&mut self) -> Option<&mut serde_yaml::Sequence> {
if self.is_null() {
*self = yaml!([]);
}
self.as_sequence_mut()
}
}
/// Utility function: return `true`.
pub fn true_() -> bool {
true
}