mirror of
https://github.com/LibreQoE/LibreQoS.git
synced 2025-02-25 18:55:32 -06:00
Support dashes in generated enums by replacing - with _ during
the match phase. Enum options have to replace minus with underscore.
E.g. `ack-filter` becomes `ack_filter`.
So you can do:
`string_table_enum!(DashingEnum, option_1, option2);`
And read `DashingEnum::from_str("option-1")`
This commit is contained in:
@@ -11,7 +11,7 @@ macro_rules! string_table_enum {
|
|||||||
impl $enum_name {
|
impl $enum_name {
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
fn from_str(s: &str) -> Self {
|
fn from_str(s: &str) -> Self {
|
||||||
match s {
|
match s.replace("-", "_").as_str() {
|
||||||
$(
|
$(
|
||||||
stringify!($option) => Self::$option,
|
stringify!($option) => Self::$option,
|
||||||
)*
|
)*
|
||||||
@@ -41,6 +41,7 @@ mod test {
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
string_table_enum!(MyEnum, option1, option2);
|
string_table_enum!(MyEnum, option1, option2);
|
||||||
|
string_table_enum!(DashingEnum, option_1, option2);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_enum_creation() {
|
fn test_enum_creation() {
|
||||||
@@ -53,4 +54,10 @@ mod test {
|
|||||||
let n = MyEnum::from_str("i want sausages");
|
let n = MyEnum::from_str("i want sausages");
|
||||||
assert_eq!(n, MyEnum::Unknown);
|
assert_eq!(n, MyEnum::Unknown);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_enum_with_dash() {
|
||||||
|
let n = DashingEnum::from_str("option-1");
|
||||||
|
assert_eq!(n, DashingEnum::option_1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user