1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::{Data, DeriveInput, Fields};
4
5use crate::helpers::{non_enum_error, non_unit_variant_error, HasTypeProperties};
6
7pub fn static_variants_array_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()?;
18 let strum_module_path = type_properties.crate_module_path();
19
20 let idents = variants
21 .iter()
22 .cloned()
23 .map(|v| match v.fields {
24 Fields::Unit => Ok(v.ident),
25 _ => Err(non_unit_variant_error()),
26 })
27 .collect::<syn::Result<Vec<_>>>()?;
28
29 Ok(quote! {
30 impl #impl_generics #strum_module_path::VariantArray for #name #ty_generics #where_clause {
31 const VARIANTS: &'static [Self] = &[ #(#name::#idents),* ];
32 }
33 })
34}
35