Add the gegl_enum macro to generate enums with Display implemented.

This commit is contained in:
Bauke 2024-01-23 18:06:24 +01:00
parent f7e91d69cc
commit 263e9131c0
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 28 additions and 0 deletions

View File

@ -43,3 +43,31 @@ macro_rules! gegl_operation {
} }
}; };
} }
/// A macro to generate an enum that implements [`std::fmt::Display`].
#[macro_export]
macro_rules! gegl_enum {
(
$enum_name:ident,
$($key:ident: $value:expr),*,
) => {
#[doc = "TODO: Generate documentation for [`gegl_enum!`]."]
#[derive(Debug)]
pub enum $enum_name {
$(
#[doc = "TODO: Generate documentation for [`gegl_enum!`]."]
$key,
)*
}
impl std::fmt::Display for $enum_name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = match self {
$($enum_name::$key => $value,)*
};
write!(f, "{display}")
}
}
};
}