#[macro_export]
macro_rules! impl_error_display {
(unconditional $error_type:ident { $($body:tt)* }) => {
$crate::impl_error_display!(@impls $error_type { $($body)* });
};
($error_type:ident { $($body:tt)* }) => {
#[cfg(not(feature = "derive"))]
$crate::impl_error_display!(@impls $error_type { $($body)* });
};
(@impls $error_type:ident { $($(#[$vattr:meta])* $variant:ident $(($($tuple_field:ident),*))? $({ $($struct_field:ident),* })? => $fmt:expr),* $(,)? }) => {
impl core::fmt::Display for $error_type {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
$(
$(#[$vattr])*
$crate::impl_error_display!(@pattern $error_type :: $variant $(($($tuple_field),*))? $({ $($struct_field),* })?) =>
write!(f, $fmt $(, $($tuple_field = $tuple_field),*)? $(, $($struct_field = $struct_field),*)?),
)*
}
}
}
impl core::error::Error for $error_type {}
};
(@pattern $error_type:ident :: $variant:ident) => {
$error_type::$variant
};
(@pattern $error_type:ident :: $variant:ident ($($tuple_field:ident),*)) => {
$error_type::$variant($($tuple_field),*)
};
(@pattern $error_type:ident :: $variant:ident { $($struct_field:ident),* }) => {
$error_type::$variant { $($struct_field),* }
};
}