Skip to content

Commit 8d77a6a

Browse files
authored
Merge pull request #6 from remkop22/only-tokio
remove switch between postgres and tokio-postgres
2 parents 12987bf + 8427306 commit 8d77a6a

File tree

3 files changed

+26
-64
lines changed

3 files changed

+26
-64
lines changed

Cargo.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ categories = ["database", "parsing", "data-structures"]
3131
[workspace.dependencies]
3232
postgres-from-row-derive = { path = "postgres-from-row-derive", version = "=0.4.0" }
3333

34-
[features]
35-
default = ["postgres"]
36-
postgres = ["dep:postgres"]
37-
tokio-postgres = ["dep:tokio-postgres"]
38-
3934
[dependencies]
40-
tokio-postgres = { version = "0.7.7", default_features = false, optional = true }
41-
postgres = { version = "0.19.4", default_features = false, optional = true }
35+
tokio-postgres = { version = "0.7.7", default_features = false }
4236
postgres-from-row-derive.workspace = true
4337

postgres-from-row-derive/src/lib.rs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,22 @@ use darling::{ast::Data, Error, FromDeriveInput, FromField, ToTokens};
22
use proc_macro::TokenStream;
33
use proc_macro2::TokenStream as TokenStream2;
44
use quote::quote;
5-
use syn::{parse_macro_input, DeriveInput, Ident, Result};
6-
7-
#[proc_macro_derive(FromRowTokioPostgres, attributes(from_row))]
8-
pub fn derive_from_row_tokio_postgres(input: TokenStream) -> TokenStream {
9-
derive_from_row(input, quote::format_ident!("tokio_postgres"))
10-
}
11-
12-
#[proc_macro_derive(FromRowPostgres, attributes(from_row))]
13-
pub fn derive_from_row_postgres(input: TokenStream) -> TokenStream {
14-
derive_from_row(input, quote::format_ident!("postgres"))
15-
}
5+
use syn::{parse_macro_input, DeriveInput, Result};
166

177
/// Calls the fallible entry point and writes any errors to the tokenstream.
18-
fn derive_from_row(input: TokenStream, module: Ident) -> TokenStream {
8+
#[proc_macro_derive(FromRow, attributes(from_row))]
9+
pub fn derive_from_row(input: TokenStream) -> TokenStream {
1910
let derive_input = parse_macro_input!(input as DeriveInput);
20-
match try_derive_from_row(&derive_input, module) {
11+
match try_derive_from_row(&derive_input) {
2112
Ok(result) => result,
2213
Err(err) => err.write_errors().into(),
2314
}
2415
}
2516

2617
/// Fallible entry point for generating a `FromRow` implementation
27-
fn try_derive_from_row(
28-
input: &DeriveInput,
29-
module: Ident,
30-
) -> std::result::Result<TokenStream, Error> {
18+
fn try_derive_from_row(input: &DeriveInput) -> std::result::Result<TokenStream, Error> {
3119
let from_row_derive = DeriveFromRow::from_derive_input(input)?;
32-
Ok(from_row_derive.generate(module)?)
20+
Ok(from_row_derive.generate()?)
3321
}
3422

3523
/// Main struct for deriving `FromRow` for a struct.
@@ -56,11 +44,11 @@ impl DeriveFromRow {
5644
}
5745

5846
/// Generates any additional where clause predicates needed for the fields in this struct.
59-
fn predicates(&self, module: &Ident) -> Result<Vec<TokenStream2>> {
47+
fn predicates(&self) -> Result<Vec<TokenStream2>> {
6048
let mut predicates = Vec::new();
6149

6250
for field in self.fields() {
63-
field.add_predicates(&module, &mut predicates)?;
51+
field.add_predicates(&mut predicates)?;
6452
}
6553

6654
Ok(predicates)
@@ -75,37 +63,37 @@ impl DeriveFromRow {
7563
}
7664

7765
/// Generate the `FromRow` implementation.
78-
fn generate(self, module: Ident) -> Result<TokenStream> {
66+
fn generate(self) -> Result<TokenStream> {
7967
self.validate()?;
8068

8169
let ident = &self.ident;
8270

8371
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
8472
let original_predicates = where_clause.clone().map(|w| &w.predicates).into_iter();
85-
let predicates = self.predicates(&module)?;
73+
let predicates = self.predicates()?;
8674

8775
let from_row_fields = self
8876
.fields()
8977
.iter()
90-
.map(|f| f.generate_from_row(&module))
78+
.map(|f| f.generate_from_row())
9179
.collect::<syn::Result<Vec<_>>>()?;
9280

9381
let try_from_row_fields = self
9482
.fields()
9583
.iter()
96-
.map(|f| f.generate_try_from_row(&module))
84+
.map(|f| f.generate_try_from_row())
9785
.collect::<syn::Result<Vec<_>>>()?;
9886

9987
Ok(quote! {
10088
impl #impl_generics postgres_from_row::FromRow for #ident #ty_generics where #(#original_predicates),* #(#predicates),* {
10189

102-
fn from_row(row: &#module::Row) -> Self {
90+
fn from_row(row: &postgres_from_row::tokio_postgres::Row) -> Self {
10391
Self {
10492
#(#from_row_fields),*
10593
}
10694
}
10795

108-
fn try_from_row(row: &#module::Row) -> std::result::Result<Self, #module::Error> {
96+
fn try_from_row(row: &postgres_from_row::tokio_postgres::Row) -> std::result::Result<Self, postgres_from_row::tokio_postgres::Error> {
10997
Ok(Self {
11098
#(#try_from_row_fields),*
11199
})
@@ -187,14 +175,14 @@ impl FromRowField {
187175
/// and when using either `from` or `try_from` attributes it additionally pushes this bound:
188176
/// `T: std::convert::From<R>`, where `T` is the type specified in the struct and `R` is the
189177
/// type specified in the `[try]_from` attribute.
190-
fn add_predicates(&self, module: &Ident, predicates: &mut Vec<TokenStream2>) -> Result<()> {
178+
fn add_predicates(&self, predicates: &mut Vec<TokenStream2>) -> Result<()> {
191179
let target_ty = &self.target_ty()?;
192180
let ty = &self.ty;
193181

194182
predicates.push(if self.flatten {
195183
quote! (#target_ty: postgres_from_row::FromRow)
196184
} else {
197-
quote! (#target_ty: for<'a> #module::types::FromSql<'a>)
185+
quote! (#target_ty: for<'a> postgres_from_row::tokio_postgres::types::FromSql<'a>)
198186
});
199187

200188
if self.from.is_some() {
@@ -203,15 +191,15 @@ impl FromRowField {
203191
let try_from = quote!(std::convert::TryFrom<#target_ty>);
204192

205193
predicates.push(quote!(#ty: #try_from));
206-
predicates.push(quote!(#module::Error: std::convert::From<<#ty as #try_from>::Error>));
194+
predicates.push(quote!(postgres_from_row::tokio_postgres::Error: std::convert::From<<#ty as #try_from>::Error>));
207195
predicates.push(quote!(<#ty as #try_from>::Error: std::fmt::Debug));
208196
}
209197

210198
Ok(())
211199
}
212200

213201
/// Generate the line needed to retrievee this field from a row when calling `from_row`.
214-
fn generate_from_row(&self, module: &Ident) -> Result<TokenStream2> {
202+
fn generate_from_row(&self) -> Result<TokenStream2> {
215203
let ident = self.ident.as_ref().unwrap();
216204
let column_name = self.column_name();
217205
let field_ty = &self.ty;
@@ -220,7 +208,7 @@ impl FromRowField {
220208
let mut base = if self.flatten {
221209
quote!(<#target_ty as postgres_from_row::FromRow>::from_row(row))
222210
} else {
223-
quote!(#module::Row::get::<&str, #target_ty>(row, #column_name))
211+
quote!(postgres_from_row::tokio_postgres::Row::get::<&str, #target_ty>(row, #column_name))
224212
};
225213

226214
if self.from.is_some() {
@@ -233,7 +221,7 @@ impl FromRowField {
233221
}
234222

235223
/// Generate the line needed to retrieve this field from a row when calling `try_from_row`.
236-
fn generate_try_from_row(&self, module: &Ident) -> Result<TokenStream2> {
224+
fn generate_try_from_row(&self) -> Result<TokenStream2> {
237225
let ident = self.ident.as_ref().unwrap();
238226
let column_name = self.column_name();
239227
let field_ty = &self.ty;
@@ -242,7 +230,7 @@ impl FromRowField {
242230
let mut base = if self.flatten {
243231
quote!(<#target_ty as postgres_from_row::FromRow>::try_from_row(row)?)
244232
} else {
245-
quote!(#module::Row::try_get::<&str, #target_ty>(row, #column_name)?)
233+
quote!(postgres_from_row::tokio_postgres::Row::try_get::<&str, #target_ty>(row, #column_name)?)
246234
};
247235

248236
if self.from.is_some() {

src/lib.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
#![deny(missing_docs)]
22
#![doc = include_str!("../README.md")]
33

4-
#[cfg(feature = "postgres")]
5-
mod active_postgres {
6-
pub use postgres::{Error, Row};
7-
pub use postgres_from_row_derive::FromRowPostgres as FromRow;
8-
}
9-
10-
#[cfg(feature = "tokio-postgres")]
11-
mod active_postgres {
12-
pub use postgres_from_row_derive::FromRowTokioPostgres as FromRow;
13-
pub use tokio_postgres::{Error, Row};
14-
}
4+
pub use postgres_from_row_derive::FromRow;
5+
pub use tokio_postgres;
156

167
/// A trait that allows mapping rows from either [postgres](<https://docs.rs/postgres>) or [tokio-postgres](<https://docs.rs/tokio-postgres>), to other types.
17-
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
188
pub trait FromRow: Sized {
199
/// Performce the conversion
2010
///
2111
/// # Panics
2212
///
2313
/// panics if the row does not contain the expected column names.
24-
fn from_row(row: &active_postgres::Row) -> Self;
14+
fn from_row(row: &tokio_postgres::Row) -> Self;
2515

2616
/// Try's to perform the conversion.
2717
///
2818
/// Will return an error if the row does not contain the expected column names.
29-
fn try_from_row(row: &active_postgres::Row) -> Result<Self, active_postgres::Error>;
19+
fn try_from_row(row: &tokio_postgres::Row) -> Result<Self, tokio_postgres::Error>;
3020
}
31-
32-
#[doc(hidden)]
33-
pub use active_postgres::FromRow;
34-
35-
//
36-
// #[cfg(all(feature = "postgres", feature = "tokio-postgres"))]
37-
// compile_error!("Can't combine feature `postgres` and `tokio-postgres`");
38-
//
39-
// #[cfg(not(any(feature = "postgres", feature = "tokio-postgres")))]
40-
// compile_error!("Must have at least one enabled feature: `postgres` or `tokio-postgres`.");

0 commit comments

Comments
 (0)