@@ -11,7 +11,7 @@ mod utils;
1111use canyon_entity_macro:: parse_canyon_entity_proc_macro_attr;
1212use proc_macro:: TokenStream as CompilerTokenStream ;
1313use proc_macro2:: { Ident , TokenStream } ;
14- use quote:: { quote, ToTokens } ;
14+ use quote:: quote;
1515use syn:: { DeriveInput , Fields , Type , Visibility } ;
1616
1717use query_operations:: {
@@ -442,6 +442,11 @@ pub fn implement_row_mapper_for_type(input: proc_macro::TokenStream) -> proc_mac
442442 }
443443 } ) ;
444444
445+
446+ // TODO: refactor the code below after the current bugfixes, to conditinally generate
447+ // the required methods and populate the CanyonMapper trait dependencing on the cfg flags
448+ // enabled with a more elegant solution (a fn for feature, for ex)
449+ #[ cfg( feature = "postgres" ) ]
445450 // Here it's where the incoming values of the DatabaseResult are wired into a new
446451 // instance, mapping the fields of the type against the columns
447452 let init_field_values = fields. iter ( ) . map ( |( _vis, ident, _ty) | {
@@ -452,6 +457,8 @@ pub fn implement_row_mapper_for_type(input: proc_macro::TokenStream) -> proc_mac
452457 }
453458 } ) ;
454459
460+
461+ #[ cfg( feature = "mssql" ) ]
455462 let init_field_values_sqlserver = fields. iter ( ) . map ( |( _vis, ident, ty) | {
456463 let ident_name = ident. to_string ( ) ;
457464
@@ -530,6 +537,7 @@ pub fn implement_row_mapper_for_type(input: proc_macro::TokenStream) -> proc_mac
530537 }
531538 } ) ;
532539
540+ #[ cfg( feature = "mysql" ) ]
533541 let init_field_values_mysql = fields. iter ( ) . map ( |( _vis, ident, _ty) | {
534542 let ident_name = ident. to_string ( ) ;
535543 quote ! {
@@ -541,27 +549,40 @@ pub fn implement_row_mapper_for_type(input: proc_macro::TokenStream) -> proc_mac
541549 // The type of the Struct
542550 let ty = ast. ident ;
543551
544- let tokens = quote ! {
545- impl canyon_sql :: crud :: RowMapper < Self > for #ty {
546- #[ cfg( feature= "postgres" ) ]
547- fn deserialize_postgresql ( row : & canyon_sql :: db_clients :: tokio_postgres :: Row ) -> #ty {
548- Self {
549- # ( #init_field_values ) , *
550- }
552+ let mut impl_methods = quote ! { } ; // Collect methods conditionally
553+
554+ #[ cfg( feature = "postgres" ) ]
555+ impl_methods . extend ( quote ! {
556+ fn deserialize_postgresql ( row : & canyon_sql :: db_clients :: tokio_postgres :: Row ) -> #ty {
557+ Self {
558+ # ( #init_field_values ) , *
551559 }
552- #[ cfg( feature="mssql" ) ]
553- fn deserialize_sqlserver( row: & canyon_sql:: db_clients:: tiberius:: Row ) -> #ty {
554- Self {
555- #( #init_field_values_sqlserver) , *
556- }
560+ }
561+ } ) ;
562+
563+ #[ cfg( feature = "mssql" ) ]
564+ impl_methods. extend ( quote ! {
565+ fn deserialize_sqlserver( row: & canyon_sql:: db_clients:: tiberius:: Row ) -> #ty {
566+ Self {
567+ #( #init_field_values_sqlserver) , *
557568 }
558- #[ cfg( feature="mysql" ) ]
559- fn deserialize_mysql( row: & canyon_sql:: db_clients:: mysql_async:: Row ) -> #ty {
560- Self {
561- #( #init_field_values_mysql) , *
562- }
569+ }
570+ } ) ;
571+
572+ #[ cfg( feature = "mysql" ) ]
573+ impl_methods. extend ( quote ! {
574+ fn deserialize_mysql( row: & canyon_sql:: db_clients:: mysql_async:: Row ) -> #ty {
575+ Self {
576+ #( #init_field_values_mysql) , *
563577 }
564578 }
579+ } ) ;
580+
581+ // Wrap everything in the shared `impl` block
582+ let tokens = quote ! {
583+ impl canyon_sql:: crud:: RowMapper <Self > for #ty {
584+ #impl_methods
585+ }
565586 } ;
566587
567588 tokens. into ( )
@@ -588,6 +609,9 @@ fn fields_with_types(fields: &Fields) -> Vec<(Visibility, Ident, Type)> {
588609 . collect :: < Vec < _ > > ( )
589610}
590611
612+ #[ cfg( feature = "mssql" ) ]
613+ use quote:: ToTokens ;
614+ #[ cfg( feature = "mssql" ) ]
591615fn get_field_type_as_string ( typ : & Type ) -> String {
592616 match typ {
593617 Type :: Array ( type_) => type_. to_token_stream ( ) . to_string ( ) ,
0 commit comments