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
use crate::ast::entry::Entry;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Object {
pub class: String,
pub name: Option<String>,
pub children: Vec<Entry>
}
impl Object {
pub fn entry(class: String, name: Option<String>, children: Vec<Entry>) -> Entry {
Entry::Object(Self { class, name, children })
}
pub fn add_child(&mut self, child: Entry) {
self.children.push(child);
}
pub fn dbg(&self) -> String {
format!(
"Object {{\n class: {},\n name: {:?},\n children: [\n{} ]\n}}",
self.class,
self.name,
(&self.children).into_iter().map(|child| {
let text = child.dbg().lines()
.map(|line| String::from(" ") + line + "\n")
.collect::<String>();
text.trim_end().to_string() + ",\n"
}).collect::<String>()
)
}
pub fn get_xml(&self) -> String {
let class = self.class.replace(".", "");
let beginning = match &self.name {
Some(name) => format!("<object class=\"{}\" id=\"{}\">", class, name),
None => format!("<object class=\"{}\">", class)
};
let mut properties = String::new();
let mut children = String::new();
for child in &self.children {
if let Entry::Property(property) = child {
properties += &property.get_xml();
}
else {
children += &format!("<child>{}</child>", child.get_xml());
}
}
format!("{}{}{}</object>", beginning, properties, children)
}
}