| 1 |
#![allow(unused_macro_rules)] |
| 2 |
|
| 3 |
macro_rules! json_str { |
| 4 |
([]) => { |
| 5 |
"[]" |
| 6 |
}; |
| 7 |
([ $e0:tt $(, $e:tt)* $(,)? ]) => { |
| 8 |
concat!("[", |
| 9 |
json_str!($e0), |
| 10 |
$(",", json_str!($e),)* |
| 11 |
"]") |
| 12 |
}; |
| 13 |
({}) => { |
| 14 |
"{}" |
| 15 |
}; |
| 16 |
({ $k0:tt : $v0:tt $(, $k:tt : $v:tt)* $(,)? }) => { |
| 17 |
concat!("{", |
| 18 |
stringify!($k0), ":", json_str!($v0), |
| 19 |
$(",", stringify!($k), ":", json_str!($v),)* |
| 20 |
"}") |
| 21 |
}; |
| 22 |
(($other:tt)) => { |
| 23 |
$other |
| 24 |
}; |
| 25 |
($other:tt) => { |
| 26 |
stringify!($other) |
| 27 |
}; |
| 28 |
} |
| 29 |
|
| 30 |
macro_rules! pretty_str { |
| 31 |
($json:tt) => { |
| 32 |
pretty_str_impl!("", $json) |
| 33 |
}; |
| 34 |
} |
| 35 |
|
| 36 |
macro_rules! pretty_str_impl { |
| 37 |
($indent:expr, []) => { |
| 38 |
"[]" |
| 39 |
}; |
| 40 |
($indent:expr, [ $e0:tt $(, $e:tt)* $(,)? ]) => { |
| 41 |
concat!("[\n ", |
| 42 |
$indent, pretty_str_impl!(concat!(" ", $indent), $e0), |
| 43 |
$(",\n ", $indent, pretty_str_impl!(concat!(" ", $indent), $e),)* |
| 44 |
"\n", $indent, "]") |
| 45 |
}; |
| 46 |
($indent:expr, {}) => { |
| 47 |
"{}" |
| 48 |
}; |
| 49 |
($indent:expr, { $k0:tt : $v0:tt $(, $k:tt : $v:tt)* $(,)? }) => { |
| 50 |
concat!("{\n ", |
| 51 |
$indent, stringify!($k0), ": ", pretty_str_impl!(concat!(" ", $indent), $v0), |
| 52 |
$(",\n ", $indent, stringify!($k), ": ", pretty_str_impl!(concat!(" ", $indent), $v),)* |
| 53 |
"\n", $indent, "}") |
| 54 |
}; |
| 55 |
($indent:expr, ($other:tt)) => { |
| 56 |
$other |
| 57 |
}; |
| 58 |
($indent:expr, $other:tt) => { |
| 59 |
stringify!($other) |
| 60 |
}; |
| 61 |
} |