on ecc80c9 help sign in
dodrio/src/node.rs
94 lines 2.3 kB history raw
1use bumpalo::BumpAllocSafe;
3#[derive(Clone, Debug)]
4pub struct Node<'a, Attributes, Children> {
5 data: NodeData<'a>,
6 attributes: Attributes,
7 children: Children,
8}
10#[derive(Clone, Copy, Debug)]
11pub enum NodeData<'a> {
12 Element { tag_name: &'a str },
13 Text { text: &'a str },
16#[derive(Clone, Debug)]
17pub struct NodeRef<'a> {
18 pub(crate) data: NodeData<'a>,
19 pub(crate) attributes: &'a [Attribute<'a>],
20 pub(crate) children: &'a [NodeRef<'a>],
23#[derive(Clone, Debug)]
24pub struct Attribute<'a> {
25 pub name: &'a str,
26 pub value: &'a str,
29impl<'a, Attributes, Children> BumpAllocSafe for Node<'a, Attributes, Children>
30where
31 Attributes: BumpAllocSafe,
32 Children: BumpAllocSafe,
36impl<'a, Attributes, Children> From<&'a Node<'a, Attributes, Children>> for NodeRef<'a>
37where
38 Attributes: AsRef<[Attribute<'a>]>,
39 Children: AsRef<[NodeRef<'a>]>,
41 fn from(node: &'a Node<'a, Attributes, Children>) -> NodeRef<'a> {
42 NodeRef {
43 data: node.data,
44 attributes: node.attributes.as_ref(),
45 children: node.children.as_ref(),
46 }
47 }
50impl<'a, Attributes, Children> From<&'a mut Node<'a, Attributes, Children>> for NodeRef<'a>
51where
52 Attributes: AsRef<[Attribute<'a>]>,
53 Children: AsRef<[NodeRef<'a>]>,
55 fn from(node: &'a mut Node<'a, Attributes, Children>) -> NodeRef<'a> {
56 NodeRef {
57 data: node.data,
58 attributes: node.attributes.as_ref(),
59 children: node.children.as_ref(),
60 }
61 }
64impl<'a, Attributes, Children> Node<'a, Attributes, Children>
65where
66 Attributes: AsRef<[Attribute<'a>]>,
67 Children: AsRef<[NodeRef<'a>]>,
69 pub fn element(
70 tag_name: &'a str,
71 attributes: Attributes,
72 children: Children,
73 ) -> Node<'a, Attributes, Children> {
74 let data = NodeData::Element { tag_name };
75 Node {
76 data,
77 attributes,
78 children,
79 }
80 }
83impl<'a> Node<'a, [Attribute<'a>; 0], [NodeRef<'a>; 0]> {
84 pub fn text(text: &'a str) -> Node<'a, [Attribute<'a>; 0], [NodeRef<'a>; 0]> {
85 let data = NodeData::Text { text };
86 let attributes = [];
87 let children = [];
88 Node {
89 data,
90 attributes,
91 children,
92 }
93 }