einfach_xml_builder/
element.rs

1use crate::{Attribute, Namespace};
2
3/// Represents an XML element.
4pub struct Element<'a> {
5    /// The name of the element.
6    name: &'a str,
7    /// The namespaces associated with the element.
8    namespaces: Vec<Namespace<'a>>,
9    /// The attributes of the element.
10    attributes: Vec<Attribute<'a>>,
11    /// The child elements of the element.
12    children: Vec<Element<'a>>,
13}
14
15impl<'a> Element<'a> {
16    /// Creates a new instance of `Element` with the given name.
17    ///
18    /// # Arguments
19    ///
20    /// * `name` - The name of the element.
21    ///
22    /// # Example
23    ///
24    /// ```
25    /// let element = Element::new("root");
26    /// ```
27    pub fn new(name: &'a str) -> Self {
28        Element {
29            name,
30            namespaces: Vec::new(),
31            attributes: Vec::new(),
32            children: Vec::new(),
33        }
34    }
35
36    /// Adds a namespace to the element and returns a modified `Element`.
37    ///
38    /// # Arguments
39    ///
40    /// * `namespace` - The namespace to be added.
41    ///
42    /// # Example
43    ///
44    /// ```
45    /// let element = Element::new("root")
46    ///     .add_namespace(Namespace::new("name", "http://example.com"));
47    /// ```
48    pub fn add_namespace(mut self, namespace: Namespace<'a>) -> Self {
49        self.namespaces.push(namespace);
50        self
51    }
52
53    /// Adds an attribute to the element and returns a modified `Element`.
54    ///
55    /// # Arguments
56    ///
57    /// * `attribute` - The attribute to be added.
58    ///
59    /// # Example
60    ///
61    /// ```
62    /// let element = Element::new("root")
63    ///     .add_attribute(Attribute::new("attr1", "value1"));
64    /// ```
65    pub fn add_attribute(mut self, attribute: Attribute<'a>) -> Self {
66        self.attributes.push(attribute);
67        self
68    }
69
70    /// Adds a child element to the element and returns a modified `Element`.
71    ///
72    /// # Arguments
73    ///
74    /// * `child` - The child element to be added.
75    ///
76    /// # Example
77    ///
78    /// ```
79    /// let child = Element::new("child");
80    /// let element = Element::new("root")
81    ///     .add_child(child);
82    /// ```
83    pub fn add_child(mut self, child: Element<'a>) -> Self {
84        self.children.push(child);
85        self
86    }
87}
88
89impl std::fmt::Display for Element<'_> {
90    /// Formats the element and its content as an XML string.
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        write!(f, "<{}>\n", self.name)?;
93
94        for namespace in &self.namespaces {
95            write!(f, "    {}\n", namespace.to_string())?;
96        }
97
98        for attribute in &self.attributes {
99            write!(f, "    {}\n", attribute.to_string())?;
100        }
101        write!(f,">\n")?;
102        for child in &self.children {
103            write!(f, "    {}\n", child.to_string())?;
104        }
105
106        write!(f, "</{}>", self.name)?;
107        Ok(())
108    }
109}