In a procedural macro, how could we generate a match for a specific enum?
enum Enum{
A,
B(u32),
C
}
and generate
match val{
0 => /*code for A*/,
1 => /*code for B*/,
2 =>/* code for C*/
}
I don't care about the discriminants, just the order and the matching data.
I don't need the boilerplate for the procedural macro, but a hint is welcome.
If you wanted to just generate match for an arbitrary val, then that's not possible. Macros run before types are created. Macros have ability to create and modify definitions of types, so any knowledge of the types would create a circular dependency. Macros operate only on the syntax, just the source code they're invoked on.
You will need to parse the enum definition yourself in the macro, and make assumptions about what are the integer discriminants of the fields, or assign some explicitly when generating the enum definition and code to parse. And then you'll have to generate the integer-to-enum code yourself from the macro.