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:
Herbert Wolverson
2023-01-17 21:41:59 +00:00
parent d83c1dc852
commit c6661520dd

View File

@@ -11,7 +11,7 @@ macro_rules! string_table_enum {
impl $enum_name {
#[allow(unused)]
fn from_str(s: &str) -> Self {
match s {
match s.replace("-", "_").as_str() {
$(
stringify!($option) => Self::$option,
)*
@@ -41,6 +41,7 @@ mod test {
use serde::{Serialize, Deserialize};
string_table_enum!(MyEnum, option1, option2);
string_table_enum!(DashingEnum, option_1, option2);
#[test]
fn test_enum_creation() {
@@ -53,4 +54,10 @@ mod test {
let n = MyEnum::from_str("i want sausages");
assert_eq!(n, MyEnum::Unknown);
}
#[test]
fn test_enum_with_dash() {
let n = DashingEnum::from_str("option-1");
assert_eq!(n, DashingEnum::option_1);
}
}