#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

#56 in #row


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
~37K SLoC