I have an enum
with over 100 variants. and I have to get each of its variants from a string. Something like this.
enum VeryLongEnum {
A,
B,
C,
D,
E,
}
impl From<&'static str > for VeryLongEnum {
fn from(s: &'static str) -> VeryLongEnum {
match s {
"A" => VeryLongEnum::A,
"B" => VeryLongEnum::B,
"C" => VeryLongEnum::C,
"D" => VeryLongEnum::D,
"E" => VeryLongEnum::E,
}
}
}
But I don't want to write all the variants one by one, it's crazy.
I'm trying to create a mini macro to simplify this, I did something like this.
macro_rules! from_str {
( $s:expr ) => {{
let t: VeryLongEnum = VeryLongEnum::$s;
t
}};
}
to use like this.
let variant = "A";
let en = from_str!(variant);
But I'm having an error which says expected a identifier
.
I understand that identifiers and expresions are different types of token trees, but the question is how can I "force" this to generate the enum variant with the literal?
variant
is a string that exists at runtime, you cannot pass it to a macro like that.An alternative is to define a macro that defines the enum as well as the string conversion. This macro can use the
stringify!
macro in Rust to convert at identifier to a static string that can be passed to pattern match. Since the conversion is fallible, you should define aTryFrom
instead ofFrom
(orFromStr
which allows you to call"A".parse()
).Output:
Playground