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 // But you can disable the messages.
23 if variant_properties.disabled.is_some() {
24 continue;
25 }
26
27 let params = match variant.fields {
28 Fields::Unit => quote! {},
29 Fields::Unnamed(..) => quote! { (..) },
30 Fields::Named(..) => quote! { {..} },
31 };
32
33 for (key, value) in variant_properties.string_props {
34 string_arms.push(quote! { #key => ::core::option::Option::Some( #value )});
35 }
36
37 string_arms.push(quote! { _ => ::core::option::Option::None });
38
39 arms.push(quote! {
40 &#name::#ident #params => {
41 match prop {
42 #(#string_arms),*
43 }
44 }
45 });
46 }
47
48 if arms.len() < variants.len() {
49 arms.push(quote! { _ => ::core::option::Option::None });
50 }
51
52 Ok(quote! {
53 impl #impl_generics #strum_module_path::EnumProperty for #name #ty_generics #where_clause {
54 fn get_str(&self, prop: &str) -> ::core::option::Option<&'static str> {
55 match self {
56 #(#arms),*
57 }
58 }
59 }
60 })
61}
62