1 use proc_macro2
::TokenStream
;
3 use syn
::{Data, DeriveInput, Fields}
;
5 use crate::helpers
::{non_enum_error, HasStrumVariantProperties, HasTypeProperties}
;
7 pub fn enum_properties_inner(ast
: &DeriveInput
) -> syn
::Result
<TokenStream
> {
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()),
14 let type_properties
= ast
.get_type_properties()?
;
15 let strum_module_path
= type_properties
.crate_module_path();
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() {
29 let params
= match variant
.fields
{
30 Fields
::Unit
=> quote
! {}
,
31 Fields
::Unnamed(..) => quote
! { (..) }
,
32 Fields
::Named(..) => quote
! { {..}
},
35 for (key
, value
) in variant_properties
.string_props
{
36 string_arms
.push(quote
! { #key => ::core::option::Option::Some( #value )}
);
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 }
);
44 &#name::#ident #params => {
52 if arms
.len() < variants
.len() {
53 arms
.push(quote
! { _ => ::core::option::Option::None }
);
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> {