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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use html_escape::encode_quoted_attribute;

use crate::attribute::Attribute;
use crate::component::Component;

pub mod attribute;
pub mod component;
pub mod prelude;

#[derive(Clone, Default)]
pub struct VBody(Vec<VNode>);

impl VBody {
    pub fn new() -> Self {
        Self(Vec::new())
    }

    pub fn node(mut self, node: VNode) -> Self {
        self.0.push(node);

        self
    }

    pub fn merge(mut self, component: Self) -> Self {
        self.insert(component);

        self
    }

    pub fn insert(&mut self, component: Self) {
        for node in component.0.into_iter() {
            self.0.push(node);
        }
    }

    pub fn render(&self) -> String {
        let mut result = String::new();

        for node in self.0.iter() {
            result.push_str(node.render().as_str());
        }

        result
    }
}

#[derive(Clone)]
pub enum VNode {
    Element(VElement),
    Component(Component),
    Content(String),
}

impl VNode {
    pub fn element(element: impl Into<VElement>) -> Self {
        Self::Element(element.into())
    }

    pub fn content(text: impl Into<String>) -> Self {
        Self::Content(text.into())
    }

    pub fn component(component: Component) -> Self {
        Self::Component(component)
    }

    pub fn render(&self) -> String {
        match self {
            Self::Element(element) => element.render(),
            Self::Component(component) => component.render(),
            Self::Content(content) => encode_quoted_attribute(content.as_str()).into_owned(),
        }
    }
}

#[derive(Clone)]
pub struct VElement {
    pub name: String,
    pub attributes: Vec<Attribute>,
    pub children: Vec<VNode>,
    pub is_single: bool,
}

impl VElement {
    pub fn new(element: impl Into<String>) -> Self {
        Self {
            name: element.into(),
            attributes: Vec::new(),
            children: Vec::new(),
            is_single: false,
        }
    }

    pub fn attr(mut self, attr: impl Into<Attribute>) -> Self {
        self.attributes.push(attr.into());

        self
    }

    pub fn child(mut self, child: impl Into<VNode>) -> Self {
        self.children.push(child.into());

        self
    }

    pub fn merge(mut self, component: VBody) -> Self {
        for node in component.0.into_iter() {
            self.children.push(node);
        }

        self
    }

    pub fn node(self, child: impl Into<VNode>) -> Self {
        self.child(child)
    }

    pub fn single(mut self) -> Self {
        self.is_single = true;

        self
    }

    pub fn render(&self) -> String {
        let mut attrs = String::new();

        for attr in self.attributes.iter() {
            attrs.push_str(attr.render().as_str())
        }

        let mut content = String::new();

        for child in self.children.iter() {
            content.push_str(child.render().as_str());
        }

        match self.is_single {
            true => format!("<{0}{1}>", self.name, attrs),
            false => format!("<{0}{1}>{2}</{0}>", self.name, attrs, content),
        }
    }
}

// #[cfg(test)]
// mod tests {
//     use super::*;

//     #[test]
//     fn empty_div() {
//         let page = "<div></div>";
//         let element = Element::new("div");

//         assert_eq!(page, element.render())
//     }

//     #[test]
//     fn div_with_text() {
//         let page = "<div>text content</div>";
//         let element = Element::new("div").text("text content");
//         assert_eq!(page, element.render())
//     }

//     #[test]
//     fn div_with_text_and_attrs() {
//         let page = "<div id='test-id' class>text content</div>";
//         let element = Element::new("div")
//             .attr("id", "test-id")
//             .empty_attr("class")
//             .text("text content");

//         assert_eq!(page, element.render())
//     }

//     #[test]
//     fn div_with_child() {
//         let page = "<div class='container'><p>paragraph</p></div>";
//         let element = Element::new("div")
//             .attr("class", "container")
//             .children(Element::new("p").text("paragraph"));

//         assert_eq!(page, element.render())
//     }
// }