Skip to main content

xml_builder/
builder.rs

1use crate::{XML, XMLVersion};
2
3/// Builder structure used to generate a custom XML structure.
4pub struct XMLBuilder {
5    /// The XML version to set for the document.
6    ///
7    /// Defaults to `XML1.0`.
8    version: XMLVersion,
9
10    /// The encoding to set for the document.
11    ///
12    /// Defaults to `UTF-8`.
13    encoding: Option<String>,
14
15    /// XML standalone attribute.
16    ///
17    /// A `None` value indicates no displaying.
18    ///
19    /// Defaults to `None`
20    standalone: Option<bool>,
21
22    /// Whether we want to indentate the document.
23    ///
24    /// Defaults to `true`.
25    indent: bool,
26
27    /// Whether the XML attributes should be sorted or not.
28    ///
29    /// Defaults to `false`.
30    sort_attributes: bool,
31
32    /// Whether we want to break lines or not.
33    ///
34    /// Defaults to `true`.
35    break_lines: bool,
36
37    /// Whether we want to expand empty tags or not.
38    ///
39    /// Defaults to `false`.
40    expand_empty_tags: bool,
41}
42
43impl Default for XMLBuilder {
44    fn default() -> Self {
45        Self {
46            version: XMLVersion::XML1_0,
47            encoding: None,
48            standalone: None,
49            indent: true,
50            sort_attributes: false,
51            break_lines: true,
52            expand_empty_tags: false,
53        }
54    }
55}
56
57impl XMLBuilder {
58    /// Builds a new `XMLBuilder`.
59    #[must_use]
60    pub fn new() -> Self {
61        Self::default()
62    }
63
64    /// Sets the XML version attribute field.
65    ///
66    /// # Arguments
67    ///
68    /// `version` - An enum value representing the new version to use for the XML.
69    #[must_use]
70    pub const fn version(mut self, version: XMLVersion) -> Self {
71        self.version = version;
72
73        self
74    }
75
76    /// Sets the XML encoding attribute field.
77    ///
78    /// # Arguments
79    ///
80    /// `encoding` - A String representing the encoding to use for the document.
81    #[must_use]
82    pub fn encoding(mut self, encoding: String) -> Self {
83        self.encoding = Some(encoding);
84
85        self
86    }
87
88    /// Sets the standalone attribute for this XML document.
89    #[must_use]
90    pub const fn standalone(mut self, standalone: Option<bool>) -> Self {
91        self.standalone = standalone;
92
93        self
94    }
95
96    /// Sets the XML indentation.
97    #[must_use]
98    pub const fn indent(mut self, indent: bool) -> Self {
99        self.indent = indent;
100
101        self
102    }
103
104    /// Enables attributes sorting.
105    #[must_use]
106    pub const fn sort_attributes(mut self, sort: bool) -> Self {
107        self.sort_attributes = sort;
108
109        self
110    }
111
112    /// Sets whether to break lines.
113    #[must_use]
114    pub const fn break_lines(mut self, break_lines: bool) -> Self {
115        self.break_lines = break_lines;
116
117        self
118    }
119
120    /// Sets whether to expand empty tags.
121    #[must_use]
122    pub const fn expand_empty_tags(mut self, expand_empty_tags: bool) -> Self {
123        self.expand_empty_tags = expand_empty_tags;
124
125        self
126    }
127
128    /// Builds a new XML structure by consuming self.
129    #[must_use]
130    pub fn build(self) -> XML {
131        XML::new(
132            self.version,
133            self.encoding,
134            self.standalone,
135            self.indent,
136            self.sort_attributes,
137            self.break_lines,
138            self.expand_empty_tags,
139        )
140    }
141}