postgres_types/
pg_lsn.rs
1use bytes::BytesMut;
5use postgres_protocol::types;
6use std::error::Error;
7use std::fmt;
8use std::str::FromStr;
9
10use crate::{FromSql, IsNull, ToSql, Type};
11
12#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
14pub struct PgLsn(u64);
15
16#[derive(Debug)]
18pub struct ParseLsnError(());
19
20impl From<u64> for PgLsn {
21 fn from(lsn_u64: u64) -> Self {
22 PgLsn(lsn_u64)
23 }
24}
25
26impl From<PgLsn> for u64 {
27 fn from(lsn: PgLsn) -> u64 {
28 lsn.0
29 }
30}
31
32impl FromStr for PgLsn {
33 type Err = ParseLsnError;
34
35 fn from_str(lsn_str: &str) -> Result<Self, Self::Err> {
36 let Some((split_hi, split_lo)) = lsn_str.split_once('/') else {
37 return Err(ParseLsnError(()));
38 };
39 let (hi, lo) = (
40 u64::from_str_radix(split_hi, 16).map_err(|_| ParseLsnError(()))?,
41 u64::from_str_radix(split_lo, 16).map_err(|_| ParseLsnError(()))?,
42 );
43 Ok(PgLsn((hi << 32) | lo))
44 }
45}
46
47impl fmt::Display for PgLsn {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 write!(f, "{:X}/{:X}", self.0 >> 32, self.0 & 0x00000000ffffffff)
50 }
51}
52
53impl fmt::Debug for PgLsn {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 f.write_fmt(format_args!("{}", self))
56 }
57}
58
59impl<'a> FromSql<'a> for PgLsn {
60 fn from_sql(_: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
61 let v = types::lsn_from_sql(raw)?;
62 Ok(v.into())
63 }
64
65 accepts!(PG_LSN);
66}
67
68impl ToSql for PgLsn {
69 fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
70 types::lsn_to_sql((*self).into(), out);
71 Ok(IsNull::No)
72 }
73
74 accepts!(PG_LSN);
75
76 to_sql_checked!();
77}