strum_macros/macros/
enum_variant_names.rs1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::{Data, DeriveInput, LitStr};
4
5use crate::helpers::{non_enum_error, HasStrumVariantProperties, HasTypeProperties};
6
7pub 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 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(
26 type_properties.case_style,
27 type_properties.prefix.as_ref(),
28 type_properties.suffix.as_ref(),
29 ))
30 })
31 .collect::<syn::Result<Vec<LitStr>>>()?;
32
33 Ok(quote! {
34 impl #impl_generics #strum_module_path::VariantNames for #name #ty_generics #where_clause {
35 const VARIANTS: &'static [&'static str] = &[ #(#names),* ];
36 }
37 })
38}