docx_reader/reader/
table_cell_property.rs

1use std::io::Read;
2use std::str::FromStr;
3use xml::attribute::OwnedAttribute;
4use xml::reader::{EventReader, XmlEvent};
5
6use crate::types::*;
7
8use super::*;
9
10impl ElementReader for TableCellProperty {
11	fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
12		let mut property = TableCellProperty::new();
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					ignore::ignore_element(e.clone(), XMLElement::TableCellPropertyChange, r);
21
22					match e {
23						XMLElement::TableCellWidth => {
24							let mut w = 0;
25							let mut width_type = WidthType::Auto;
26							for a in attributes {
27								let local_name = &a.name.local_name;
28								if local_name == "type" {
29									width_type = WidthType::from_str(&a.value)?;
30								} else if local_name == "w" {
31									w = usize::from_str(&a.value)?;
32								}
33							}
34							property = property.width(w, width_type);
35						}
36						XMLElement::TableGridSpan => {
37							if let Some(a) = &attributes.get(0) {
38								property = property.grid_span(usize::from_str(&a.value)?)
39							}
40						}
41						XMLElement::TableVMerge => {
42							if let Some(a) = &attributes.get(0) {
43								property = property.vertical_merge(VMergeType::from_str(&a.value)?);
44							} else {
45								// Treat as a continue without attribute
46								property = property.vertical_merge(VMergeType::Continue)
47							}
48						}
49						XMLElement::VAlign => {
50							if let Some(a) = &attributes.get(0) {
51								property = property.vertical_align(VAlignType::from_str(&a.value)?);
52							}
53						}
54						XMLElement::Shading => {
55							if let Ok(shd) = Shading::read(r, &attributes) {
56								property = property.shading(shd);
57							}
58						}
59						XMLElement::TextDirection => {
60							if let Some(a) = &attributes.get(0) {
61								if let Ok(v) = TextDirectionType::from_str(&a.value) {
62									property = property.text_direction(v);
63								}
64							}
65						}
66						XMLElement::TableCellBorders => {
67							let borders = TableCellBorders::read(r, &attributes)?;
68							property = property.set_borders(borders);
69						}
70						_ => {}
71					}
72				}
73				Ok(XmlEvent::EndElement { name, .. }) => {
74					let e = XMLElement::from_str(&name.local_name).unwrap();
75					if e == XMLElement::TableCellProperty {
76						return Ok(property);
77					}
78				}
79				Err(_) => return Err(ReaderError::XMLReadError),
80				_ => {}
81			}
82		}
83	}
84}