11use proc_macro2:: TokenStream ;
2- use quote:: { ToTokens , TokenStreamExt , format_ident, quote} ;
3- use syn:: { DataEnum , DeriveInput , Generics , Ident , Result , Variant , Visibility } ;
2+ use quote:: { ToTokens , format_ident, quote} ;
3+ use syn:: { DataEnum , DeriveInput , Ident , Result , Variant , Visibility } ;
44
55use crate :: {
66 validate:: {
@@ -15,7 +15,6 @@ pub struct ValidateEnum<'a> {
1515 visibility : & ' a Visibility ,
1616 ident : & ' a Ident ,
1717 error_ident : Ident ,
18- generics : & ' a Generics ,
1918 variants : Vec < ValidateEnumVariant < ' a > > ,
2019}
2120
@@ -25,7 +24,6 @@ impl<'a> ValidateEnum<'a> {
2524 visibility : & input. vis ,
2625 ident : & input. ident ,
2726 error_ident : format_ident ! ( "{}ValidationError" , input. ident) ,
28- generics : & input. generics ,
2927 variants : Vec :: with_capacity ( data. variants . len ( ) ) ,
3028 } ;
3129
@@ -41,7 +39,7 @@ impl<'a> ValidateEnum<'a> {
4139 Ok ( result)
4240 }
4341
44- fn error_type ( & self ) -> ( & Ident , TokenStream ) {
42+ pub fn error_type ( & self ) -> ( TokenStream , TokenStream ) {
4543 let visibility = & self . visibility ;
4644 let error_ident = & self . error_ident ;
4745
@@ -51,14 +49,14 @@ impl<'a> ValidateEnum<'a> {
5149 . iter ( )
5250 . map ( |variant| & variant. ident )
5351 . collect :: < Vec < _ > > ( ) ;
54- let error_variant_types = self
52+ let ( error_variant_types, variant_error_types ) : ( Vec < _ > , Vec < _ > ) = self
5553 . variants
5654 . iter ( )
57- . map ( |variant| variant. error_type ( ) . 0 )
58- . collect :: < Vec < _ > > ( ) ;
55+ . map ( |variant| variant. error_type ( ) )
56+ . unzip ( ) ;
5957
6058 (
61- error_ident,
59+ error_ident. to_token_stream ( ) ,
6260 quote ! {
6361 #[ allow( dead_code) ]
6462 #[ derive( Debug , PartialEq ) ]
@@ -76,51 +74,23 @@ impl<'a> ValidateEnum<'a> {
7674
7775 #[ automatically_derived]
7876 impl :: std:: error:: Error for #error_ident { }
77+
78+ #( #variant_error_types ) *
7979 } ,
8080 )
8181 }
82- }
83-
84- impl < ' a > ToTokens for ValidateEnum < ' a > {
85- fn to_tokens ( & self , tokens : & mut TokenStream ) {
86- let ident = & self . ident ;
87- let ( impl_generics, type_generics, where_clause) = & self . generics . split_for_impl ( ) ;
8882
89- let ( error_ident, error_type) = self . error_type ( ) ;
90- let variant_error_types = self . variants . iter ( ) . map ( |variant| variant. error_type ( ) . 1 ) ;
91- let sync_variant_match_arms = self
92- . variants
93- . iter ( )
94- . map ( |variant| variant. match_arm ( Execution :: Sync ) ) ;
95- let async_variant_match_arms = self
83+ pub fn validations ( & self , execution : Execution ) -> TokenStream {
84+ let variant_match_arms = self
9685 . variants
9786 . iter ( )
98- . map ( |variant| variant. match_arm ( Execution :: Async ) ) ;
99-
100- tokens. append_all ( quote ! {
101- #error_type
87+ . map ( |variant| variant. match_arm ( execution) ) ;
10288
103- #( #variant_error_types ) *
104-
105- #[ automatically_derived]
106- impl #impl_generics :: fortifier:: Validate for #ident #type_generics #where_clause {
107- type Error = #error_ident;
108-
109- fn validate_sync( & self ) -> Result <( ) , :: fortifier:: ValidationErrors <Self :: Error >> {
110- match & self {
111- #( #sync_variant_match_arms ) , *
112- }
113- }
114-
115- fn validate_async( & self ) -> :: std:: pin:: Pin <Box <impl Future <Output = Result <( ) , :: fortifier:: ValidationErrors <Self :: Error >>>>> {
116- Box :: pin( async move {
117- match & self {
118- #( #async_variant_match_arms ) , *
119- }
120- } )
121- }
89+ quote ! {
90+ match & self {
91+ #( #variant_match_arms ) , *
12292 }
123- } )
93+ }
12494 }
12595}
12696
0 commit comments