summaryrefslogtreecommitdiff
path: root/src/types/rendered_element.rs
blob: cbc923da862d1ad8d1ac719253b69266f66274cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::collections::HashMap;

use unique_id::{string::StringGenerator, Generator};

use super::ElementName;

#[derive(Debug)]
pub struct RenderedElement {
    pub id: String,
    pub child_elements: Vec<RenderedElement>,
    pub name: ElementName,
    pub props: HashMap<&'static str, &'static str>,
}

impl RenderedElement {
    pub fn new(name: ElementName) -> Self {
        let gen = StringGenerator::default();
        Self {
            id: gen.next_id(),
            child_elements: vec![],
            name,
            props: HashMap::new(),
        }
    }

    pub fn from_vec(name: ElementName, child_elements: Vec<RenderedElement>) -> Self {
        let gen = StringGenerator::default();
        Self {
            id: gen.next_id(),
            child_elements,
            name,
            props: HashMap::new(),
        }
    }

    pub fn push(mut self, element: RenderedElement) -> Self {
        self.child_elements.push(element);
        self
    }

    pub fn set_property(&mut self, prop: &'static str, value: &'static str) {
        let prop_ref = self.props.entry(prop).or_insert(value);
        *prop_ref = value;
    }
}