#row #postgresql #derive

macro pg_mapper

derive TryFrom<tokio_postgres::Row>

3 unstable releases

0.2.1 Mar 10, 2022
0.2.0 Feb 6, 2022
0.1.0 Dec 27, 2021

#61 in #row

Download history 15/week @ 2024-02-21 40/week @ 2024-02-28 21/week @ 2024-03-06 2/week @ 2024-03-13

78 downloads per month
Used in inline-sql

MIT license

6KB
89 lines

#[derive(TryFromRow)]

Documentation | Crates.io

Example

/// This will try to get each column value by name.
#[derive(pg_mapper::TryFromRow)]
struct User {
    email: String,
    password_digest: String,
}

/// This will try to get each column value by index.
#[derive(pg_mapper::TryFromRow)]
struct Point(i32, i32, i32);

Generates:

impl TryFrom<tokio_postgres::Row> for User {
    type Error = tokio_postgres::Error;
    fn try_from(row: tokio_postgres::Row) -> Result<Self, Self::Error> {
        Ok(Self {
            email: row.try_get("email")?,
            password_digest: row.try_get("password_digest")?,
        })
    }
}

impl TryFrom<tokio_postgres::Row> for Point {
    type Error = tokio_postgres::Error;
    fn try_from(row: tokio_postgres::Row) -> Result<Self, Self::Error> {
        Ok(Self(row.try_get(0)?, row.try_get(1)?, row.try_get(2)?))
    }
}

Dependencies

~1.5MB
~34K SLoC