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
use crate::{
attributes,
node::{Comment, Element, ElementType, Node},
tag::Tag,
text::Text,
};
use attributes::{Attribute, Value};
use std::collections::HashMap;
impl<'a> Node<'a> {
pub fn comment(text: &str) -> Self {
let comment: Comment = text.to_string().into();
comment.into()
}
pub fn text(text: &str) -> Self {
let text: Text = Text::create(text);
text.into()
}
}
impl<'a> Element<'a, Vec<Node<'a>>> {
pub fn push<N>(&mut self, node: N) -> ()
where
N: Into<Node<'a>>,
{
let node = node.into();
self.children.push(node);
}
}
impl<'a, T> Element<'a, T>
where
T: ElementType,
{
pub fn create(name: Tag<'a>) -> Self {
let attributes = HashMap::default();
let children = T::default();
let element = Element {
name,
attributes,
children,
};
element
}
pub fn add_bool_attribute(&mut self, key: Attribute<'a>) {
self.attributes.insert(key, None);
}
pub fn add_attribute(&mut self, key: Attribute<'a>, value: Value) {
self.attributes.insert(key, Some(value));
}
}