Skip to main content

docx_rs/reader/
cell_margins.rs

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