diff options
Diffstat (limited to 'src/xml')
-rw-r--r-- | src/xml/element.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/xml/element.rs b/src/xml/element.rs index 91e4130..0f44182 100644 --- a/src/xml/element.rs +++ b/src/xml/element.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + #[derive(Debug, PartialEq, Eq, Clone)] pub struct Elements { @@ -144,6 +146,20 @@ pub enum Element Comment(String), } +impl Display for Element +{ + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + match self { + Self::Tagged(tagged) => tagged.fmt(formatter), + Self::Text(text) => text.fmt(formatter), + Self::Comment(comment) => { + formatter.write_fmt(format_args!("<!--{comment}-->")) + } + } + } +} + #[derive(Debug, PartialEq, Eq, Clone)] pub struct Tagged { @@ -152,6 +168,30 @@ pub struct Tagged attributes: Vec<Attribute>, } +impl Display for Tagged +{ + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + let name = &self.name; + + let attrs = self + .attributes + .iter() + .map(|attr| { + format!("{}=\"{}\" ", attr.key, String::from_utf8_lossy(&attr.value)) + }) + .collect::<String>(); + + let child_elements = self + .child_elements + .into_iter() + .map(ToString::to_string) + .collect::<String>(); + + formatter.write_fmt(format_args!("<{name} {attrs}>{child_elements}</{name}>",)) + } +} + impl Tagged { pub fn new<Name, ChildElements, Attrs>( |