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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use html_escape::encode_quoted_attribute;

use crate::attribute::VAttribute;
use crate::component::VComponent;

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

pub type Element = VBody;

#[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(VComponent),
    Content(VContent),
}

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

    pub fn content(content: VContent) -> Self {
        Self::Content(content)
    }

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

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

#[derive(Clone)]
pub struct VContent {
    content: String,
    is_raw: bool,
}

impl VContent {
    pub fn new(content: impl Into<String>) -> Self {
        Self {
            content: content.into(),
            is_raw: false,
        }
    }

    pub fn raw(mut self) -> Self {
        self.is_raw = true;

        self
    }

    pub fn render(&self) -> String {
        match self.is_raw {
            true => self.content.to_string(),
            false => encode_quoted_attribute(self.content.as_str()).to_string(),
        }
    }
}

#[derive(Clone)]
pub struct VElement {
    pub name: String,
    pub attributes: Vec<VAttribute>,
    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<VAttribute>) -> 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())
//     }
// }