1use crate::jid::Jid;
2use crate::node::{Attrs, Node, NodeContent, NodeValue};
3
4#[derive(Debug, Default)]
5pub struct NodeBuilder {
6 tag: String,
7 attrs: Attrs,
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 jid_attr(mut self, key: impl Into<String>, jid: Jid) -> Self {
27 self.attrs.insert(key.into(), NodeValue::Jid(jid));
28 self
29 }
30
31 pub fn attrs<I, K, V>(mut self, attrs: I) -> Self
32 where
33 I: IntoIterator<Item = (K, V)>,
34 K: Into<String>,
35 V: Into<NodeValue>,
36 {
37 for (key, value) in attrs.into_iter() {
38 self.attrs.insert(key.into(), value.into());
39 }
40 self
41 }
42
43 pub fn children(mut self, children: impl IntoIterator<Item = Node>) -> Self {
44 self.content = Some(NodeContent::Nodes(children.into_iter().collect()));
45 self
46 }
47
48 pub fn bytes(mut self, bytes: impl Into<Vec<u8>>) -> Self {
49 self.content = Some(NodeContent::Bytes(bytes.into()));
50 self
51 }
52
53 pub fn string_content(mut self, s: impl Into<String>) -> Self {
54 self.content = Some(NodeContent::String(s.into()));
55 self
56 }
57
58 pub fn build(self) -> Node {
59 Node {
60 tag: self.tag,
61 attrs: self.attrs,
62 content: self.content,
63 }
64 }
65
66 pub fn apply_content(mut self, content: Option<NodeContent>) -> Self {
67 self.content = content;
68 self
69 }
70}