1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::{Data, DeriveInput, Fields};
4
5use crate::helpers::{non_enum_error, HasStrumVariantProperties, HasTypeProperties};
6
7pub 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 let params = match variant.fields {
30 Fields::Unit => quote! {},
31 Fields::Unnamed(..) => quote! { (..) },
32 Fields::Named(..) => quote! { {..} },
33 };
34
35 for (key, value) in variant_properties.string_props {
36 string_arms.push(quote! { #key => ::core::option::Option::Some( #value )});
37 }
38
39 string_arms.push(quote! { _ => ::core::option::Option::None });
40 bool_arms.push(quote! { _ => ::core::option::Option::None });
41 num_arms.push(quote! { _ => ::core::option::Option::None });
42
43 arms.push(quote! {
44 &#name::#ident #params => {
45 match prop {
46 #(#string_arms),*
47 }
48 }
49 });
50 }
51
52 if arms.len() < variants.len() {
53 arms.push(quote! { _ => ::core::option::Option::None });
54 }
55
56 Ok(quote! {
57 impl #impl_generics #strum_module_path::EnumProperty for #name #ty_generics #where_clause {
58 fn get_str(&self, prop: &str) -> ::core::option::Option<&'static str> {
59 match self {
60 #(#arms),*
61 }
62 }
63 }
64 })
65}
66