docx_reader/reader/
table_cell_margins.rs

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