mirror of
https://github.com/LibreQoE/LibreQoS.git
synced 2024-11-28 10:53:48 -06:00
Break dash-supporting enum into its own macro.
This commit is contained in:
parent
c6661520dd
commit
e0552ec55b
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user