diff options
author | tom barrett <spalf0@gmail.com> | 2019-06-27 10:00:42 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-06-27 10:00:42 -0500 |
commit | a76efb8444af9d4a0ee558e72903c67bc2cff5d3 (patch) | |
tree | b93849385e23e51466658502fe0a8a3064405ba5 /src/xmlelements.rs | |
parent | 93d0a0eb6a5c35ad8b82f368c5251d9690c508f9 (diff) | |
parent | ee8d055be8326eb1561900bcca6acd9e38071f4a (diff) |
Merge branch 'master' into feature/dude
Diffstat (limited to 'src/xmlelements.rs')
-rw-r--r-- | src/xmlelements.rs | 113 |
1 files changed, 109 insertions, 4 deletions
diff --git a/src/xmlelements.rs b/src/xmlelements.rs index 86d7c77..ea880c8 100644 --- a/src/xmlelements.rs +++ b/src/xmlelements.rs @@ -2,9 +2,39 @@ use ggez::filesystem::File; use std::io::BufReader; use xml::reader::{ EventReader, - XmlEvent::{self, StartElement}, + 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>, } @@ -33,20 +63,95 @@ impl XMLElements { .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<usize, ()> { + ) -> 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 - .parse() - .unwrap()) + .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(()) } |