docx_rs/reader/
table_cell_property.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use xml::attribute::OwnedAttribute;
5use xml::reader::{EventReader, XmlEvent};
6
7use super::*;
8use crate::types::*;
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.first() {
38                                property = property.grid_span(usize::from_str(&a.value)?)
39                            }
40                        }
41                        XMLElement::TableVMerge => {
42                            if let Some(a) = attributes.first() {
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.first() {
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.first() {
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                        XMLElement::CellMargins => {
71                            if let Ok(margins) = CellMargins::read(r, &attributes) {
72                                property = property.margins(margins);
73                            }
74                        }
75                        _ => {}
76                    }
77                }
78                Ok(XmlEvent::EndElement { name, .. }) => {
79                    let e = XMLElement::from_str(&name.local_name).unwrap();
80                    if e == XMLElement::TableCellProperty {
81                        return Ok(property);
82                    }
83                }
84                Err(_) => return Err(ReaderError::XMLReadError),
85                _ => {}
86            }
87        }
88    }
89}