1 | use proc_macro2::TokenStream; |
2 | use quote::quote; |
3 | use syn::{Data, DeriveInput}; |
4 | |
5 | use crate::helpers::{non_enum_error, HasStrumVariantProperties, HasTypeProperties}; |
6 | |
7 | pub fn enum_variant_names_inner(ast: &DeriveInput) -> syn::Result<TokenStream> { |
8 | let name = &ast.ident; |
9 | let gen = &ast.generics; |
10 | let (impl_generics, ty_generics, where_clause) = gen.split_for_impl(); |
11 | |
12 | let variants = match &ast.data { |
13 | Data::Enum(v) => &v.variants, |
14 | _ => return Err(non_enum_error()), |
15 | }; |
16 | |
17 | // Derives for the generated enum |
18 | let type_properties = ast.get_type_properties()?; |
19 | let strum_module_path = type_properties.crate_module_path(); |
20 | |
21 | let names = variants |
22 | .iter() |
23 | .map(|v| { |
24 | let props = v.get_variant_properties()?; |
25 | Ok(props.get_preferred_name(type_properties.case_style)) |
26 | }) |
27 | .collect::<syn::Result<Vec<_>>>()?; |
28 | |
29 | Ok(quote! { |
30 | impl #impl_generics #strum_module_path::VariantNames for #name #ty_generics #where_clause { |
31 | const VARIANTS: &'static [&'static str] = &[ #(#names),* ]; |
32 | } |
33 | }) |
34 | } |
35 | |