Break dash-supporting enum into its own macro.

This commit is contained in:
Herbert Wolverson 2023-01-17 22:01:56 +00:00
parent c6661520dd
commit e0552ec55b

View File

@ -1,5 +1,43 @@
#[macro_export]
macro_rules! string_table_enum {
($enum_name: ident, $($option:ident),*) => {
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[allow(non_camel_case_types)]
enum $enum_name {
$($option, )*
Unknown
}
impl $enum_name {
#[allow(unused)]
fn from_str(s: &str) -> Self {
match s {
$(
stringify!($option) => Self::$option,
)*
_ => Self::Unknown
}
}
#[allow(unused)]
fn to_str(&self) -> &str {
match self {
$(
Self::$option => stringify!($option),
)*
Self::Unknown => "unknown",
}
}
}
impl Default for $enum_name {
fn default() -> Self { Self::Unknown }
}
};
}
#[macro_export]
macro_rules! dashy_table_enum {
($enum_name: ident, $($option:ident),*) => {
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[allow(non_camel_case_types)]
@ -41,7 +79,7 @@ mod test {
use serde::{Serialize, Deserialize};
string_table_enum!(MyEnum, option1, option2);
string_table_enum!(DashingEnum, option_1, option2);
dashy_table_enum!(DashingEnum, option_1, option2);
#[test]
fn test_enum_creation() {