Skip to main content

docx_rs/reader/
table_cell_property.rs

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