use crate::{Attribute, Namespace};
pub struct Element<'a> {
name: &'a str,
namespaces: Vec<Namespace<'a>>,
attributes: Vec<Attribute<'a>>,
children: Vec<Element<'a>>,
}
impl<'a> Element<'a> {
pub fn new(name: &'a str) -> Self {
Element {
name,
namespaces: Vec::new(),
attributes: Vec::new(),
children: Vec::new(),
}
}
pub fn add_namespace(mut self, namespace: Namespace<'a>) -> Self {
self.namespaces.push(namespace);
self
}
pub fn add_attribute(mut self, attribute: Attribute<'a>) -> Self {
self.attributes.push(attribute);
self
}
pub fn add_child(mut self, child: Element<'a>) -> Self {
self.children.push(child);
self
}
}
impl std::fmt::Display for Element<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<{}>\n", self.name)?;
for namespace in &self.namespaces {
write!(f, " {}\n", namespace.to_string())?;
}
for attribute in &self.attributes {
write!(f, " {}\n", attribute.to_string())?;
}
write!(f,">\n")?;
for child in &self.children {
write!(f, " {}\n", child.to_string())?;
}
write!(f, "</{}>", self.name)?;
Ok(())
}
}