Skip to main content

docx_rs/reader/
div.rs

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