1use std::io::Read;
2use std::str::FromStr;
3
4use xml::attribute::OwnedAttribute;
5use xml::reader::{EventReader, XmlEvent};
6
7use super::*;
8
9impl ElementReader for Div {
10 fn read<R: Read>(
11 r: &mut EventReader<R>,
12 attrs: &[OwnedAttribute],
13 ) -> Result<Self, ReaderError> {
14 let id = read_id(attrs).unwrap_or_default();
15 let mut div = Div::new(id);
16 loop {
17 let e = r.next();
18 match e {
19 Ok(XmlEvent::StartElement {
20 attributes, name, ..
21 }) => {
22 let e = XMLElement::from_str(&name.local_name).unwrap();
23 match e {
24 XMLElement::MarginLeft => {
25 if let Some(val) = read_val(&attributes) {
26 if let Ok(val) = f32::from_str(&val) {
27 div = div.margin_left(val as usize);
28 }
29 }
30 }
31 XMLElement::MarginRight => {
32 if let Some(val) = read_val(&attributes) {
33 if let Ok(val) = f32::from_str(&val) {
34 div = div.margin_right(val as usize);
35 }
36 }
37 }
38 XMLElement::MarginTop => {
39 if let Some(val) = read_val(&attributes) {
40 if let Ok(val) = f32::from_str(&val) {
41 div = div.margin_top(val as usize);
42 }
43 }
44 }
45 XMLElement::MarginBottom => {
46 if let Some(val) = read_val(&attributes) {
47 if let Ok(val) = f32::from_str(&val) {
48 div = div.margin_bottom(val as usize);
49 }
50 }
51 }
52 XMLElement::DivsChild => loop {
53 let e = r.next();
54 match e {
55 Ok(XmlEvent::StartElement {
56 attributes, name, ..
57 }) => {
58 let e = XMLElement::from_str(&name.local_name).unwrap();
59 if let XMLElement::Div = e {
60 if let Ok(c) = Div::read(r, &attributes) {
61 div = div.add_child(c)
62 }
63 }
64 }
65 Ok(XmlEvent::EndElement { name, .. }) => {
66 let e = XMLElement::from_str(&name.local_name).unwrap();
67 if let XMLElement::DivsChild = e {
68 break;
69 }
70 }
71 Err(_) => return Err(ReaderError::XMLReadError),
72 _ => {}
73 }
74 },
75 _ => {}
76 }
77 }
78 Ok(XmlEvent::EndElement { name, .. }) => {
79 let e = XMLElement::from_str(&name.local_name).unwrap();
80 if let XMLElement::Div = e {
81 return Ok(div);
82 }
83 }
84 Err(_) => return Err(ReaderError::XMLReadError),
85 _ => {}
86 }
87 }
88 }
89}