1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Fields, LitStr};
use crate::helpers::{non_enum_error, HasStrumVariantProperties, HasTypeProperties};
pub fn enum_message_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let name = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let variants = match &ast.data {
Data::Enum(v) => &v.variants,
_ => return Err(non_enum_error()),
};
let type_properties = ast.get_type_properties()?;
let strum_module_path = type_properties.crate_module_path();
let mut arms = Vec::new();
let mut detailed_arms = Vec::new();
let mut documentation_arms = Vec::new();
let mut serializations = Vec::new();
for variant in variants {
let variant_properties = variant.get_variant_properties()?;
let messages = variant_properties.message.as_ref();
let detailed_messages = variant_properties.detailed_message.as_ref();
let documentation = &variant_properties.documentation;
let ident = &variant.ident;
let params = match variant.fields {
Fields::Unit => quote! {},
Fields::Unnamed(..) => quote! { (..) },
Fields::Named(..) => quote! { {..} },
};
{
let serialization_variants =
variant_properties.get_serializations(type_properties.case_style);
let count = serialization_variants.len();
serializations.push(quote! {
&#name::#ident #params => {
static ARR: [&'static str; #count] = [#(#serialization_variants),*];
&ARR
}
});
}
if variant_properties.disabled.is_some() {
continue;
}
if let Some(msg) = messages {
let params = params.clone();
let tokens = quote! { &#name::#ident #params => ::core::option::Option::Some(#msg) };
arms.push(tokens.clone());
if detailed_messages.is_none() {
detailed_arms.push(tokens);
}
}
if let Some(msg) = detailed_messages {
let params = params.clone();
detailed_arms
.push(quote! { &#name::#ident #params => ::core::option::Option::Some(#msg) });
}
if !documentation.is_empty() {
let params = params.clone();
let documentation: Vec<LitStr> = documentation.iter().map(|lit_str| {
let line = lit_str.value();
if line.starts_with(' ') {
LitStr::new(&line.as_str()[1..], lit_str.span())
} else {
lit_str.clone()
}
}).collect();
if documentation.len() == 1 {
let text = &documentation[0];
documentation_arms
.push(quote! { &#name::#ident #params => ::core::option::Option::Some(#text) });
} else {
documentation_arms
.push(quote! {
&#name::#ident #params => ::core::option::Option::Some(concat!(#(concat!(#documentation, "\n")),*))
});
}
}
}
if arms.len() < variants.len() {
arms.push(quote! { _ => ::core::option::Option::None });
}
if detailed_arms.len() < variants.len() {
detailed_arms.push(quote! { _ => ::core::option::Option::None });
}
if documentation_arms.len() < variants.len() {
documentation_arms.push(quote! { _ => ::core::option::Option::None });
}
Ok(quote! {
impl #impl_generics #strum_module_path::EnumMessage for #name #ty_generics #where_clause {
fn get_message(&self) -> ::core::option::Option<&'static str> {
match self {
#(#arms),*
}
}
fn get_detailed_message(&self) -> ::core::option::Option<&'static str> {
match self {
#(#detailed_arms),*
}
}
fn get_documentation(&self) -> ::core::option::Option<&'static str> {
match self {
#(#documentation_arms),*
}
}
fn get_serializations(&self) -> &'static [&'static str] {
match self {
#(#serializations),*
}
}
}
})
}