I have a use case where I need to insert some fields into an enum variant if it's annotated with a certain attribute. So far I've tried the following approach:
use syn::{parse_macro_input, parse_quote, Data, DeriveInput, Field, Fields, GenericParam, Generics, Index, AttrStyle, Visibility, Token, VisPublic};
// ...
match ast.data {
Data::Enum(data) => {
for variant in data.variants {
let mut is_created = false;
let mut is_votable = false;
for attr in variant.attrs {
if let AttrStyle::Outer = attr.style {
// Not sure if this will work...
if attr.path.is_ident("created") {
is_created = true;
} else if attr.path.is_ident("votable") {
is_votable = true;
}
} else {
panic!("invalid attribute!")
}
}
if is_created {
match variant.fields {
Fields::Named(fields) => {
fields.named.push(
Field::parse_named(
// What do I do here?
).unwrap()
)
}
}
}
}
}
_ => panic!("Bad input data!")
}
Ideally, I'd want to do something like:
// ...
if is_created {
match variant.fields {
Fields::Named(fields) => {
fields.named.push(
Field::parse_named(
parse_quote! {
pub a: u32
}
).unwrap()
)
}
}
}
// ...
But syn::Field
doesn't seem to implement parse...
I'm not quite sure how to proceed here, any ideas?