1 use proc_macro2::TokenStream;
2 use quote::quote;
3 use syn::{Data, DeriveInput};
4 
5 use crate::helpers::{non_enum_error, HasStrumVariantProperties, HasTypeProperties};
6 
enum_properties_inner(ast: &DeriveInput) -> syn::Result<TokenStream>7 pub fn enum_properties_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
8     let name = &ast.ident;
9     let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
10     let variants = match &ast.data {
11         Data::Enum(v) => &v.variants,
12         _ => return Err(non_enum_error()),
13     };
14     let type_properties = ast.get_type_properties()?;
15     let strum_module_path = type_properties.crate_module_path();
16 
17     let mut arms = Vec::new();
18     for variant in variants {
19         let ident = &variant.ident;
20         let variant_properties = variant.get_variant_properties()?;
21         let mut string_arms = Vec::new();
22         let mut bool_arms = Vec::new();
23         let mut num_arms = Vec::new();
24         // But you can disable the messages.
25         if variant_properties.disabled.is_some() {
26             continue;
27         }
28 
29         use syn::Fields::*;
30         let params = match variant.fields {
31             Unit => quote! {},
32             Unnamed(..) => quote! { (..) },
33             Named(..) => quote! { {..} },
34         };
35 
36         for (key, value) in variant_properties.string_props {
37             string_arms.push(quote! { #key => ::core::option::Option::Some( #value )})
38         }
39 
40         string_arms.push(quote! { _ => ::core::option::Option::None });
41         bool_arms.push(quote! { _ => ::core::option::Option::None });
42         num_arms.push(quote! { _ => ::core::option::Option::None });
43 
44         arms.push(quote! {
45             &#name::#ident #params => {
46                 match prop {
47                     #(#string_arms),*
48                 }
49             }
50         });
51     }
52 
53     if arms.len() < variants.len() {
54         arms.push(quote! { _ => ::core::option::Option::None });
55     }
56 
57     Ok(quote! {
58         impl #impl_generics #strum_module_path::EnumProperty for #name #ty_generics #where_clause {
59             fn get_str(&self, prop: &str) -> ::core::option::Option<&'static str> {
60                 match self {
61                     #(#arms),*
62                 }
63             }
64         }
65     })
66 }
67