einfach_xml_builder/builder.rs
1use crate::{Declaration, Element};
2
3/// Represents a builder for constructing an XML document.
4pub struct Builder<'a> {
5 /// The XML declaration.
6 declaration: Declaration<'a>,
7 /// The root element of the XML document.
8 element: Element<'a>,
9}
10
11impl<'a> Builder<'a> {
12 /// Creates a new instance of `Builder` with the given declaration and root element.
13 ///
14 /// # Arguments
15 ///
16 /// * `declaration` - The XML declaration.
17 /// * `element` - The root element of the XML document.
18 ///
19 /// # Example
20 ///
21 /// ```
22 /// let declaration = Declaration::new("1.0", "UTF-8");
23 /// let element = Element::new("root");
24 /// let builder = Builder::new(declaration, element);
25 /// ```
26 pub fn new(declaration: Declaration<'a>, element: Element<'a>) -> Self {
27 Builder {
28 declaration,
29 element,
30 }
31 }
32}
33
34impl<'a> std::fmt::Display for Builder<'a> {
35 /// Formats the builder and its content as an XML document string.
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 write!(f, "{}\n", self.declaration.to_string())?;
38 write!(f, "{}", self.element.to_string())?;
39 Ok(())
40 }
41}