Skip to content

Version 0.2.0 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ license-file.workspace = true
keywords.workspace = true
categories.workspace = true

[lib]
doctest = false

[workspace]
members = ["postgres-from-row-derive"]

[workspace.package]
version = "0.1.0"
version = "0.2.0"
authors = ["Remo Pas <remo.pas22@gmail.com>"]
edition = "2021"
repository = "https://github.com/remkop22/postgres-from-row"
Expand All @@ -26,7 +29,7 @@ keywords = ["postgres", "postgres-tokio", "postgresql", "from-row", "mapper"]
categories = ["database", "parsing", "data-structures"]

[workspace.dependencies]
postgres-from-row-derive = { path = "postgres-from-row-derive", version = "=0.1.0" }
postgres-from-row-derive = { path = "postgres-from-row-derive", version = "=0.2.0" }

[features]
default = ["postgres"]
Expand Down
16 changes: 8 additions & 8 deletions postgres-from-row-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use darling::ast::{self, Style};
use darling::{FromDeriveInput, FromField};
use proc_macro::TokenStream;
Expand Down Expand Up @@ -56,8 +55,8 @@ impl DeriveFromRow {
Style::Struct => fields.fields,
};

let from_row_fields = fields.iter().map(FromRowField::generate_from_row);
let try_from_row_fields = fields.iter().map(FromRowField::generate_try_from_row);
let from_row_fields = fields.iter().map(|f| f.generate_from_row(&module));
let try_from_row_fields = fields.iter().map(|f| f.generate_try_from_row(&module));

Ok(quote! {
impl #impl_generics postgres_from_row::FromRow for #ident #ty_generics #where_clause {
Expand All @@ -68,7 +67,7 @@ impl DeriveFromRow {
}
}

fn try_from_row(row: &#module::Row) -> Result<Self, #module::Error> {
fn try_from_row(row: &#module::Row) -> std::result::Result<Self, #module::Error> {
Ok(Self {
#(#try_from_row_fields),*
})
Expand All @@ -89,7 +88,7 @@ struct FromRowField {
}

impl FromRowField {
fn generate_from_row(&self) -> proc_macro2::TokenStream {
fn generate_from_row(&self, module: &Ident) -> proc_macro2::TokenStream {
let ident = self.ident.as_ref().unwrap();
let str_ident = ident.to_string();
let ty = &self.ty;
Expand All @@ -100,11 +99,12 @@ impl FromRowField {
}
} else {
quote! {
#ident: row.get::<&str, #ty>(#str_ident)
#ident: #module::Row::get::<&str, #ty>(row, #str_ident)
}
}
}
fn generate_try_from_row(&self) -> proc_macro2::TokenStream {

fn generate_try_from_row(&self, module: &Ident) -> proc_macro2::TokenStream {
let ident = self.ident.as_ref().unwrap();
let str_ident = ident.to_string();
let ty = &self.ty;
Expand All @@ -115,7 +115,7 @@ impl FromRowField {
}
} else {
quote! {
#ident: row.try_get::<&str, #ty>(#str_ident)?
#ident: #module::Row::try_get::<&str, #ty>(row, #str_ident)?
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! #[derive(FromRow)]
//! struct Todo {
//! todo_id: i32,
//! text: String
//! text: String,
//! author_id: i32,
//! }
//!
Expand Down Expand Up @@ -91,8 +91,7 @@ pub trait FromRow: Sized {
fn try_from_row(row: &active_postgres::Row) -> Result<Self, active_postgres::Error>;
}

#[doc(no_inline)]
/// gfdsfd
#[doc(hidden)]
pub use active_postgres::FromRow;

//
Expand Down
10 changes: 9 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use postgres::Row;
use postgres_from_row::FromRow;

#[derive(FromRow)]
Expand All @@ -6,7 +7,7 @@ pub struct Todo {
todo_id: i32,
text: String,
#[from_row(flatten)]
user: User
user: User,
}

#[derive(FromRow)]
Expand All @@ -15,4 +16,11 @@ pub struct User {
user_id: i32,
}

#[allow(dead_code)]
fn from_row(row: &Row) {
let _ = Todo::from_row(row);
let _ = Todo::try_from_row(row).unwrap();

let _ = User::from_row(row);
let _ = Todo::try_from_row(row).unwrap();
}