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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::{io::Cursor, str::Utf8Error, string::FromUtf8Error};

use quick_xml::{events::BytesDecl, Writer};
use thiserror::Error;

use crate::ty::XmlTag;

#[derive(Error, Debug)]

pub enum XmlSerializeError {
    #[error(transparent)]
    Xml(#[from] quick_xml::Error),
    #[error(transparent)]
    Utf8String(#[from] FromUtf8Error),
    #[error(transparent)]
    Utf8Error(#[from] Utf8Error),
    #[error("missing root element name, try to implement 'fn root() -> XmlTag {{ b\"my-root-element-name\" }}'")]
    MissingRoot,
}

pub type XmlSerializeResult<T> = Result<T, XmlSerializeError>;

pub trait XmlSerialize {
    fn is_enum() -> bool {
        false
    }

    fn root() -> Option<XmlTag> {
        None
    }

    fn xml_serialize<W: std::io::Write>(
        &self,
        tag: &str,
        writer: &mut Writer<W>,
    ) -> XmlSerializeResult<()>;
}

pub fn to_string<T>(value: &T) -> XmlSerializeResult<String>
where
    T: XmlSerialize,
{
    let mut writer = Writer::new(Cursor::new(Vec::new()));
    let name = if T::is_enum() {
        ""
    } else {
        std::str::from_utf8(T::root().ok_or(XmlSerializeError::MissingRoot)?)?
    };
    value.xml_serialize(name, &mut writer)?;
    Ok(String::from_utf8(writer.into_inner().into_inner())?)
}

pub fn to_string_with_decl<T>(value: &T) -> XmlSerializeResult<String>
where
    T: XmlSerialize,
{
    let mut writer = Writer::new(Cursor::new(Vec::new()));
    writer.write_event(quick_xml::events::Event::Decl(BytesDecl::new(
        "1.0",
        Some("UTF-8"),
        Some("yes"),
    )))?;
    let name = if T::is_enum() {
        ""
    } else {
        std::str::from_utf8(T::root().ok_or(XmlSerializeError::MissingRoot)?)?
    };
    value.xml_serialize(name, &mut writer)?;
    Ok(String::from_utf8(writer.into_inner().into_inner())?)
}

pub fn to_string_pretty<T>(value: &T) -> XmlSerializeResult<String>
where
    T: XmlSerialize,
{
    let mut writer = Writer::new_with_indent(Cursor::new(Vec::new()), b' ', 2);
    let name = if T::is_enum() {
        ""
    } else {
        std::str::from_utf8(T::root().ok_or(XmlSerializeError::MissingRoot)?)?
    };

    value.xml_serialize(name, &mut writer)?;
    Ok(String::from_utf8(writer.into_inner().into_inner())?)
}

pub fn to_string_pretty_with_decl<T>(value: &T) -> XmlSerializeResult<String>
where
    T: XmlSerialize,
{
    let mut writer = Writer::new_with_indent(Cursor::new(Vec::new()), b' ', 2);
    writer.write_event(quick_xml::events::Event::Decl(BytesDecl::new(
        "1.0",
        Some("UTF-8"),
        Some("yes"),
    )))?;
    let name = if T::is_enum() {
        ""
    } else {
        std::str::from_utf8(T::root().ok_or(XmlSerializeError::MissingRoot)?)?
    };

    value.xml_serialize(name, &mut writer)?;
    Ok(String::from_utf8(writer.into_inner().into_inner())?)
}