docx_rs/reader/
cell_margins.rs

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 CellMargins {
10    fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
11        let mut margins = CellMargins::default();
12        loop {
13            let e = r.next();
14            match e {
15                Ok(XmlEvent::StartElement {
16                    attributes, name, ..
17                }) => {
18                    let e = XMLElement::from_str(&name.local_name).unwrap();
19                    match e {
20                        XMLElement::Top => {
21                            if let Ok(width) = read_width(&attributes) {
22                                margins = margins.margin_top(width.0 as usize, width.1)
23                            }
24                        }
25                        XMLElement::Right => {
26                            if let Ok(width) = read_width(&attributes) {
27                                margins = margins.margin_right(width.0 as usize, width.1)
28                            }
29                        }
30                        XMLElement::Bottom => {
31                            if let Ok(width) = read_width(&attributes) {
32                                margins = margins.margin_bottom(width.0 as usize, width.1)
33                            }
34                        }
35                        XMLElement::Left => {
36                            if let Ok(width) = read_width(&attributes) {
37                                margins = margins.margin_left(width.0 as usize, width.1)
38                            }
39                        }
40                        _ => {}
41                    }
42                }
43                Ok(XmlEvent::EndElement { name, .. }) => {
44                    let e = XMLElement::from_str(&name.local_name).unwrap();
45                    if e == XMLElement::CellMargins {
46                        return Ok(margins);
47                    }
48                }
49                Err(_) => return Err(ReaderError::XMLReadError),
50                _ => {}
51            }
52        }
53    }
54}