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