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
use crate::react_bindings;
use js_sys::Array;
use wasm_bindgen::{JsCast, JsValue};
#[derive(Debug, Clone)]
pub struct VNode(pub(crate) JsValue);
impl VNode {
pub fn empty() -> VNode {
VNode(JsValue::null())
}
}
impl Default for VNode {
fn default() -> Self {
Self::empty()
}
}
impl AsRef<JsValue> for VNode {
fn as_ref(&self) -> &JsValue {
&self.0
}
}
impl From<VNode> for JsValue {
fn from(value: VNode) -> Self {
value.0
}
}
impl<T: Into<VNode>> From<Option<T>> for VNode {
fn from(value: Option<T>) -> Self {
value.map(|value| value.into()).unwrap_or_default()
}
}
impl<'a> From<&'a str> for VNode {
fn from(value: &'a str) -> Self {
VNode(value.into())
}
}
impl<'a> From<&'a String> for VNode {
fn from(value: &'a String) -> Self {
VNode(value.into())
}
}
macro_rules! impl_into_vnode {
{ $( $T:ty ),*$( , )? } => {
$(
impl From<$T> for VNode {
fn from(value: $T) -> Self {
VNode(value.into())
}
}
)*
};
}
impl_into_vnode! {
String,
f32, f64,
i8, i16, i32, i64, i128, isize,
u8, u16, u32, u64, u128, usize,
}
#[derive(Debug, Default, Clone)]
pub struct VNodeList {
empty: bool,
arr: Array,
}
impl VNodeList {
pub fn new() -> Self {
Self {
empty: true,
arr: Array::new(),
}
}
pub fn empty(&self) -> bool {
self.empty
}
pub fn push(&mut self, node: &VNode) {
self.empty = false;
self.arr.push(node.as_ref());
}
}
impl AsRef<JsValue> for VNodeList {
fn as_ref(&self) -> &JsValue {
&self.arr
}
}
impl From<VNodeList> for JsValue {
fn from(value: VNodeList) -> Self {
value.arr.into()
}
}
impl TryFrom<JsValue> for VNodeList {
type Error = JsValue;
fn try_from(value: JsValue) -> Result<Self, Self::Error> {
let arr = if value.is_instance_of::<Array>() {
value.dyn_into::<Array>()?
} else {
react_bindings::children_to_array(&value)?
};
Ok(VNodeList {
empty: arr.length() == 0,
arr,
})
}
}
impl Extend<VNode> for VNodeList {
fn extend<T: IntoIterator<Item = VNode>>(&mut self, iter: T) {
for node in iter.into_iter() {
self.push(&node);
}
}
}
impl FromIterator<VNode> for VNodeList {
fn from_iter<T: IntoIterator<Item = VNode>>(iter: T) -> Self {
let mut result = Self::new();
for node in iter.into_iter() {
result.push(&node);
}
result
}
}