1 | use crate::helpers::{case_style::snakify, non_enum_error, HasStrumVariantProperties}; |
2 | use proc_macro2::TokenStream; |
3 | use quote::{format_ident, quote}; |
4 | use syn::{Data, DeriveInput}; |
5 | |
6 | pub fn enum_is_inner(ast: &DeriveInput) -> syn::Result<TokenStream> { |
7 | let variants = match &ast.data { |
8 | Data::Enum(v) => &v.variants, |
9 | _ => return Err(non_enum_error()), |
10 | }; |
11 | let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); |
12 | |
13 | let enum_name = &ast.ident; |
14 | let variants: Vec<_> = variants |
15 | .iter() |
16 | .filter_map(|variant| { |
17 | if variant.get_variant_properties().ok()?.disabled.is_some() { |
18 | return None; |
19 | } |
20 | |
21 | let variant_name = &variant.ident; |
22 | let fn_name = format_ident!("is_ {}" , snakify(&variant_name.to_string())); |
23 | let doc_comment = format!( |
24 | "Returns [true] if the enum is [ {}:: {}] otherwise [false]" , |
25 | enum_name, variant_name |
26 | ); |
27 | Some(quote! { |
28 | #[must_use] |
29 | #[inline] |
30 | #[doc = #doc_comment] |
31 | pub const fn #fn_name(&self) -> bool { |
32 | match self { |
33 | &#enum_name::#variant_name { .. } => true, |
34 | _ => false |
35 | } |
36 | } |
37 | }) |
38 | }) |
39 | .collect(); |
40 | |
41 | Ok(quote! { |
42 | impl #impl_generics #enum_name #ty_generics #where_clause { |
43 | #(#variants)* |
44 | } |
45 | } |
46 | .into()) |
47 | } |
48 | |