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
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::collections::HashMap;
use web_sys::{window, Element};

pub struct Component {
    name: String,
    attributes: HashMap<String, String>,
    children: Vec<Box<Renderable>>,
}

impl Component {
    pub fn new(
        name: &str,
        attributes: HashMap<String, String>,
        children: Vec<Box<Renderable>>,
    ) -> Component {
        Component {
            name: name.into(),
            attributes,
            children,
        }
    }
}

fn attr_to_string(key: &str, value: &str) -> String {
    format!(" {}='{}'", key, value)
}

pub trait Renderable {
    #[cfg(target_arch = "wasm32")]
    fn render(&self) -> Element;

    #[cfg(not(target_arch = "wasm32"))]
    fn render(&self) -> String;
}

impl Renderable for Component {
    fn render(&self) -> String {
        let attributes = self
            .attributes
            .iter()
            .map(|(key, value)| attr_to_string(&key, &value))
            .collect::<Vec<String>>()
            .join("");

        let children = self
            .children
            .iter()
            .map(|child| child.render())
            .collect::<Vec<String>>()
            .join("");

        if children.len() > 0 {
            format!("<{}{}>{}<{}>", &self.name, attributes, children, &self.name)
        } else {
            format!("<{}{} />", &self.name, attributes)
        }
    }
}