• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use proc_macro2::TokenStream;
2 use quote::quote;
3 use syn::{Data, DeriveInput};
4 
5 use crate::helpers::variant_props::HasStrumVariantProperties;
6 use crate::helpers::{non_enum_error, HasTypeProperties};
7 
enum_count_inner(ast: &DeriveInput) -> syn::Result<TokenStream>8 pub(crate) fn enum_count_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
9     let n = match &ast.data {
10         Data::Enum(v) => v.variants.iter().try_fold(0usize, |acc, v| {
11             if v.get_variant_properties()?.disabled.is_none() {
12                 Ok::<usize, syn::Error>(acc + 1usize)
13             } else {
14                 Ok::<usize, syn::Error>(acc)
15             }
16         })?,
17         _ => return Err(non_enum_error()),
18     };
19     let type_properties = ast.get_type_properties()?;
20     let strum_module_path = type_properties.crate_module_path();
21 
22     // Used in the quasi-quotation below as `#name`
23     let name = &ast.ident;
24 
25     // Helper is provided for handling complex generic types correctly and effortlessly
26     let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
27 
28     Ok(quote! {
29         // Implementation
30         impl #impl_generics #strum_module_path::EnumCount for #name #ty_generics #where_clause {
31             const COUNT: usize = #n;
32         }
33     })
34 }
35