1use crate::node::{Node, NodeContent};
2use std::collections::HashMap;
3
4#[derive(Debug, Default)]
5pub struct NodeBuilder {
6 tag: String,
7 attrs: HashMap<String, String>,
8 content: Option<NodeContent>,
9}
10
11impl NodeBuilder {
12 pub fn new(tag: impl Into<String>) -> Self {
13 Self {
14 tag: tag.into(),
15 ..Default::default()
16 }
17 }
18
19 pub fn attr(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
20 self.attrs.insert(key.into(), value.into());
21 self
22 }
23
24 pub fn attrs<I, K, V>(mut self, attrs: I) -> Self
25 where
26 I: IntoIterator<Item = (K, V)>,
27 K: Into<String>,
28 V: Into<String>,
29 {
30 for (key, value) in attrs.into_iter() {
31 self.attrs.insert(key.into(), value.into());
32 }
33 self
34 }
35
36 pub fn children(mut self, children: impl IntoIterator<Item = Node>) -> Self {
37 self.content = Some(NodeContent::Nodes(children.into_iter().collect()));
38 self
39 }
40
41 pub fn bytes(mut self, bytes: impl Into<Vec<u8>>) -> Self {
42 self.content = Some(NodeContent::Bytes(bytes.into()));
43 self
44 }
45
46 pub fn string_content(mut self, s: impl Into<String>) -> Self {
47 self.content = Some(NodeContent::String(s.into()));
48 self
49 }
50
51 pub fn build(self) -> Node {
52 Node {
53 tag: self.tag,
54 attrs: self.attrs,
55 content: self.content,
56 }
57 }
58
59 pub fn apply_content(mut self, content: Option<NodeContent>) -> Self {
60 self.content = content;
61 self
62 }
63}