1use std::default::Default;
2use syn::{Ident, LitStr, Variant};
3
4use super::case_style::{CaseStyle, CaseStyleHelpers};
5use super::metadata::{kw, VariantExt, VariantMeta};
6use super::occurrence_error;
7
8pub trait HasStrumVariantProperties {
9 fn get_variant_properties(&self) -> syn::Result<StrumVariantProperties>;
10}
11
12#[derive(Clone, Eq, PartialEq, Debug, Default)]
13pub struct StrumVariantProperties {
14 pub disabled: Option<kw::disabled>,
15 pub default: Option<kw::default>,
16 pub default_with: Option<LitStr>,
17 pub ascii_case_insensitive: Option<bool>,
18 pub message: Option<LitStr>,
19 pub detailed_message: Option<LitStr>,
20 pub documentation: Vec<LitStr>,
21 pub string_props: Vec<(LitStr, LitStr)>,
22 serialize: Vec<LitStr>,
23 pub to_string: Option<LitStr>,
24 ident: Option<Ident>,
25}
26
27impl StrumVariantProperties {
28 fn ident_as_str(&self, case_style: Option<CaseStyle>) -> LitStr {
29 let ident = self.ident.as_ref().expect("identifier");
30 LitStr::new(&ident.convert_case(case_style), ident.span())
31 }
32
33 pub fn get_preferred_name(
34 &self,
35 case_style: Option<CaseStyle>,
36 prefix: Option<&LitStr>,
37 ) -> LitStr {
38 let mut output = self.to_string.as_ref().cloned().unwrap_or_else(|| {
39 self.serialize
40 .iter()
41 .max_by_key(|s| s.value().len())
42 .cloned()
43 .unwrap_or_else(|| self.ident_as_str(case_style))
44 });
45
46 if let Some(prefix) = prefix {
47 output = LitStr::new(&(prefix.value() + &output.value()), output.span());
48 }
49
50 output
51 }
52
53 pub fn get_serializations(&self, case_style: Option<CaseStyle>) -> Vec<LitStr> {
54 let mut attrs = self.serialize.clone();
55 if let Some(to_string) = &self.to_string {
56 attrs.push(to_string.clone());
57 }
58
59 if attrs.is_empty() {
60 attrs.push(self.ident_as_str(case_style));
61 }
62
63 attrs
64 }
65}
66
67impl HasStrumVariantProperties for Variant {
68 fn get_variant_properties(&self) -> syn::Result<StrumVariantProperties> {
69 let mut output = StrumVariantProperties {
70 ident: Some(self.ident.clone()),
71 ..Default::default()
72 };
73
74 let mut message_kw = None;
75 let mut detailed_message_kw = None;
76 let mut disabled_kw = None;
77 let mut default_kw = None;
78 let mut default_with_kw = None;
79 let mut to_string_kw = None;
80 let mut ascii_case_insensitive_kw = None;
81 for meta in self.get_metadata()? {
82 match meta {
83 VariantMeta::Message { value, kw } => {
84 if let Some(fst_kw) = message_kw {
85 return Err(occurrence_error(fst_kw, kw, "message"));
86 }
87
88 message_kw = Some(kw);
89 output.message = Some(value);
90 }
91 VariantMeta::DetailedMessage { value, kw } => {
92 if let Some(fst_kw) = detailed_message_kw {
93 return Err(occurrence_error(fst_kw, kw, "detailed_message"));
94 }
95
96 detailed_message_kw = Some(kw);
97 output.detailed_message = Some(value);
98 }
99 VariantMeta::Documentation { value } => {
100 output.documentation.push(value);
101 }
102 VariantMeta::Serialize { value, .. } => {
103 output.serialize.push(value);
104 }
105 VariantMeta::ToString { value, kw } => {
106 if let Some(fst_kw) = to_string_kw {
107 return Err(occurrence_error(fst_kw, kw, "to_string"));
108 }
109
110 to_string_kw = Some(kw);
111 output.to_string = Some(value);
112 }
113 VariantMeta::Disabled(kw) => {
114 if let Some(fst_kw) = disabled_kw {
115 return Err(occurrence_error(fst_kw, kw, "disabled"));
116 }
117
118 disabled_kw = Some(kw);
119 output.disabled = Some(kw);
120 }
121 VariantMeta::Default(kw) => {
122 if let Some(fst_kw) = default_kw {
123 return Err(occurrence_error(fst_kw, kw, "default"));
124 }
125
126 default_kw = Some(kw);
127 output.default = Some(kw);
128 }
129 VariantMeta::DefaultWith { kw, value } => {
130 if let Some(fst_kw) = default_with_kw {
131 return Err(occurrence_error(fst_kw, kw, "default_with"));
132 }
133
134 default_with_kw = Some(kw);
135 output.default_with = Some(value);
136 }
137 VariantMeta::AsciiCaseInsensitive { kw, value } => {
138 if let Some(fst_kw) = ascii_case_insensitive_kw {
139 return Err(occurrence_error(fst_kw, kw, "ascii_case_insensitive"));
140 }
141
142 ascii_case_insensitive_kw = Some(kw);
143 output.ascii_case_insensitive = Some(value);
144 }
145 VariantMeta::Props { props, .. } => {
146 output.string_props.extend(props);
147 }
148 }
149 }
150
151 Ok(output)
152 }
153}
154