#[macro_export]
macro_rules! spelled {
(@value $variant:ident) => {
stringify!($variant)
};
(@value $variant:ident $value:tt) => {
$value
};
(
$(#[$meta:meta])*
$vis:vis enum $enum:ident {
$(#[$get_doc:meta])*
$get_vis:vis fn $get:ident(self) -> $value:ty;
$(#[$of_doc:meta])*
$of_vis:vis fn $of:ident($given:ty);
$($(#[$doc:meta])* $variant:ident $(= $spelling:tt)?,)+
}
) => {
$(#[$meta])*
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
$vis enum $enum {
$($(#[$doc])* $variant,)+
}
impl $enum {
$vis const ALL: &'static [Self] = &[$($enum::$variant,)+];
}
$crate::spelled! {
impl $enum {
$(#[$get_doc])*
$get_vis fn $get(self) -> $value;
$(#[$of_doc])*
$of_vis fn $of($given);
$([$enum::$variant] = ($crate::spelled!(@value $variant $($spelling)?)),)+
}
}
};
(
$(#[$meta:meta])*
$vis:vis enum $enum:ident {
$(#[$get_doc:meta])*
$get_vis:vis fn $get:ident(self) -> $value:ty;
$(#[$of_doc:meta])*
$of_vis:vis fn $of:ident($given:ty);
$($(#[$doc:meta])* $variant:ident = $spelling:path,)+
}
) => {
$crate::spelled! {
$(#[$meta])*
$vis enum $enum {
$(#[$get_doc])*
$get_vis fn $get(self) -> $value;
$(#[$of_doc])*
$of_vis fn $of($given);
$($(#[$doc])* $variant = $spelling,)+
}
}
};
(
impl $type:ty {
$(#[$get_doc:meta])*
$get_vis:vis fn $get:ident(self) -> $value:ty;
$(#[$of_doc:meta])*
$of_vis:vis fn $of:ident($given:ty);
$([$($case:tt)+] = $spelling:tt,)+
}
) => {
impl $type {
$(#[$get_doc])*
$get_vis const fn $get(self) -> $value {
match self {
$($($case)+ => $spelling,)+
}
}
$(#[$of_doc])*
$of_vis fn $of(value: $given) -> Option<Self> {
match value {
$($spelling => Some($($case)+),)+
_ => None,
}
}
}
};
}