gtk_ui_builder/ast/entries/
object.rs

1use crate::ast::entry::Entry;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct Object {
5    pub class: String,
6    pub name: Option<String>,
7    pub children: Vec<Entry>
8}
9
10impl Object {
11    pub fn entry(class: String, name: Option<String>, children: Vec<Entry>) -> Entry {
12        Entry::Object(Self { class, name, children })
13    }
14
15    pub fn add_child(&mut self, child: Entry) {
16        self.children.push(child);
17    }
18
19    /// Get pretty string description of this entry
20    pub fn dbg(&self) -> String {
21        format!(
22            "Object {{\n  class: {},\n  name: {:?},\n  children: [\n{}  ]\n}}",
23            self.class,
24            self.name,
25            (&self.children).into_iter().map(|child| {
26                let text = child.dbg().lines()
27                    .map(|line| String::from("      ") + line + "\n")
28                    .collect::<String>();
29
30                text.trim_end().to_string() + ",\n"
31            }).collect::<String>()
32        )
33    }
34
35    /// Get XML description of this entry
36    pub fn get_xml(&self) -> String {
37        let class = self.class.replace(".", "");
38
39        let beginning = match &self.name {
40            Some(name) => format!("<object class=\"{}\" id=\"{}\">", class, name),
41            None => format!("<object class=\"{}\">", class)
42        };
43
44        let mut signals = String::new();
45        let mut properties = String::new();
46        let mut children = String::new();
47
48        for child in &self.children {
49            #[cfg(feature = "rhai-events")]
50            if let Entry::RhaiEvent(event) = child {
51                signals += &event.get_xml();
52
53                continue;
54            }
55            
56            if let Entry::Property(property) = child {
57                properties += &property.get_xml();
58            }
59
60            else {
61                children += &format!("<child>{}</child>", child.get_xml());
62            }
63        }
64
65        format!("{}{}{}{}</object>", beginning, signals, properties, children)
66    }
67}