Debian Codegraph @

src/pg_lsn.rs

raw symbols SCIP
1 //! Log Sequence Number (LSN) type for PostgreSQL Write-Ahead Log
2 //! (WAL), also known as the transaction log.
3
4 use bytes::BytesMut;
5 use postgres_protocol::types;
6 use std::error::Error;
7 use std::fmt;
8 use std::str::FromStr;
9
10 use crate::{FromSql, IsNull, ToSql, Type};
11
12 /// Postgres `PG_LSN` type.
13 #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
14 pub struct PgLsn(u64);
15
16 /// Error parsing LSN.
17 #[derive(Debug)]
18 pub struct ParseLsnError(());
19
20 impl From<u64> for PgLsn {
21 fn from(lsn_u64: u64) -> Self {
22 PgLsn(lsn_u64)
23 }
24 }
25
26 impl From<PgLsn> for u64 {
27 fn from(lsn: PgLsn) -> u64 {
28 lsn.0
29 }
30 }
31
32 impl 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
47 impl 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
53 impl fmt::Debug for PgLsn {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 fmt::Display::fmt(self, f)
56 }
57 }
58
59 impl<'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
68 impl 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 }