#[macro_export]
macro_rules! kvfmt_inner {
([$fmt:expr] [$($args:tt)*]) => {
format_args!($fmt, $( $args )*)
};
([""] [$($args:tt)*] ?$name:expr $(, $($t:tt)*)?) => {
$crate::kvfmt_inner!([concat!(stringify!($name), "={:?}")] [$( $args )* $name] $($($t)*)?);
};
([""] [$($args:tt)*] $name:expr $(, $($t:tt)*)?) => {
$crate::kvfmt_inner!([concat!(stringify!($name), "={}")] [$( $args )* $name ] $($($t)*)?);
};
([$fmt:expr] [$($args:tt)*] ?$name:expr $(, $($t:tt)*)?) => {
$crate::kvfmt_inner!([concat!($fmt, " ", stringify!($name), "={:?}")] [$( $args )*, $name] $($($t)*)?);
};
([$fmt:expr] [$($args:tt)*] $name:expr $(, $($t:tt)*)?) => {
$crate::kvfmt_inner!([concat!($fmt, " ", stringify!($name), "={}")] [$( $args )*, $name ] $($($t)*)?);
};
}
#[macro_export]
macro_rules! kvfmt {
($($t:tt)*) => {
std::fmt::format($crate::kvfmt_inner!([""] [] $($t)*));
};
}
#[cfg(test)]
mod tests {
#[test]
fn kv() {
let hi = "henlo!";
assert!(!kvfmt!(?hi, ?hi).is_empty());
assert!(!kvfmt!(hi, hi).is_empty());
assert!(!kvfmt!(?hi, hi).is_empty());
assert!(!kvfmt!(hi, hi, hi).is_empty());
assert!(!kvfmt!(hi, ?hi, hi).is_empty());
assert_eq!("hi=henlo! hi=\"henlo!\"", kvfmt!(hi, ?hi));
}
}