html_rs/
tags.rs

1mod body;
2mod head;
3mod html;
4mod script;
5mod style;
6
7use std::{
8    collections::BTreeMap,
9    fmt::{Debug, Display},
10};
11
12use crate::elements::ElementName;
13
14pub use self::{body::HtmlBody, head::HtmlHead, html::Html, script::HtmlScript, style::HtmlStyle};
15
16#[derive(Debug)]
17pub struct Tag {
18    pub element: Box<dyn ElementName>,
19    pub attrs: BTreeMap<String, String>,
20}
21
22impl PartialEq for Tag {
23    fn eq(&self, other: &Self) -> bool {
24        self.element.name() == other.element.name() && self.attrs == other.attrs
25    }
26}
27
28impl Eq for Tag {}
29
30impl Display for Tag {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        let mut output = format!("{}", self.element.name());
33        let max_idx = self.attrs.len();
34        if max_idx > 0 {
35            output.push(' ');
36        }
37        for (idx, (name, value)) in self.attrs.iter().enumerate() {
38            output.push_str(format!(r#"{name}="{value}""#).as_str());
39            if idx + 1 < max_idx {
40                output.push(' ')
41            }
42        }
43        write!(f, "{output}")
44    }
45}
46impl Tag {
47    pub fn set_attr<K: AsRef<str>, V: AsRef<str>>(&mut self, key: K, value: V) {
48        let _b = self
49            .attrs
50            .insert(key.as_ref().to_owned(), value.as_ref().to_owned());
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    // use super::*;
57
58    #[test]
59    #[ignore = "Todo"]
60    fn ok_on_build_html_tag() {
61        todo!()
62    }
63}