1 | use proc_macro2::TokenStream; |
2 | use quote::quote; |
3 | use syn::{Data, DeriveInput, LitStr}; |
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 |
26 | .get_preferred_name(type_properties.case_style, type_properties.prefix.as_ref())) |
27 | }) |
28 | .collect::<syn::Result<Vec<LitStr>>>()?; |
29 | |
30 | Ok(quote! { |
31 | impl #impl_generics #strum_module_path::VariantNames for #name #ty_generics #where_clause { |
32 | const VARIANTS: &'static [&'static str] = &[ #(#names),* ]; |
33 | } |
34 | }) |
35 | } |
36 | |