0

Say I have some external library code (that is also generated from a procedural macro itself):

struct Type1;
struct Type2<'a> (&'a str);
struct Type3<'a> (&'a str);

// Expanded definition from a proc macro
enum Enum<'a> {
    A,
    B,
    C(Type1),
    D,
    E(Type2<'a>),
    F,
    G(Type3<'a>),
    H(Type1),
    I
}

I want to use procedural macros to generate the following:

// These types are hand-written
struct Type1; 
struct Type2 (String);
struct Type3 (String);

// Generated from procedural macros and pattern-matching?
enum Enum {
    A,
    B,
    C(Type1),
    D,
    E(Type2),
    F,
    G(Type3),
    H(Type1),
    I
}

Is this possible?

Here, the comparison between '&str and String is a toy example but in reality could be an arbitrarily complex type that needs manual conversion.

gust
  • 878
  • 9
  • 23
  • Sounds more like the usecase for a [build script](https://doc.rust-lang.org/cargo/reference/build-scripts.html) but yea you can read files, and output any valid rust code based on them in a proc macro. If you have to expand macros first you can use something like [cargo-expand](https://github.com/dtolnay/cargo-expand) to do it. – cafce25 Mar 09 '23 at 23:32
  • Does this answer your question? [Can a macro access the field names and types of a remote struct from its path?](https://stackoverflow.com/questions/73912766/can-a-macro-access-the-field-names-and-types-of-a-remote-struct-from-its-path) – Caesar Mar 10 '23 at 00:07
  • Do you want to wrap the structs in the macro (in which case it's definitely possible) or to access them remotely (in which case it is mostly not)? – Chayim Friedman Mar 10 '23 at 02:12

0 Answers0