tui_markup_renderer/
markup_element.rs

1use std::{fmt, cell::RefCell, collections::HashMap, rc::Rc};
2
3pub struct MarkupElement {
4    pub deep: usize,
5    pub id: String,
6    pub name: String,
7    pub order: i32,
8    pub text: Option<String>,
9    pub attributes: HashMap<String, String>,
10    pub children: Vec<Rc<RefCell<MarkupElement>>>,
11    pub parent_node: Option<Rc<RefCell<MarkupElement>>>,
12    pub dependencies: Vec<String>,
13}
14
15impl Clone for MarkupElement {
16    fn clone(&self) -> Self {
17        MarkupElement {
18            deep: self.deep,
19            id: self.id.clone(),
20            name: self.name.clone(),
21            text: self.text.clone(),
22            order: self.order,
23            attributes: self.attributes.clone(),
24            children: self.children.clone(),
25            parent_node: self.parent_node.clone(),
26            dependencies: self.dependencies.clone(),
27        }
28    }
29}
30
31impl fmt::Display for MarkupElement {
32    #[inline]
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        let attr_vls: String = self
35            .attributes
36            .keys()
37            .map(|key| {
38                let wrapped_value = self.attributes.get(key);
39                let value = if let Some(value) = wrapped_value { value } else { "" };
40                format!(" {}=\"{}\"", key, value)
41            })
42            .collect();
43        let children: String = self
44            .children
45            .iter()
46            .map(|child| format!("{}", child.as_ref().borrow()))
47            .collect();
48        let tab = "\t".repeat(self.deep);
49        let new_str = format!(
50            "{}<{}{}>\n{}\n{}</{}>\n",
51            tab, self.name, attr_vls, children, tab, self.name
52        );
53        fmt::Display::fmt(&new_str, f)
54    }
55}
56
57impl fmt::Debug for MarkupElement {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        let mut r = f.debug_struct("MarkupElement");
60        r.field("id", &self.id);
61        r.field("name", &self.name);
62        r.field("text", &self.text);
63        if self.order != -1 {
64            r.field("order", &self.order);
65        }
66        r.field("attributes", &self.attributes);
67        r.field("dependencies", &self.dependencies);
68        /*
69        r.field("children", &self.children);
70        // */
71        r.finish()
72    }
73}
74