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