xml_builder/builder.rs
1use crate::{XMLVersion, XML};
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: 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: "UTF-8".into(),
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 pub fn new() -> Self {
60 Self::default()
61 }
62
63 /// Sets the XML version attribute field.
64 ///
65 /// # Arguments
66 ///
67 /// `version` - An enum value representing the new version to use for the XML.
68 pub fn version(mut self, version: XMLVersion) -> Self {
69 self.version = version;
70
71 self
72 }
73
74 /// Sets the XML encoding attribute field.
75 ///
76 /// # Arguments
77 ///
78 /// `encoding` - A String representing the encoding to use for the document.
79 pub fn encoding(mut self, encoding: String) -> Self {
80 self.encoding = encoding;
81
82 self
83 }
84
85 /// Sets the standalone attribute for this XML document.
86 pub fn standalone(mut self, standalone: Option<bool>) -> Self {
87 self.standalone = standalone;
88
89 self
90 }
91
92 /// Sets the XML indentation.
93 pub fn indent(mut self, indent: bool) -> Self {
94 self.indent = indent;
95
96 self
97 }
98
99 /// Enables attributes sorting.
100 pub fn sort_attributes(mut self, sort: bool) -> Self {
101 self.sort_attributes = sort;
102
103 self
104 }
105
106 /// Sets whether to break lines.
107 pub fn break_lines(mut self, break_lines: bool) -> Self {
108 self.break_lines = break_lines;
109
110 self
111 }
112
113 /// Sets whether to expand empty tags.
114 pub fn expand_empty_tags(mut self, expand_empty_tags: bool) -> Self {
115 self.expand_empty_tags = expand_empty_tags;
116
117 self
118 }
119
120 /// Builds a new XML structure by consuming self.
121 pub fn build(self) -> XML {
122 XML::new(
123 self.version,
124 self.encoding,
125 self.standalone,
126 self.indent,
127 self.sort_attributes,
128 self.break_lines,
129 self.expand_empty_tags,
130 )
131 }
132}