用rust写前端(二)

rust 过程宏使用

设计

html虚拟节点的枚举类型

1
2
3
4
5
6
7
8
9
10
11
12
pub enum HtmlVNode {
Element(Box<HtmlElement>), // <div>1</div>
HtmlString(Box<HtmlString>), // "asdf"
Block(Box<HtmlBlock>), // {} 作用域
Empty, // 空
}
pub struct HtmlElement {
name: String,
props: ElementProps,
children: HtmlElementChildren,
}
pub struct HtmlElementChildren(Vec<HtmlVNode>);
1
2
let a = react!(<div>asf</div>);
dbg!(react!(<div>123<div>qwr{ "asdf" }{{a}}</div></div>));

=>

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
Node(
VNode {
name: "div",
children: [
Text(
"123",
),
Node(
VNode {
name: "div",
children: [
Text(
"qwr",
),
Text(
"asdf",
),
Node(
VNode {
name: "div",
children: [
Text(
"asf",
),
],
},
),
],
},
),
],
},
)