| 1 | use proc_macro2::TokenStream; |
| 2 | use quote::quote; |
| 3 | use syn::{Data, DeriveInput}; |
| 4 | |
| 5 | use crate::helpers::{non_enum_error, HasTypeProperties}; |
| 6 | |
| 7 | pub(crate) fn enum_count_inner(ast: &DeriveInput) -> syn::Result<TokenStream> { |
| 8 | let n: usize = match &ast.data { |
| 9 | Data::Enum(v: &DataEnum) => v.variants.len(), |
| 10 | _ => return Err(non_enum_error()), |
| 11 | }; |
| 12 | let type_properties: StrumTypeProperties = ast.get_type_properties()?; |
| 13 | let strum_module_path: Path = type_properties.crate_module_path(); |
| 14 | |
| 15 | // Used in the quasi-quotation below as `#name` |
| 16 | let name: &Ident = &ast.ident; |
| 17 | |
| 18 | // Helper is provided for handling complex generic types correctly and effortlessly |
| 19 | let (impl_generics: ImplGenerics<'_>, ty_generics: TypeGenerics<'_>, where_clause: Option<&WhereClause>) = ast.generics.split_for_impl(); |
| 20 | |
| 21 | Ok(quote! { |
| 22 | // Implementation |
| 23 | impl #impl_generics #strum_module_path::EnumCount for #name #ty_generics #where_clause { |
| 24 | const COUNT: usize = #n; |
| 25 | } |
| 26 | }) |
| 27 | } |
| 28 | |