summaryrefslogtreecommitdiff
path: root/src/xmlelements.rs
blob: ea880c825c174ca0a8e955228ef47677cb1a63a7 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use ggez::filesystem::File;
use std::io::BufReader;
use xml::reader::{
    EventReader,
    XmlEvent::{self, EndElement, StartElement},
};

#[derive(Debug, Clone)]
pub struct Property {
    pub entity: String,
    pub keyframe: usize,
    pub delay: usize,
}

impl Property {
    pub fn new(property_elements: Vec<XmlEvent>) -> Property {
        let entity = XMLElements::get_attribute_value(&property_elements, "entity")
            .unwrap()
            .parse()
            .unwrap();
        let keyframe = XMLElements::get_attribute_value(&property_elements, "keyframe")
            .unwrap()
            .parse()
            .unwrap();
        let delay = XMLElements::get_attribute_value(&property_elements, "delay")
            .unwrap()
            .parse()
            .unwrap();

        Property {
            entity,
            keyframe,
            delay,
        }
    }
}

pub struct XMLElements {
    pub events: Vec<XmlEvent>,
}

impl XMLElements {
    pub fn new(file: File) -> XMLElements {
        XMLElements {
            events: EventReader::new(BufReader::new(file))
                .into_iter()
                .map(Result::unwrap)
                .collect(),
        }
    }

    pub fn get_element(&self, element_name: &str) -> XmlEvent {
        self.events
            .clone()
            .into_iter()
            .find(|e| {
                if let StartElement { name, .. } = e {
                    name.local_name == element_name
                } else {
                    false
                }
            })
            .unwrap()
    }

    pub fn get_children(&self, element: &XmlEvent, children_name: &str) -> Vec<XmlEvent> {
        let start_index = self.events.iter().position(|e| e == element).unwrap();

        let element_name = if let StartElement { name, .. } = element {
            &name.local_name
        } else {
            ""
        };

        let end_index = self.events[start_index..]
            .iter()
            .position(|e| {
                if let EndElement { name } = e {
                    element_name == name.local_name
                } else {
                    false
                }
            })
            .unwrap()
            + start_index;

        self.events[start_index..end_index]
            .iter()
            .cloned()
            .filter(|e| {
                if let StartElement { name, .. } = e {
                    name.local_name == children_name
                } else {
                    false
                }
            })
            .collect()
    }

    pub fn get_elements(&self, element_name: &str) -> Vec<XmlEvent> {
        self.events
            .clone()
            .into_iter()
            .filter(|e| {
                if let StartElement { name, .. } = e {
                    name.local_name == element_name
                } else {
                    false
                }
            })
            .collect()
    }

    pub fn get_element_attribute(
        &self,
        element_name: &str,
        attribute_name: &str,
    ) -> Result<String, ()> {
        let element = self.get_element(element_name);
        XMLElements::get_attribute(&element, attribute_name)
    }

    pub fn get_attribute(element: &XmlEvent, attribute_name: &str) -> Result<String, ()> {
        if let StartElement { attributes, .. } = element {
            Ok(attributes
                .iter()
                .find(|a| a.name.local_name == attribute_name)
                .unwrap()
                .value
                .clone())
        } else {
            Err(())
        }
    }

    pub fn get_attribute_value(elements: &[XmlEvent], attribute_name: &str) -> Result<String, ()> {
        let element = elements
            .iter()
            .find(|e| {
                if let StartElement { attributes, .. } = e {
                    attributes.iter().any(|a| a.value == attribute_name)
                } else {
                    false
                }
            })
            .unwrap();

        if let StartElement { attributes, .. } = element {
            Ok(attributes
                .iter()
                .find(|a| a.name.local_name == "value")
                .unwrap()
                .value
                .clone())
        } else {
            Err(())
        }
    }
}