Skip to content

Commit

Permalink
pick up a closure
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhengzhuo committed Mar 3, 2021
1 parent 6610886 commit 61293ba
Showing 1 changed file with 12 additions and 46 deletions.
58 changes: 12 additions & 46 deletions sqlx-macros/src/derives/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::attributes::SqlxChildAttributes;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::{
parse_quote, punctuated::Punctuated, token::Comma, Data, DataStruct, DeriveInput, Field,
Fields, FieldsNamed, FieldsUnnamed, Lifetime,
parse_quote, punctuated::Punctuated, token::Comma, Data, DataStruct, DeriveInput, ExprPath,
Field, Fields, FieldsNamed, FieldsUnnamed, Lifetime, LitStr,
};

use super::{
Expand Down Expand Up @@ -146,9 +146,9 @@ fn expand_derive_from_row_struct(

let mut block = TokenStream::new();

if cfg!(feature = "mysql") {
let mut extend_block = |exprpath: ExprPath| {
block.extend(quote!(
impl #impl_generics ::sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause,R: ::sqlx::Row<Database = ::sqlx::MySql>, {
impl #impl_generics ::sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause,R: ::sqlx::Row<Database = #exprpath>, {
fn from_row(row: &#lifetime R) -> ::sqlx::Result<Self> {
#(#reads)*
::std::result::Result::Ok(#ident {
Expand All @@ -157,56 +157,22 @@ fn expand_derive_from_row_struct(
}
}
));
};

if cfg!(feature = "mysql") {
extend_block(LitStr::new("::sqlx::MySql", Span::call_site()).parse()?);
}
if cfg!(feature = "postgres") {
block.extend(quote!(
impl #impl_generics ::sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause,R: ::sqlx::Row<Database = ::sqlx::Postgres>, {
fn from_row(row: &#lifetime R) -> ::sqlx::Result<Self> {
#(#reads)*
::std::result::Result::Ok(#ident {
#(#names),*
})
}
}
));
extend_block(LitStr::new("::sqlx::Postgres", Span::call_site()).parse()?);
}
if cfg!(feature = "sqlite") {
block.extend(quote!(
impl #impl_generics ::sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause,R: ::sqlx::Row<Database = ::sqlx::Sqlite>, {
fn from_row(row: &#lifetime R) -> ::sqlx::Result<Self> {
#(#reads)*
::std::result::Result::Ok(#ident {
#(#names),*
})
}
}
));
extend_block(LitStr::new("::sqlx::Sqlite", Span::call_site()).parse()?);
}

if cfg!(feature = "mssql") {
block.extend(quote!(
impl #impl_generics ::sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause,R: ::sqlx::Row<Database = ::sqlx::Mssql>, {
fn from_row(row: &#lifetime R) -> ::sqlx::Result<Self> {
#(#reads)*
::std::result::Result::Ok(#ident {
#(#names),*
})
}
}
));
extend_block(LitStr::new("::sqlx::Mssql", Span::call_site()).parse()?);
}

if cfg!(feature = "any") {
block.extend(quote!(
impl #impl_generics ::sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause,R: ::sqlx::Row<Database = ::sqlx::Any>, {
fn from_row(row: &#lifetime R) -> ::sqlx::Result<Self> {
#(#reads)*
::std::result::Result::Ok(#ident {
#(#names),*
})
}
}
));
extend_block(LitStr::new("::sqlx::Any", Span::call_site()).parse()?);
}

Ok(block)
Expand Down

0 comments on commit 61293ba

Please sign in to comment.