postgres_types_extra/
pg_lsn.rs

1use bytes::{Buf, BufMut};
2use postgres_types::{FromSql, IsNull, ToSql, Type, accepts, to_sql_checked};
3use std::{error::Error, fmt};
4
5#[derive(Debug, Clone)]
6pub struct MyPgLsn {
7    pub lsn: u64,
8}
9
10impl fmt::Display for MyPgLsn {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        write!(f, "{:X}/{:X}", (self.lsn >> 32) as u32, self.lsn)
13    }
14}
15
16impl FromSql<'_> for MyPgLsn {
17    fn from_sql(_: &Type, mut raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
18        let lsn = raw.get_u64();
19
20        Ok(MyPgLsn { lsn })
21    }
22
23    accepts!(PG_LSN);
24}
25
26impl ToSql for MyPgLsn {
27    fn to_sql(
28        &self,
29        ty: &Type,
30        out: &mut bytes::BytesMut,
31    ) -> Result<postgres_types::IsNull, Box<dyn Error + Sync + Send>>
32    where
33        Self: Sized,
34    {
35        if ty.name() != "pg_lsn" {
36            return Err("Unexpected type".into());
37        }
38
39        out.put_u64(self.lsn);
40
41        Ok(IsNull::No)
42    }
43
44    accepts!(PG_LSN);
45
46    to_sql_checked!();
47}