use postgres_types::{FromSql, IsNull, ToSql, Type, to_sql_checked};
use uuid::Uuid;
use crate::{HeerId, RanjId};
impl ToSql for HeerId {
fn to_sql(
&self,
ty: &Type,
out: &mut bytes::BytesMut,
) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> {
<i64 as ToSql>::to_sql(&self.as_i64(), ty, out)
}
fn accepts(ty: &Type) -> bool {
*ty == Type::INT8
}
to_sql_checked!();
}
impl<'a> FromSql<'a> for HeerId {
fn from_sql(
ty: &Type,
raw: &'a [u8],
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let value = <i64 as FromSql>::from_sql(ty, raw)?;
HeerId::from_i64(value).map_err(|e| Box::new(e) as Box<dyn std::error::Error + Sync + Send>)
}
fn accepts(ty: &Type) -> bool {
*ty == Type::INT8
}
}
impl ToSql for RanjId {
fn to_sql(
&self,
ty: &Type,
out: &mut bytes::BytesMut,
) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> {
<Uuid as ToSql>::to_sql(&self.as_uuid(), ty, out)
}
fn accepts(ty: &Type) -> bool {
*ty == Type::UUID
}
to_sql_checked!();
}
impl<'a> FromSql<'a> for RanjId {
fn from_sql(
ty: &Type,
raw: &'a [u8],
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let uuid = <Uuid as FromSql>::from_sql(ty, raw)?;
RanjId::from_uuid(uuid).map_err(|e| Box::new(e) as Box<dyn std::error::Error + Sync + Send>)
}
fn accepts(ty: &Type) -> bool {
*ty == Type::UUID
}
}