Skip to content

Commit

Permalink
use absolute paths in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Freax13 authored and abonander committed Feb 3, 2021
1 parent 31abe22 commit 76326c6
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 130 deletions.
2 changes: 1 addition & 1 deletion sqlx-macros/src/derives/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl TypeName {
let val = &self.val;
if self.deprecated_rename {
quote_spanned!(self.span=> {
sqlx::_rename();
::sqlx::_rename();
#val
})
} else {
Expand Down
56 changes: 28 additions & 28 deletions sqlx-macros/src/derives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ fn expand_derive_decode_transparent(

// add db type for impl generics & where clause
let mut generics = generics.clone();
generics.params.insert(0, parse_quote!(DB: sqlx::Database));
generics.params.insert(0, parse_quote!(DB: ::sqlx::Database));
generics.params.insert(0, parse_quote!('r));
generics
.make_where_clause()
.predicates
.push(parse_quote!(#ty: sqlx::decode::Decode<'r, DB>));
.push(parse_quote!(#ty: ::sqlx::decode::Decode<'r, DB>));
let (impl_generics, _, where_clause) = generics.split_for_impl();

let tts = quote!(
impl #impl_generics sqlx::decode::Decode<'r, DB> for #ident #ty_generics #where_clause {
fn decode(value: <DB as sqlx::database::HasValueRef<'r>>::ValueRef) -> std::result::Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
<#ty as sqlx::decode::Decode<'r, DB>>::decode(value).map(Self)
impl #impl_generics ::sqlx::decode::Decode<'r, DB> for #ident #ty_generics #where_clause {
fn decode(value: <DB as ::sqlx::database::HasValueRef<'r>>::ValueRef) -> ::std::result::Result<Self, ::std::boxed::Box<dyn ::std::error::Error + 'static + ::std::marker::Send + ::std::marker::Sync>> {
<#ty as ::sqlx::decode::Decode<'r, DB>>::decode(value).map(Self)
}
}
);
Expand All @@ -94,19 +94,19 @@ fn expand_derive_decode_weak_enum(
.iter()
.map(|v| {
let id = &v.ident;
parse_quote!(_ if (#ident :: #id as #repr) == value => Ok(#ident :: #id),)
parse_quote!(_ if (#ident :: #id as #repr) == value => ::std::result::Result::Ok(#ident :: #id),)
})
.collect::<Vec<Arm>>();

Ok(quote!(
impl<'r, DB: sqlx::Database> sqlx::decode::Decode<'r, DB> for #ident where #repr: sqlx::decode::Decode<'r, DB> {
fn decode(value: <DB as sqlx::database::HasValueRef<'r>>::ValueRef) -> std::result::Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
let value = <#repr as sqlx::decode::Decode<'r, DB>>::decode(value)?;
impl<'r, DB: ::sqlx::Database> ::sqlx::decode::Decode<'r, DB> for #ident where #repr: ::sqlx::decode::Decode<'r, DB> {
fn decode(value: <DB as ::sqlx::database::HasValueRef<'r>>::ValueRef) -> ::std::result::Result<Self, ::std::boxed::Box<dyn ::std::error::Error + 'static + ::std::marker::Send + ::std::marker::Sync>> {
let value = <#repr as ::sqlx::decode::Decode<'r, DB>>::decode(value)?;

match value {
#(#arms)*

_ => Err(Box::new(sqlx::Error::Decode(format!("invalid value {:?} for enum {}", value, #ident_s).into())))
_ => ::std::result::Result::Err(::std::boxed::Box::new(::sqlx::Error::Decode(::std::format!("invalid value {:?} for enum {}", value, #ident_s).into())))
}
}
}
Expand All @@ -127,14 +127,14 @@ fn expand_derive_decode_strong_enum(
let attributes = parse_child_attributes(&v.attrs).unwrap();

if let Some(rename) = attributes.rename {
parse_quote!(#rename => Ok(#ident :: #id),)
parse_quote!(#rename => ::std::result::Result::Ok(#ident :: #id),)
} else if let Some(pattern) = cattr.rename_all {
let name = rename_all(&*id.to_string(), pattern);

parse_quote!(#name => Ok(#ident :: #id),)
parse_quote!(#name => ::std::result::Result::Ok(#ident :: #id),)
} else {
let name = id.to_string();
parse_quote!(#name => Ok(#ident :: #id),)
parse_quote!(#name => ::std::result::Result::Ok(#ident :: #id),)
}
});

Expand All @@ -150,9 +150,9 @@ fn expand_derive_decode_strong_enum(

if cfg!(feature = "mysql") {
tts.extend(quote!(
impl<'r> sqlx::decode::Decode<'r, sqlx::mysql::MySql> for #ident {
fn decode(value: sqlx::mysql::MySqlValueRef<'r>) -> std::result::Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
let value = <&'r str as sqlx::decode::Decode<'r, sqlx::mysql::MySql>>::decode(value)?;
impl<'r> ::sqlx::decode::Decode<'r, ::sqlx::mysql::MySql> for #ident {
fn decode(value: ::sqlx::mysql::MySqlValueRef<'r>) -> ::std::result::Result<Self, ::std::boxed::Box<dyn ::std::error::Error + 'static + ::std::marker::Send + ::std::marker::Sync>> {
let value = <&'r ::std::primitive::str as ::sqlx::decode::Decode<'r, ::sqlx::mysql::MySql>>::decode(value)?;

#values
}
Expand All @@ -162,9 +162,9 @@ fn expand_derive_decode_strong_enum(

if cfg!(feature = "postgres") {
tts.extend(quote!(
impl<'r> sqlx::decode::Decode<'r, sqlx::postgres::Postgres> for #ident {
fn decode(value: sqlx::postgres::PgValueRef<'r>) -> std::result::Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
let value = <&'r str as sqlx::decode::Decode<'r, sqlx::postgres::Postgres>>::decode(value)?;
impl<'r> ::sqlx::decode::Decode<'r, ::sqlx::postgres::Postgres> for #ident {
fn decode(value: ::sqlx::postgres::PgValueRef<'r>) -> ::std::result::Result<Self, ::std::boxed::Box<dyn ::std::error::Error + 'static + ::std::marker::Send + ::std::marker::Sync>> {
let value = <&'r ::std::primitive::str as ::sqlx::decode::Decode<'r, ::sqlx::postgres::Postgres>>::decode(value)?;

#values
}
Expand All @@ -174,9 +174,9 @@ fn expand_derive_decode_strong_enum(

if cfg!(feature = "sqlite") {
tts.extend(quote!(
impl<'r> sqlx::decode::Decode<'r, sqlx::sqlite::Sqlite> for #ident {
fn decode(value: sqlx::sqlite::SqliteValueRef<'r>) -> std::result::Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
let value = <&'r str as sqlx::decode::Decode<'r, sqlx::sqlite::Sqlite>>::decode(value)?;
impl<'r> ::sqlx::decode::Decode<'r, ::sqlx::sqlite::Sqlite> for #ident {
fn decode(value: ::sqlx::sqlite::SqliteValueRef<'r>) -> ::std::result::Result<Self, ::std::boxed::Box<dyn ::std::error::Error + 'static + ::std::marker::Send + ::std::marker::Sync>> {
let value = <&'r ::std::primitive::str as ::sqlx::decode::Decode<'r, ::sqlx::sqlite::Sqlite>>::decode(value)?;

#values
}
Expand Down Expand Up @@ -211,8 +211,8 @@ fn expand_derive_decode_struct(
for field in fields {
let ty = &field.ty;

predicates.push(parse_quote!(#ty: sqlx::decode::Decode<'r, sqlx::Postgres>));
predicates.push(parse_quote!(#ty: sqlx::types::Type<sqlx::Postgres>));
predicates.push(parse_quote!(#ty: ::sqlx::decode::Decode<'r, ::sqlx::Postgres>));
predicates.push(parse_quote!(#ty: ::sqlx::types::Type<::sqlx::Postgres>));
}

let (impl_generics, _, where_clause) = generics.split_for_impl();
Expand All @@ -229,13 +229,13 @@ fn expand_derive_decode_struct(
let names = fields.iter().map(|field| &field.ident);

tts.extend(quote!(
impl #impl_generics sqlx::decode::Decode<'r, sqlx::Postgres> for #ident #ty_generics #where_clause {
fn decode(value: sqlx::postgres::PgValueRef<'r>) -> std::result::Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
let mut decoder = sqlx::postgres::types::PgRecordDecoder::new(value)?;
impl #impl_generics ::sqlx::decode::Decode<'r, ::sqlx::Postgres> for #ident #ty_generics #where_clause {
fn decode(value: ::sqlx::postgres::PgValueRef<'r>) -> ::std::result::Result<Self, ::std::boxed::Box<dyn ::std::error::Error + 'static + ::std::marker::Send + ::std::marker::Sync>> {
let mut decoder = ::sqlx::postgres::types::PgRecordDecoder::new(value)?;

#(#reads)*

Ok(#ident {
::std::result::Result::Ok(#ident {
#(#names),*
})
}
Expand Down
48 changes: 24 additions & 24 deletions sqlx-macros/src/derives/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ fn expand_derive_encode_transparent(
.params
.insert(0, LifetimeDef::new(lifetime.clone()).into());

generics.params.insert(0, parse_quote!(DB: sqlx::Database));
generics.params.insert(0, parse_quote!(DB: ::sqlx::Database));
generics
.make_where_clause()
.predicates
.push(parse_quote!(#ty: sqlx::encode::Encode<#lifetime, DB>));
.push(parse_quote!(#ty: ::sqlx::encode::Encode<#lifetime, DB>));
let (impl_generics, _, where_clause) = generics.split_for_impl();

Ok(quote!(
impl #impl_generics sqlx::encode::Encode<#lifetime, DB> for #ident #ty_generics #where_clause {
fn encode_by_ref(&self, buf: &mut <DB as sqlx::database::HasArguments<#lifetime>>::ArgumentBuffer) -> sqlx::encode::IsNull {
<#ty as sqlx::encode::Encode<#lifetime, DB>>::encode_by_ref(&self.0, buf)
impl #impl_generics ::sqlx::encode::Encode<#lifetime, DB> for #ident #ty_generics #where_clause {
fn encode_by_ref(&self, buf: &mut <DB as ::sqlx::database::HasArguments<#lifetime>>::ArgumentBuffer) -> ::sqlx::encode::IsNull {
<#ty as ::sqlx::encode::Encode<#lifetime, DB>>::encode_by_ref(&self.0, buf)
}

fn produces(&self) -> Option<DB::TypeInfo> {
<#ty as sqlx::encode::Encode<#lifetime, DB>>::produces(&self.0)
<#ty as ::sqlx::encode::Encode<#lifetime, DB>>::produces(&self.0)
}

fn size_hint(&self) -> usize {
<#ty as sqlx::encode::Encode<#lifetime, DB>>::size_hint(&self.0)
<#ty as ::sqlx::encode::Encode<#lifetime, DB>>::size_hint(&self.0)
}
}
))
Expand All @@ -108,17 +108,17 @@ fn expand_derive_encode_weak_enum(
}

Ok(quote!(
impl<'q, DB: sqlx::Database> sqlx::encode::Encode<'q, DB> for #ident where #repr: sqlx::encode::Encode<'q, DB> {
fn encode_by_ref(&self, buf: &mut <DB as sqlx::database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
impl<'q, DB: ::sqlx::Database> ::sqlx::encode::Encode<'q, DB> for #ident where #repr: ::sqlx::encode::Encode<'q, DB> {
fn encode_by_ref(&self, buf: &mut <DB as ::sqlx::database::HasArguments<'q>>::ArgumentBuffer) -> ::sqlx::encode::IsNull {
let value = match self {
#(#values)*
};

<#repr as sqlx::encode::Encode<DB>>::encode_by_ref(&value, buf)
<#repr as ::sqlx::encode::Encode<DB>>::encode_by_ref(&value, buf)
}

fn size_hint(&self) -> usize {
<#repr as sqlx::encode::Encode<DB>>::size_hint(&Default::default())
<#repr as ::sqlx::encode::Encode<DB>>::size_hint(&Default::default())
}
}
))
Expand Down Expand Up @@ -151,21 +151,21 @@ fn expand_derive_encode_strong_enum(
}

Ok(quote!(
impl<'q, DB: sqlx::Database> sqlx::encode::Encode<'q, DB> for #ident where &'q str: sqlx::encode::Encode<'q, DB> {
fn encode_by_ref(&self, buf: &mut <DB as sqlx::database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
impl<'q, DB: ::sqlx::Database> ::sqlx::encode::Encode<'q, DB> for #ident where &'q ::std::primitive::str: ::sqlx::encode::Encode<'q, DB> {
fn encode_by_ref(&self, buf: &mut <DB as ::sqlx::database::HasArguments<'q>>::ArgumentBuffer) -> ::sqlx::encode::IsNull {
let val = match self {
#(#value_arms)*
};

<&str as sqlx::encode::Encode<'q, DB>>::encode(val, buf)
<&::std::primitive::str as ::sqlx::encode::Encode<'q, DB>>::encode(val, buf)
}

fn size_hint(&self) -> usize {
fn size_hint(&self) -> ::std::primitive::usize {
let val = match self {
#(#value_arms)*
};

<&str as sqlx::encode::Encode<'q, DB>>::size_hint(&val)
<&::std::primitive::str as ::sqlx::encode::Encode<'q, DB>>::size_hint(&val)
}
}
))
Expand Down Expand Up @@ -195,8 +195,8 @@ fn expand_derive_encode_struct(
for field in fields {
let ty = &field.ty;

predicates.push(parse_quote!(#ty: for<'q> sqlx::encode::Encode<'q, sqlx::Postgres>));
predicates.push(parse_quote!(#ty: sqlx::types::Type<sqlx::Postgres>));
predicates.push(parse_quote!(#ty: for<'q> ::sqlx::encode::Encode<'q, ::sqlx::Postgres>));
predicates.push(parse_quote!(#ty: ::sqlx::types::Type<::sqlx::Postgres>));
}

let (impl_generics, _, where_clause) = generics.split_for_impl();
Expand All @@ -214,23 +214,23 @@ fn expand_derive_encode_struct(
let ty = &field.ty;

parse_quote!(
<#ty as sqlx::encode::Encode<sqlx::Postgres>>::size_hint(&self. #id)
<#ty as ::sqlx::encode::Encode<::sqlx::Postgres>>::size_hint(&self. #id)
)
});

tts.extend(quote!(
impl #impl_generics sqlx::encode::Encode<'_, sqlx::Postgres> for #ident #ty_generics #where_clause {
fn encode_by_ref(&self, buf: &mut sqlx::postgres::PgArgumentBuffer) -> sqlx::encode::IsNull {
let mut encoder = sqlx::postgres::types::PgRecordEncoder::new(buf);
impl #impl_generics ::sqlx::encode::Encode<'_, ::sqlx::Postgres> for #ident #ty_generics #where_clause {
fn encode_by_ref(&self, buf: &mut ::sqlx::postgres::PgArgumentBuffer) -> ::sqlx::encode::IsNull {
let mut encoder = ::sqlx::postgres::types::PgRecordEncoder::new(buf);

#(#writes)*

encoder.finish();

sqlx::encode::IsNull::No
::sqlx::encode::IsNull::No
}

fn size_hint(&self) -> usize {
fn size_hint(&self) -> ::std::primitive::usize {
#column_count * (4 + 4) // oid (int) and length (int) for each column
+ #(#sizes)+* // sum of the size hints for each column
}
Expand Down
34 changes: 17 additions & 17 deletions sqlx-macros/src/derives/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ fn expand_derive_from_row_struct(
let (_, ty_generics, _) = generics.split_for_impl();

let mut generics = generics.clone();
generics.params.insert(0, parse_quote!(R: sqlx::Row));
generics.params.insert(0, parse_quote!(R: ::sqlx::Row));

if provided {
generics.params.insert(0, parse_quote!(#lifetime));
}

let predicates = &mut generics.make_where_clause().predicates;

predicates.push(parse_quote!(&#lifetime str: sqlx::ColumnIndex<R>));
predicates.push(parse_quote!(&#lifetime ::std::primitive::str: ::sqlx::ColumnIndex<R>));

for field in fields {
let ty = &field.ty;

predicates.push(parse_quote!(#ty: sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(#ty: sqlx::types::Type<R::Database>));
predicates.push(parse_quote!(#ty: ::sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(#ty: ::sqlx::types::Type<R::Database>));
}

let (impl_generics, _, where_clause) = generics.split_for_impl();
Expand All @@ -91,10 +91,10 @@ fn expand_derive_from_row_struct(
if attributes.default {
Some(
parse_quote!(let #id: #ty = row.try_get(#id_s).or_else(|e| match e {
sqlx::Error::ColumnNotFound(_) => {
Ok(Default::default())
::sqlx::Error::ColumnNotFound(_) => {
::std::result::Result::Ok(Default::default())
},
e => Err(e)
e => ::std::result::Result::Err(e)
})?;),
)
} else {
Expand All @@ -107,11 +107,11 @@ fn expand_derive_from_row_struct(
let names = fields.iter().map(|field| &field.ident);

Ok(quote!(
impl #impl_generics sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause {
fn from_row(row: &#lifetime R) -> sqlx::Result<Self> {
impl #impl_generics ::sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause {
fn from_row(row: &#lifetime R) -> ::sqlx::Result<Self> {
#(#reads)*

Ok(#ident {
::std::result::Result::Ok(#ident {
#(#names),*
})
}
Expand All @@ -136,21 +136,21 @@ fn expand_derive_from_row_struct_unnamed(
let (_, ty_generics, _) = generics.split_for_impl();

let mut generics = generics.clone();
generics.params.insert(0, parse_quote!(R: sqlx::Row));
generics.params.insert(0, parse_quote!(R: ::sqlx::Row));

if provided {
generics.params.insert(0, parse_quote!(#lifetime));
}

let predicates = &mut generics.make_where_clause().predicates;

predicates.push(parse_quote!(usize: sqlx::ColumnIndex<R>));
predicates.push(parse_quote!(::std::primitive::usize: ::sqlx::ColumnIndex<R>));

for field in fields {
let ty = &field.ty;

predicates.push(parse_quote!(#ty: sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(#ty: sqlx::types::Type<R::Database>));
predicates.push(parse_quote!(#ty: ::sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(#ty: ::sqlx::types::Type<R::Database>));
}

let (impl_generics, _, where_clause) = generics.split_for_impl();
Expand All @@ -161,9 +161,9 @@ fn expand_derive_from_row_struct_unnamed(
.map(|(idx, _)| quote!(row.try_get(#idx)?));

Ok(quote!(
impl #impl_generics sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause {
fn from_row(row: &#lifetime R) -> sqlx::Result<Self> {
Ok(#ident (
impl #impl_generics ::sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause {
fn from_row(row: &#lifetime R) -> ::sqlx::Result<Self> {
::std::result::Result::Ok(#ident (
#(#gets),*
))
}
Expand Down
Loading

0 comments on commit 76326c6

Please sign in to comment.