|
1 | 1 | use crate::models::{DisplayCase, FieldsType, Meta, Variant}; |
2 | 2 | use proc_macro2::{Ident, TokenStream}; |
3 | 3 | use quote::quote; |
4 | | - |
5 | | -pub fn generate(meta: Meta) -> TokenStream { |
6 | | - let enum_kind = gen_enum_kind(&meta); |
7 | | - let impl_display_for_enum_kind = gen_impl_display_for_enum_kind(&meta); |
8 | | - let impl_from_str_for_enum_kind = gen_impl_from_str_for_enum_kind(&meta); |
9 | | - |
10 | | - let fn_kind = gen_fn_kind(&meta); |
11 | | - let type_name = &meta.ident; |
12 | | - let kind_name = meta.kind_name(); |
13 | | - let generics = &meta.generics; |
14 | | - |
15 | | - let type_with_generics = quote!(#type_name #generics); |
16 | | - |
17 | | - quote!( |
18 | | - #enum_kind // enum DrinkKind { Mate, Coffee, Tea } |
19 | | - |
20 | | - #impl_display_for_enum_kind // impl std::fmt::Display for DrinkKind { ... } |
21 | | - |
22 | | - #impl_from_str_for_enum_kind // impl std::str::FromStr for DrinkKind { ... } |
23 | | - |
24 | | - impl #generics #type_with_generics { // impl<T> Drink<T> { |
25 | | - #fn_kind // fn kind(&self) -> DrinkKind { ... } |
26 | | - } // } |
27 | | - |
28 | | - impl #generics ::kinded::Kinded for #type_with_generics { // impl<T> ::kinded::Kinded for Drink<T> { |
29 | | - type Kind = #kind_name; // type Kind = DrinkKind; |
30 | | - // |
31 | | - fn kind(&self) -> #kind_name { // fn kind(&self) -> DrinkKind { |
32 | | - self.kind() // self.kind() |
33 | | - } // } |
34 | | - } // } |
35 | | - |
36 | | - impl #generics From<#type_with_generics> for #kind_name { // impl<'a, T> From<Drink<'a, T>> for DrinkKind { |
37 | | - fn from(value: #type_with_generics) -> #kind_name { // fn from(value: Drink<'a, T>) -> DrinkKind { |
38 | | - value.kind() // value.kind() |
39 | | - } // } |
40 | | - } // } |
41 | | - |
42 | | - impl #generics From<&#type_with_generics> for #kind_name { // impl<'a, T> From<Drink<'a, T>> for DrinkKind { |
43 | | - fn from(value: &#type_with_generics) -> #kind_name { // fn from(value: &Drink<'a, T>) -> DrinkKind { |
44 | | - value.kind() // value.kind() |
45 | | - } // } |
46 | | - } // } |
47 | | - ) |
48 | | -} |
49 | | - |
50 | | -fn gen_enum_kind(meta: &Meta) -> TokenStream { |
51 | | - let vis = &meta.vis; |
52 | | - let kind_name = meta.kind_name(); |
53 | | - let variant_names: Vec<&Ident> = meta.variants.iter().map(|v| &v.ident).collect(); |
54 | | - let traits = meta.derive_traits(); |
55 | | - |
56 | | - quote!( |
57 | | - #[derive(#(#traits),*)] // #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
58 | | - #vis enum #kind_name { // pub enum DrinkKind { |
59 | | - #(#variant_names),* // Mate, Coffee, Tea |
60 | | - } // } |
61 | | - |
62 | | - impl #kind_name { // impl DrinkKind { |
63 | | - pub fn all() -> impl Iterator<Item = #kind_name> { // pub fn all() -> impl Iterator<Item = DrinkKind> { |
64 | | - [ // [ |
65 | | - #(#kind_name::#variant_names),* // DrinkKind::Mate, DrinkKind::Coffee, DrinkKind::Tea |
66 | | - ].into_iter() // ] |
67 | | - } // } |
68 | | - } // } |
69 | | - ) |
70 | | -} |
71 | | - |
72 | | -fn gen_impl_display_for_enum_kind(meta: &Meta) -> TokenStream { |
73 | | - let kind_name = meta.kind_name(); |
74 | | - let maybe_case = meta.kinded_attrs.display; |
75 | | - |
76 | | - let match_branches = meta.variants.iter().map(|variant| { |
77 | | - let original_variant_name_str = variant.ident.to_string(); |
78 | | - let cased_variant_name = apply_display_case(original_variant_name_str, maybe_case); |
79 | | - let variant_name = &variant.ident; |
80 | | - quote!( |
81 | | - #kind_name::#variant_name => write!(f, #cased_variant_name) |
82 | | - ) |
83 | | - }); |
84 | | - |
85 | | - quote!( |
86 | | - impl std::fmt::Display for #kind_name { |
87 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
88 | | - match self { |
89 | | - #(#match_branches),* |
90 | | - } |
91 | | - } |
92 | | - } |
93 | | - ) |
94 | | -} |
95 | | - |
96 | | -fn gen_impl_from_str_for_enum_kind(meta: &Meta) -> TokenStream { |
97 | | - let kind_name = meta.kind_name(); |
98 | | - // let maybe_case = meta.kinded_attrs.display; |
99 | | - |
100 | | - // let match_branches = meta.variants.iter().map(|variant| { |
101 | | - // let original_variant_name_str = variant.ident.to_string(); |
102 | | - // let cased_variant_name = apply_display_case(original_variant_name_str, maybe_case); |
103 | | - // let variant_name = &variant.ident; |
104 | | - // quote!( |
105 | | - // #kind_name::#variant_name => write!(f, #cased_variant_name) |
106 | | - // ) |
107 | | - // }); |
108 | | - |
109 | | - quote!( |
110 | | - impl ::std::str::FromStr for #kind_name { |
111 | | - type Err = &'static str; |
112 | | - |
113 | | - fn from_str(s: &str) -> Result<Self, Self::Err> { |
114 | | - todo!() |
115 | | - } |
116 | | - } |
117 | | - ) |
118 | | -} |
119 | | - |
120 | | -fn apply_display_case(original: String, maybe_display_case: Option<DisplayCase>) -> String { |
121 | | - use convert_case::{Case, Casing}; |
122 | | - |
123 | | - if let Some(display_case) = maybe_display_case { |
124 | | - let case: Case = display_case.into(); |
125 | | - original.to_case(case) |
126 | | - } else { |
127 | | - original |
128 | | - } |
129 | | -} |
130 | | - |
131 | | -fn gen_fn_kind(meta: &Meta) -> TokenStream { |
132 | | - let name = &meta.ident; |
133 | | - let kind_name = meta.kind_name(); |
134 | | - let match_branches = meta |
135 | | - .variants |
136 | | - .iter() |
137 | | - .map(|variant| gen_match_branch(name, &kind_name, variant)); |
138 | | - |
139 | | - quote!( |
140 | | - pub fn kind(&self) -> #kind_name { // pub fn kind(&self) -> DrinkKind { |
141 | | - match self { // match self { |
142 | | - #(#match_branches),* // Drink::Coffee(..) => DrinkKind::Coffee, |
143 | | - } // } |
144 | | - } // } |
145 | | - ) |
146 | | -} |
147 | | - |
148 | | -fn gen_match_branch(name: &Ident, kind_name: &Ident, variant: &Variant) -> TokenStream { |
149 | | - let variant_name = &variant.ident; |
150 | | - let variant_destruct = match variant.fields_type { |
151 | | - FieldsType::Named => quote!({ .. }), |
152 | | - FieldsType::Unnamed => quote!((..)), |
153 | | - FieldsType::Unit => quote!(), |
154 | | - }; |
155 | | - |
156 | | - quote!( |
157 | | - #name::#variant_name #variant_destruct => #kind_name::#variant_name |
158 | | - ) |
159 | | -} |
0 commit comments