Skip to main content

docx_rs/reader/
table_property.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use crate::TableAlignmentType;
5
6use super::*;
7
8// TODO: layout: Option<TableLayout>,
9impl ElementReader for TableProperty {
10    fn read<R: Read>(
11        r: &mut EventReader<R>,
12        _attrs: &[OwnedAttribute],
13    ) -> Result<Self, ReaderError> {
14        let mut tp = TableProperty::new();
15        tp = tp.set_borders(TableBorders::with_empty());
16        loop {
17            let e = r.next();
18            match e {
19                Ok(XmlEvent::StartElement {
20                    attributes, name, ..
21                }) => {
22                    let e = XMLElement::from_str(&name.local_name).unwrap();
23                    match e {
24                        XMLElement::TableBorders => {
25                            if let Ok(borders) = TableBorders::read(r, &attributes) {
26                                tp = tp.set_borders(borders);
27                            }
28                        }
29                        XMLElement::TableCellMargin => {
30                            if let Ok(margins) = TableCellMargins::read(r, &attributes) {
31                                tp = tp.set_margins(margins);
32                            }
33                        }
34                        XMLElement::TableWidth => {
35                            if let Ok((w, width_type)) = read_width(&attributes) {
36                                tp = tp.width(w as usize, width_type);
37                            }
38                        }
39                        XMLElement::Justification => {
40                            if let Ok(v) = TableAlignmentType::from_str(&attributes[0].value) {
41                                tp = tp.align(v);
42                            }
43                        }
44                        XMLElement::TableIndent => {
45                            if let Ok((w, _)) = read_width(&attributes) {
46                                if w != 0 {
47                                    tp = tp.indent(w as i32);
48                                }
49                            }
50                        }
51                        XMLElement::TableStyle => {
52                            if let Some(s) = read_val(&attributes) {
53                                tp = tp.style(s);
54                            }
55                        }
56                        XMLElement::TablePositionProperty => {
57                            if let Ok(p) = TablePositionProperty::read(r, &attributes) {
58                                tp = tp.position(p);
59                            }
60                        }
61                        _ => {}
62                    }
63                }
64                Ok(XmlEvent::EndElement { name, .. }) => {
65                    let e = XMLElement::from_str(&name.local_name).unwrap();
66                    if e == XMLElement::TableProperty {
67                        return Ok(tp);
68                    }
69                }
70                Err(_) => return Err(ReaderError::XMLReadError),
71                _ => {}
72            }
73        }
74    }
75}