rustical_xml/
se.rs

1use crate::XmlRootTag;
2use quick_xml::{
3    events::{BytesStart, Event, attributes::Attribute},
4    name::Namespace,
5};
6use std::collections::HashMap;
7pub use xml_derive::XmlSerialize;
8
9pub trait XmlSerialize {
10    fn serialize(
11        &self,
12        ns: Option<Namespace>,
13        tag: Option<&str>,
14        namespaces: &HashMap<Namespace, &str>,
15        writer: &mut quick_xml::Writer<&mut Vec<u8>>,
16    ) -> std::io::Result<()>;
17
18    fn attributes<'a>(&self) -> Option<Vec<Attribute<'a>>>;
19}
20
21impl<T: XmlSerialize> XmlSerialize for Option<T> {
22    fn serialize(
23        &self,
24        ns: Option<Namespace>,
25        tag: Option<&str>,
26        namespaces: &HashMap<Namespace, &str>,
27        writer: &mut quick_xml::Writer<&mut Vec<u8>>,
28    ) -> std::io::Result<()> {
29        if let Some(some) = self {
30            some.serialize(ns, tag, namespaces, writer)
31        } else {
32            Ok(())
33        }
34    }
35
36    fn attributes<'a>(&self) -> Option<Vec<Attribute<'a>>> {
37        None
38    }
39}
40
41pub trait XmlSerializeRoot {
42    fn serialize_root(&self, writer: &mut quick_xml::Writer<&mut Vec<u8>>) -> std::io::Result<()>;
43
44    fn serialize_to_string(&self) -> std::io::Result<String> {
45        let mut buf: Vec<_> = b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".into();
46        let mut writer = quick_xml::Writer::new_with_indent(&mut buf, b' ', 4);
47        self.serialize_root(&mut writer)?;
48        Ok(String::from_utf8_lossy(&buf).to_string())
49    }
50}
51
52impl<T: XmlSerialize + XmlRootTag> XmlSerializeRoot for T {
53    fn serialize_root(&self, writer: &mut quick_xml::Writer<&mut Vec<u8>>) -> std::io::Result<()> {
54        let namespaces = Self::root_ns_prefixes();
55        self.serialize(Self::root_ns(), Some(Self::root_tag()), &namespaces, writer)
56    }
57}
58
59impl XmlSerialize for () {
60    fn serialize(
61        &self,
62        ns: Option<Namespace>,
63        tag: Option<&str>,
64        namespaces: &HashMap<Namespace, &str>,
65        writer: &mut quick_xml::Writer<&mut Vec<u8>>,
66    ) -> std::io::Result<()> {
67        let prefix = ns
68            .map(|ns| namespaces.get(&ns))
69            .unwrap_or(None)
70            .map(|prefix| {
71                if !prefix.is_empty() {
72                    [*prefix, ":"].concat()
73                } else {
74                    String::new()
75                }
76            });
77        let has_prefix = prefix.is_some();
78        let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
79        if let Some(tagname) = tagname.as_ref() {
80            let mut bytes_start = BytesStart::new(tagname);
81            if !has_prefix && let Some(ns) = &ns {
82                bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
83            }
84            writer.write_event(Event::Empty(bytes_start))?;
85        }
86        Ok(())
87    }
88
89    fn attributes<'a>(&self) -> Option<Vec<quick_xml::events::attributes::Attribute<'a>>> {
90        None
91    }
92}