|
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 | +//! |
6 | 65 |
|
7 | 66 | #[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 | +} |
9 | 71 |
|
10 | 72 | #[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 | +} |
12 | 77 |
|
| 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. |
13 | 79 | #[cfg(any(feature = "postgres", feature = "tokio-postgres"))] |
14 | 80 | 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>; |
17 | 92 | } |
18 | 93 |
|
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; |
21 | 97 |
|
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