pg_mapper

Crates.iopg_mapper
lib.rspg_mapper
version0.2.1
sourcesrc
created_at2021-12-27 14:27:36.418798
updated_at2022-03-10 13:04:59.82265
descriptionderive `TryFrom`
homepage
repositoryhttps://github.com/malobre/pg_mapper
max_upload_size
id503737
size7,051
Maël Obréjan (malobre)

documentation

README

#[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)?))
    }
}
Commit count: 19

cargo fmt