float_pigment_mlp/node/
element.rs

1use std::{
2    cell::{RefCell, RefMut},
3    rc::Rc,
4};
5
6use super::{attribute::Attribute, NodeType};
7
8pub struct Element {
9    tag: String,
10    attributes: Attribute,
11    children: RefCell<Vec<Rc<NodeType>>>,
12}
13
14impl std::fmt::Debug for Element {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        write!(
17            f,
18            "Element {{ tag: {:?}, attributes: {:?}, children: {:?} }}",
19            self.tag,
20            self.attributes,
21            self.children.borrow()
22        )
23    }
24}
25
26impl Element {
27    pub(crate) fn new(tag: String) -> Self {
28        Self {
29            attributes: Attribute::default(),
30            tag,
31            children: RefCell::new(vec![]),
32        }
33    }
34    pub fn tag(&self) -> &str {
35        self.tag.as_str()
36    }
37    pub fn children_mut(&self) -> RefMut<'_, Vec<Rc<NodeType>>> {
38        self.children.borrow_mut()
39    }
40    pub fn for_each_child(&self, f: Box<dyn Fn(&NodeType)>) {
41        self.children
42            .borrow()
43            .iter()
44            .for_each(|item| f(item.as_ref()))
45    }
46    pub fn attributes(&self) -> &Attribute {
47        &self.attributes
48    }
49}