MCPcopy Create free account
hub / github.com/SeaQL/sea-orm / impl_active_enum

Method impl_active_enum

sea-orm-macros/src/derives/active_enum.rs:202–432  ·  view source on GitHub ↗
(&self)

Source from the content-addressed store, hash-verified

200 }
201
202 fn impl_active_enum(&self) -> TokenStream {
203 let Self {
204 ident,
205 enum_name,
206 rs_type,
207 db_type,
208 is_string,
209 variants,
210 rename_all,
211 } = self;
212
213 let variant_idents: Vec<syn::Ident> = variants
214 .iter()
215 .map(|variant| variant.ident.clone())
216 .collect();
217
218 let variant_values: Vec<TokenStream> = variants
219 .iter()
220 .map(|variant| {
221 let variant_span = variant.ident.span();
222
223 if let Some(string_value) = &variant.string_value {
224 let string = string_value.value();
225 quote! { #string }
226 } else if let Some(num_value) = &variant.num_value {
227 quote! { #num_value }
228 } else if let Some(rename_rule) = variant.rename.or(*rename_all) {
229 let variant_ident = variant.ident.convert_case(Some(rename_rule));
230 quote! { #variant_ident }
231 } else {
232 quote_spanned! {
233 variant_span => compile_error!("Missing macro attribute, either `string_value`, `num_value` or `rename_all` should be specified");
234 }
235 }
236 })
237 .collect();
238
239 let val = if *is_string {
240 quote! { v.as_ref() }
241 } else {
242 quote! { v }
243 };
244
245 let enum_name_iden = format_ident!("{}Enum", ident);
246
247 let str_variants: Vec<String> = variants
248 .iter()
249 .filter_map(|variant| {
250 variant
251 .string_value
252 .as_ref()
253 .map(|string_value| string_value.value())
254 .or(variant
255 .rename
256 .map(|rename| variant.ident.convert_case(Some(rename))))
257 })
258 .collect();
259

Callers 1

expandMethod · 0.45

Calls 3

as_refMethod · 0.80
convert_caseMethod · 0.45

Tested by

no test coverage detected