Skip to content

Commit 07ed6e2

Browse files
committed
add docs
1 parent b831737 commit 07ed6e2

File tree

2 files changed

+157
-14
lines changed

2 files changed

+157
-14
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,65 @@
11
# postgres-from-row
2-
Derive `FromRow` to automaticly convert postgres rows to structs.
2+
3+
Derive [`FromRow`] to generate a mapping between a struct and postgres rows.
4+
5+
This crate works with [postgres](<https://docs.rs/postgres>) by default.
6+
7+
```toml
8+
[dependencies]
9+
postgres_from_row = "0.1.0"
10+
```
11+
12+
If you want to use it with [tokio-postgres](<https://docs.rs/tokio-postgres>), enable it like so:
13+
14+
```toml
15+
[dependencies]
16+
postgres_from_row = { version = "0.1.0", default_features = false, features = ["tokio-postgres"] }
17+
```
18+
## Examples
19+
```rust
20+
use postgres_from_row::FromRow;
21+
22+
#[derive(FromRow)]
23+
struct Todo {
24+
todo_id: i32,
25+
text: String
26+
author_id: i32,
27+
}
28+
29+
let row = client.query_one("SELECT todo_id, text, author_id FROM todos").unwrap();
30+
31+
// Pass a row with the correct columns.
32+
let todo = Todo::from_row(&row);
33+
34+
let row = client.query_one("SELECT foo FROM bar").unwrap();
35+
36+
// Use `try_from_row` if the operation could fail.
37+
let todo = Todo::try_from_row(&row);
38+
assert!(todo.is_err());
39+
```
40+
41+
Each field need's to implement [`postgres::types::FromSql`], as this will be used to convert a
42+
single column to the specified type. If you want to override this behavior and delegate it to a
43+
nested structure that also implements [`FromRow`], use `#[from_row(flatten)]`:
44+
45+
```rust
46+
use postgres_from_row::FromRow;
47+
48+
#[derive(FromRow)]
49+
struct Todo {
50+
todo_id: i32,
51+
text: String,
52+
#[from_row(flatten)]
53+
author: User
54+
}
55+
56+
#[derive(FromRow)]
57+
struct User {
58+
user_id: i32,
59+
username: String
60+
}
61+
62+
let row = client.query_one("SELECT todo_id, text, user_id, username FROM todos t, users u WHERE t.author_id = u.user_id").unwrap();
63+
let todo = Todo::from_row(&row);
64+
```
65+

src/lib.rs

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,103 @@
1-
#[cfg(feature = "postgres")]
2-
use postgres::{Error, Row};
3-
4-
#[cfg(feature = "tokio-postgres")]
5-
use tokio_postgres::{Error, Row};
1+
#![deny(missing_docs)]
2+
//! Derive [`FromRow`] to generate a mapping between a struct and postgres rows.
3+
//!
4+
//! This crate works with [postgres](<https://docs.rs/postgres>) by default.
5+
//!
6+
//! ```toml
7+
//! [dependencies]
8+
//! postgres_from_row = "0.1.0"
9+
//! ```
10+
//!
11+
//! If you want to use it with [tokio-postgres](<https://docs.rs/tokio-postgres>), enable it like so:
12+
//!
13+
//! ```toml
14+
//! [dependencies]
15+
//! postgres_from_row = { version = "0.1.0", default_features = false, features = ["tokio-postgres"] }
16+
//! ```
17+
//! # Examples
18+
//! ```rust
19+
//! use postgres_from_row::FromRow;
20+
//!
21+
//! #[derive(FromRow)]
22+
//! struct Todo {
23+
//! todo_id: i32,
24+
//! text: String
25+
//! author_id: i32,
26+
//! }
27+
//!
28+
//! let row = client.query_one("SELECT todo_id, text, author_id FROM todos").unwrap();
29+
//!
30+
//! // Pass a row with the correct columns.
31+
//! let todo = Todo::from_row(&row);
32+
//!
33+
//! let row = client.query_one("SELECT foo FROM bar").unwrap();
34+
//!
35+
//! // Use `try_from_row` if the operation could fail.
36+
//! let todo = Todo::try_from_row(&row);
37+
//! assert!(todo.is_err());
38+
//! ```
39+
//!
40+
//! Each field need's to implement [`postgres::types::FromSql`], as this will be used to convert a
41+
//! single column to the specified type. If you want to override this behavior and delegate it to a
42+
//! nested structure that also implements [`FromRow`], use `#[from_row(flatten)]`:
43+
//!
44+
//! ```rust
45+
//! use postgres_from_row::FromRow;
46+
//!
47+
//! #[derive(FromRow)]
48+
//! struct Todo {
49+
//! todo_id: i32,
50+
//! text: String,
51+
//! #[from_row(flatten)]
52+
//! author: User
53+
//! }
54+
//!
55+
//! #[derive(FromRow)]
56+
//! struct User {
57+
//! user_id: i32,
58+
//! username: String
59+
//! }
60+
//!
61+
//! let row = client.query_one("SELECT todo_id, text, user_id, username FROM todos t, users u WHERE t.author_id = u.user_id").unwrap();
62+
//! let todo = Todo::from_row(&row);
63+
//! ```
64+
//!
665
766
#[cfg(feature = "postgres")]
8-
pub use postgres_from_row_derive::FromRowPostgres as FromRow;
67+
mod active_postgres {
68+
pub use postgres::{Error, Row};
69+
pub use postgres_from_row_derive::FromRowPostgres as FromRow;
70+
}
971

1072
#[cfg(feature = "tokio-postgres")]
11-
pub use postgres_from_row_derive::FromRowTokioPostgres as FromRow;
73+
mod active_postgres {
74+
pub use postgres_from_row_derive::FromRowTokioPostgres as FromRow;
75+
pub use tokio_postgres::{Error, Row};
76+
}
1277

78+
/// A trait that allows mapping rows from either [postgres](<https://docs.rs/postgres>) or [tokio-postgres](<https://docs.rs/tokio-postgres>), to other types.
1379
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
1480
pub trait FromRow: Sized {
15-
fn from_row(row: &Row) -> Self;
16-
fn try_from_row(row: &Row) -> Result<Self, Error>;
81+
/// Performce the conversion
82+
///
83+
/// # Panics
84+
///
85+
/// panics if the row does not contain the expected column names.
86+
fn from_row(row: &active_postgres::Row) -> Self;
87+
88+
/// Try's to perform the conversion.
89+
///
90+
/// Will return an error if the row does not contain the expected column names.
91+
fn try_from_row(row: &active_postgres::Row) -> Result<Self, active_postgres::Error>;
1792
}
1893

19-
#[cfg(all(feature = "postgres", feature = "tokio-postgres"))]
20-
compile_error!("Can't combine feature `postgres` and `tokio-postgres`");
94+
#[doc(no_inline)]
95+
/// gfdsfd
96+
pub use active_postgres::FromRow;
2197

22-
#[cfg(not(any(feature = "postgres", feature = "tokio-postgres")))]
23-
compile_error!("Must have at least one enabled feature: `postgres` or `tokio-postgres`.");
98+
//
99+
// #[cfg(all(feature = "postgres", feature = "tokio-postgres"))]
100+
// compile_error!("Can't combine feature `postgres` and `tokio-postgres`");
101+
//
102+
// #[cfg(not(any(feature = "postgres", feature = "tokio-postgres")))]
103+
// compile_error!("Must have at least one enabled feature: `postgres` or `tokio-postgres`.");

0 commit comments

Comments
 (0)