1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#[cfg_attr(docsrs, doc(cfg(feature = "xml")))]
impl crate::ToSql for xmltree::Element {
    fn ty(&self) -> crate::pq::Type {
        crate::pq::types::XML
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/xml.c#L336
     */
    fn to_text(&self) -> crate::Result<Option<String>> {
        let mut vec = Vec::new();

        self.write(&mut vec)
            .map_err(|e| self.error(&e.to_string()))?;

        Ok(Some(String::from_utf8(vec)?))
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/xml.c#L418
     */
    fn to_binary(&self) -> crate::Result<Option<Vec<u8>>> {
        let mut buf = Vec::new();

        self.write(&mut buf)
            .map_err(|e| self.error(&e.to_string()))?;

        Ok(Some(buf))
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "xml")))]
impl crate::FromSql for xmltree::Element {
    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/xml.c#L258
     */
    fn from_text(ty: &crate::pq::Type, raw: Option<&str>) -> crate::Result<Self> {
        xmltree::Element::parse(crate::not_null(raw)?.as_bytes()).map_err(|_| Self::error(ty, raw))
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/xml.c#L351
     */
    fn from_binary(ty: &crate::pq::Type, raw: Option<&[u8]>) -> crate::Result<Self> {
        let s = String::from_binary(ty, raw)?;

        Self::from_text(ty, Some(&s))
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "xml")))]
impl crate::entity::Simple for xmltree::Element {}

#[cfg(test)]
mod test {
    static XML: &str = r#"<?xml version="1.0"?>
<!-- Awesome data incoming -->
<data awesome="true">
  <datum>Science</datum>
  <datum><![CDATA[Literature]]></datum>
  <datum>Math &gt; others</datum>
</data>"#;

    crate::sql_test!(
        xml,
        xmltree::Element,
        [(
            &format!("'{}'", super::XML),
            xmltree::Element::parse(super::XML.as_bytes()).unwrap()
        )]
    );
}