yaml

Macro yaml 

Source
macro_rules! yaml {
    ({}) => { ... };
    ({ $( $k:expr => $v:expr ),+ } ) => { ... };
    ({ $( $k:expr => $v:expr ),+, } ) => { ... };
    ([]) => { ... };
    ( [ $( $v:expr ),+ ] ) => { ... };
    ( [ $( $v:expr ),+, ] ) => { ... };
    ( $v:expr ) => { ... };
}
Expand description

A lightweight syntax for YAML.


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());