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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::{
CollapsedHtmlToken,
CollapsedNode,
};
use custom_derive::custom_derive;
use enum_derive::{
enum_derive_util,
EnumFromInner,
};
pub type Attributes = std::collections::HashMap<String, String>;
custom_derive! {
#[derive(Debug, Clone, EnumFromInner, Eq, PartialEq)]
pub enum Node {
Dom(HtmlToken),
Text(String),
Vec(Vec<Node>),
Comment(Option<String>),
}
}
pub trait AsInnerHtml {
fn as_inner_html(&self) -> String;
}
impl AsInnerHtml for Vec<CollapsedNode> {
fn as_inner_html(&self) -> String {
self.iter().map(|node| node.as_inner_html()).collect()
}
}
impl AsInnerHtml for CollapsedNode {
fn as_inner_html(&self) -> String {
match self {
CollapsedNode::Dom(token) => token.as_inner_html(),
CollapsedNode::Text(s) => s.to_string(),
CollapsedNode::Comment(str_opt) => match str_opt {
Some(s) => format!("<!-- {} -->", s),
None => "<!-- -->".into(),
},
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct HtmlToken {
pub node_type: String,
pub children: Vec<Node>,
pub attributes: Attributes,
}
fn format_attributes(attr: &Attributes) -> String {
attr.iter().fold("".to_string(), |accum, (key, val)| {
if val != "" {
format!("{} {}=\"{}\"", accum, key, val)
} else {
format!("{} {}", accum, key)
}
})
}
fn format_path(path: &Path) -> String {
if path.len() > 0 {
let path_str = path.iter().fold("".to_string(), |accum, path_segment| {
format!("{}{},", accum, path_segment)
});
path_str.chars().take(path_str.len() - 1).collect()
} else {
"".to_string()
}
}
use lazy_static::lazy_static;
lazy_static! {
static ref VOID_TAGS: std::collections::HashSet<String> = {
let mut void_tags = std::collections::HashSet::new();
void_tags.insert("area".to_string());
void_tags.insert("base".to_string());
void_tags.insert("br".to_string());
void_tags.insert("col".to_string());
void_tags.insert("command".to_string());
void_tags.insert("embed".to_string());
void_tags.insert("hr".to_string());
void_tags.insert("img".to_string());
void_tags.insert("input".to_string());
void_tags.insert("keygen".to_string());
void_tags.insert("link".to_string());
void_tags.insert("meta".to_string());
void_tags.insert("param".to_string());
void_tags.insert("source".to_string());
void_tags.insert("track".to_string());
void_tags.insert("wbr".to_string());
void_tags
};
}
impl AsInnerHtml for CollapsedHtmlToken {
fn as_inner_html(&self) -> String {
let path_string = format!(" data-smithy-path=\"{}\"", format_path(&self.path));
let attributes_string = if self.attributes.len() > 0 {
format!(" {}", format_attributes(&self.attributes))
} else {
"".to_string()
};
if !VOID_TAGS.contains(&self.node_type) {
let child_html = self
.children
.iter()
.map(|node| node.as_inner_html())
.collect::<Vec<String>>()
.join("");
format!(
"<{}{}{}>{}</{}>",
self.node_type, attributes_string, path_string, child_html, self.node_type
)
} else {
format!("<{}{}{} />", self.node_type, attributes_string, path_string)
}
}
}
pub type Path = [usize];
pub enum Phase<'a> {
Rendering,
PostRendering,
UiEventHandling((&'a crate::UiEvent, &'a Path)),
WindowEventHandling(&'a crate::WindowEvent),
RefAssignment(Vec<usize>),
}
pub type EventHandled = bool;
#[derive(Debug)]
pub enum PhaseResult {
Rendering(Node),
PostRendering,
UiEventHandling(EventHandled),
WindowEventHandling(EventHandled),
RefAssignment,
}
impl PhaseResult {
pub fn unwrap_node(self) -> Node {
match self {
PhaseResult::Rendering(node) => node,
_ => panic!("unwrap_node called on PhaseResult that was not of variant Rendering"),
}
}
pub fn unwrap_event_handled(self) -> EventHandled {
match self {
PhaseResult::UiEventHandling(event_handled) => event_handled,
PhaseResult::WindowEventHandling(event_handled) => event_handled,
_ => {
panic!("unwrap_event_handled called on PhaseResult that was not of variant UiEventHandling or WindowEventHandling")
},
}
}
}
pub struct SmithyComponent<'a>(pub Box<FnMut(Phase) -> PhaseResult + 'a>);
pub trait Component {
fn render(&mut self) -> Node;
fn handle_post_render(&mut self) {}
fn handle_ref_assignment(&mut self, _path_so_far: Vec<usize>) {}
fn handle_ui_event(&mut self, _event: &crate::UiEvent, _path: &Path) -> EventHandled {
false
}
fn handle_window_event(&mut self, _event: &crate::WindowEvent) -> EventHandled {
false
}
}
impl<'a> Component for SmithyComponent<'a> {
fn handle_ui_event(&mut self, event: &crate::UiEvent, path: &Path) -> EventHandled {
self.0(Phase::UiEventHandling((event, path))).unwrap_event_handled()
}
fn handle_window_event(&mut self, event: &crate::WindowEvent) -> EventHandled {
self.0(Phase::WindowEventHandling(event)).unwrap_event_handled()
}
fn render(&mut self) -> Node {
self.0(Phase::Rendering).unwrap_node()
}
fn handle_post_render(&mut self) {
self.0(Phase::PostRendering);
}
fn handle_ref_assignment(&mut self, path_so_far: Vec<usize>) {
self.0(Phase::RefAssignment(path_so_far));
}
}