Trait sqlm_postgres::SqlType

source ·
pub trait SqlType {
    type Type;

    // Provided method
    fn query_literal<'a>(
        sql: &'a Sql<'a, Primitive<Self::Type>, Self>,
        conn: impl Connection + 'a,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'a>>
       where Self: FromSqlOwned + ToSql + Send + Sync,
             Self::Type: Send + Sync { ... }
}
Expand description

A trait used to which Rust type a Postgres type is read into.

§Example

This can be useful to implement manually when e.g. reading a Postgres string column into an enum.

#[derive(Debug, Default, Clone, Copy)]
pub enum Role {
    #[default]
    User,
    Admin,
}

impl sqlm_postgres::SqlType for Role {
    type Type = String;
}

impl Role {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::User => "user",
            Self::Admin => "admin",
        }
    }
}

impl<'a> sqlm_postgres::FromSql<'a> for Role {
    fn from_sql(
        ty: &postgres_types::Type,
        raw: &'a [u8],
    ) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
        use std::str::FromStr;
        Ok(Role::from_str(<&str>::from_sql(ty, raw)?)?)
    }

    fn accepts(ty: &postgres_types::Type) -> bool {
        <&str as sqlm_postgres::FromSql<'_>>::accepts(ty)
    }
}

impl sqlm_postgres::ToSql for Role {
    fn to_sql(
        &self,
        ty: &postgres_types::Type,
        out: &mut bytes::BytesMut,
    ) -> Result<postgres_types::IsNull, Box<dyn std::error::Error + Sync + Send>>
    where
        Self: Sized,
    {
        <&str as sqlm_postgres::ToSql>::to_sql(&self.as_str(), ty, out)
    }

    fn accepts(ty: &postgres_types::Type) -> bool
    where
        Self: Sized,
    {
        <&str as sqlm_postgres::ToSql>::accepts(ty)
    }

    fn to_sql_checked(
        &self,
        ty: &postgres_types::Type,
        out: &mut bytes::BytesMut,
    ) -> Result<postgres_types::IsNull, Box<dyn std::error::Error + Sync + Send>> {
        <&str as sqlm_postgres::ToSql>::to_sql_checked(&self.as_str(), ty, out)
    }
}

impl std::str::FromStr for Role {
    type Err = UnknownRole;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "user" => Ok(Role::User),
            "admin" => Ok(Role::Admin),
            s => Err(UnknownRole(s.to_string())),
        }
    }
}

#[derive(Debug)]
pub struct UnknownRole(String);

impl std::fmt::Display for UnknownRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Unknown bevel lhs `{}`", self.0)
    }
}

impl std::error::Error for UnknownRole {}

Required Associated Types§

Provided Methods§

source

fn query_literal<'a>( sql: &'a Sql<'a, Primitive<Self::Type>, Self>, conn: impl Connection + 'a, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'a>>
where Self: FromSqlOwned + ToSql + Send + Sync, Self::Type: Send + Sync,

Implementations on Foreign Types§

source§

impl SqlType for Value

§

type Type = Value

source§

impl SqlType for bool

§

type Type = bool

source§

impl SqlType for f32

§

type Type = f32

source§

impl SqlType for f64

§

type Type = f64

source§

impl SqlType for i32

§

type Type = i32

source§

impl SqlType for i64

§

type Type = i64

source§

impl SqlType for String

§

type Type = String

source§

impl SqlType for Vector

§

type Type = Vector

source§

impl SqlType for Date

§

type Type = Date

source§

impl SqlType for OffsetDateTime

source§

impl SqlType for Uuid

§

type Type = Uuid

source§

impl<'a> SqlType for &'a str

§

type Type = String

Implementors§