Skip to content

Instantly share code, notes, and snippets.

@magistau
Created April 18, 2025 18:27
Show Gist options
  • Save magistau/ff705fef2b7f41306eff2a94792a343a to your computer and use it in GitHub Desktop.
Save magistau/ff705fef2b7f41306eff2a94792a343a to your computer and use it in GitHub Desktop.
#[macro_export]
macro_rules! opt_metavar {
(; $default:tt) => {
$default
};
($opt:tt; $default:tt) => {
$opt
};
}
#[macro_export]
macro_rules! unfmt_match {
(
$scrutinee:expr;
$(
$fmt:literal $(as $pat:pat)? => $arm:expr,
)*
$(_ => $fbarm:expr $(,)?)?
) => {
#[allow(unused_labels)]
'unfmt_match: {
let input = $scrutinee;
$(
if let Some($crate::opt_metavar!($($pat)?; ())) = ::unfmt::unformat!($fmt, input, true) {
break 'unfmt_match ($arm);
}
)*
$(
$fbarm
)?
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment