Macro value

Source
macro_rules! value {
    (@array [$($elems:expr,)*]) => { ... };
    (@array [$($elems:expr),*]) => { ... };
    (@array [$($elems:expr,)*] nil $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] true $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] false $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] $last:expr) => { ... };
    (@array [$($elems:expr),*] , $($rest:tt)*) => { ... };
    (@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => { ... };
    (@map $map:ident () () ()) => { ... };
    (@map $map:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => { ... };
    (@map $map:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => { ... };
    (@map $map:ident [$($key:tt)+] ($value:expr)) => { ... };
    (@map $map:ident ($($key:tt)+) (: nil $($rest:tt)*) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)+) (: {$($inner:tt)*} $($rest:tt)*) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)+) (: $value:expr) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)+) (:) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)+) () $copy:tt) => { ... };
    (@map $map:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => { ... };
    (@map $map:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => { ... };
    (@map $map:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => { ... };
    (@map $map:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => { ... };
    (nil) => { ... };
    (true) => { ... };
    (false) => { ... };
    ([]) => { ... };
    ([ $($tt:tt)+ ]) => { ... };
    ({}) => { ... };
    ({ $($tt:tt)+ }) => { ... };
    ($other:expr) => { ... };
}
Expand description

Construct an expr::Value from a json-like literal.

Compared to json:

  • Trailing commas are allowed
  • nil is used instead of null
let value = expr::value!({
    "code": 200,
    "success": true,
    "payload": {
        "features": [
            "expr",
            "macro",
        ],
        "homepage": nil,
    },
});

Variables or expressions can be interpolated into the literal. Any type interpolated into an array element or map value must implement Into, while any type interpolated into a map key must implement Into<String>. If the interpolated type contains a map with non-string keys, the value! macro will panic.

let code = 200;
let features = vec!["expr", "macro"];

let value = expr::value!({
    "code": code,
    "success": code == 200,
    "payload": {
        features[0]: features[1],
    },
});