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
use crate::{XMLVersion, XML};

/// Builder structure used to generate a custom XML structure.
pub struct XMLBuilder {
    /// The XML version to set for the document.
    ///
    /// Defaults to `XML1.0`.
    version: XMLVersion,

    /// The encoding to set for the document.
    ///
    /// Defaults to `UTF-8`.
    encoding: String,

    /// XML standalone attribute.
    ///
    /// A `None` value indicates no displaying.
    ///
    /// Defaults to `None`
    standalone: Option<bool>,

    /// Whether we want to indentate the document.
    ///
    /// Defaults to `true`.
    indent: bool,

    /// Whether the XML attributes should be sorted or not.
    ///
    /// Defaults to `false`.
    sort_attributes: bool,
}

impl Default for XMLBuilder {
    fn default() -> Self {
        Self {
            version: XMLVersion::XML1_0,
            encoding: "UTF-8".into(),
            standalone: None,
            indent: true,
            sort_attributes: false,
        }
    }
}

impl XMLBuilder {
    /// Builds a new XMLBuilder
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the XML version attribute field.
    ///
    /// # Arguments
    ///
    /// `version` - An enum value representing the new version to use for the XML.
    pub fn version(mut self, version: XMLVersion) -> Self {
        self.version = version;

        self
    }

    /// Sets the XML encoding attribute field.
    ///
    /// # Arguments
    ///
    /// `encoding` - A String representing the encoding to use for the document.
    pub fn encoding(mut self, encoding: String) -> Self {
        self.encoding = encoding;

        self
    }

    /// Sets the standalone attribute for this XML document.
    pub fn standalone(mut self, standalone: Option<bool>) -> Self {
        self.standalone = standalone;

        self
    }

    /// Sets the XML indentation.
    pub fn indent(mut self, indent: bool) -> Self {
        self.indent = indent;

        self
    }

    /// Enables attributes sorting.
    pub fn sort_attributes(mut self, sort: bool) -> Self {
        self.sort_attributes = sort;

        self
    }

    /// Builds a new XML structure by consuming self.
    pub fn build(self) -> XML {
        XML::new(
            self.version,
            self.encoding,
            self.standalone,
            self.indent,
            self.sort_attributes,
        )
    }
}