Skip to main content

sentinel_driver/types/
xml.rs

1use bytes::{BufMut, BytesMut};
2
3use crate::error::{Error, Result};
4use crate::types::{FromSql, Oid, ToSql};
5
6/// PostgreSQL XML type -- a validated XML string.
7///
8/// On the wire, XML is just UTF-8 text with the XML OID.
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub struct PgXml(pub String);
11
12impl ToSql for PgXml {
13    fn oid(&self) -> Oid {
14        Oid::XML
15    }
16
17    fn to_sql(&self, buf: &mut BytesMut) -> Result<()> {
18        buf.put_slice(self.0.as_bytes());
19        Ok(())
20    }
21}
22
23impl FromSql for PgXml {
24    fn oid() -> Oid {
25        Oid::XML
26    }
27
28    fn from_sql(buf: &[u8]) -> Result<Self> {
29        let s = String::from_utf8(buf.to_vec())
30            .map_err(|e| Error::Decode(format!("xml: invalid UTF-8: {e}")))?;
31        Ok(PgXml(s))
32    }
33}