2022-01-15 22:14:43 +00:00
|
|
|
use proc_macro::{Span, TokenStream};
|
2021-04-21 23:46:54 +00:00
|
|
|
use quote::quote;
|
|
|
|
use syn::{parse_macro_input, Data, DeriveInput};
|
|
|
|
|
|
|
|
pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream {
|
|
|
|
let ast = parse_macro_input!(input as DeriveInput);
|
|
|
|
let variants = match &ast.data {
|
|
|
|
Data::Enum(v) => &v.variants,
|
2022-01-15 22:14:43 +00:00
|
|
|
_ => {
|
|
|
|
return syn::Error::new(Span::call_site().into(), "Only enums are supported")
|
|
|
|
.into_compile_error()
|
|
|
|
.into()
|
|
|
|
}
|
2021-04-21 23:46:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let generics = ast.generics;
|
|
|
|
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
|
|
|
|
|
|
|
let struct_name = &ast.ident;
|
|
|
|
let idents = variants.iter().map(|v| &v.ident);
|
|
|
|
let names = variants.iter().map(|v| v.ident.to_string());
|
|
|
|
let indices = 0..names.len();
|
|
|
|
|
|
|
|
TokenStream::from(quote! {
|
2022-04-27 19:08:11 +00:00
|
|
|
impl #impl_generics #struct_name #ty_generics #where_clause {
|
|
|
|
pub fn enum_variant_index(&self) -> usize {
|
2021-04-21 23:46:54 +00:00
|
|
|
match self {
|
|
|
|
#(#struct_name::#idents {..} => #indices,)*
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 19:08:11 +00:00
|
|
|
pub fn enum_variant_name(&self) -> &'static str {
|
2021-04-21 23:46:54 +00:00
|
|
|
static variants: &[&str] = &[
|
|
|
|
#(#names,)*
|
|
|
|
];
|
|
|
|
let index = self.enum_variant_index();
|
|
|
|
variants[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|