Any crate to extract enum into runtime data?

I was amazed by GitHub - google/argh: Rust derive-based argument parsing optimized for code size which provides macro that we define a struct/enum and all logic are generated so we can run single line of call to parse vector of strings into a structure we want. I was trying to make similar thing to parse a string.

And the problem came to me that I need some macro to extract an enum into data that runtime could inspect.

for example I have:

enum A {
  B,
  C(String),
  D(String, u16),
  E(Result<String, String>),
}

I need some data from the structure:

vec![
  vec!["B"],
  vec!["C", "String"],
  vec!["C", "String", "u16"],
  vec!["D", "Result", ] // or even some nested structure
]

might be possible to do that with macros like in argh, but I could not figure out how to really make it work by now. Also I know strum but it does not work with arguments in variant.

Is there any crate that provides similar feature that I could use to extract such data from enum?

If you want to parse Rust code, use syn. It outputs a proper AST that you can manipulate easily.

sure, proc_macro_derive gets TokenStream and it relies on syn to match and generate code that implements extra methods.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.