docx_rs/reader/
table_cell_borders.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 TableCellBorders {
11    fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
12        let mut borders = TableCellBorders::with_empty();
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                    match e {
21                        XMLElement::Top => {
22                            let attr = read_border(&attributes)?;
23                            let mut border = TableCellBorder::new(TableCellBorderPosition::Top)
24                                .border_type(attr.border_type)
25                                .color(attr.color);
26                            if let Some(size) = attr.size {
27                                border = border.size(size as usize);
28                            };
29                            borders = borders.set(border);
30                            continue;
31                        }
32                        XMLElement::Right | XMLElement::End => {
33                            let attr = read_border(&attributes)?;
34                            let mut border = TableCellBorder::new(TableCellBorderPosition::Right)
35                                .border_type(attr.border_type)
36                                .color(attr.color);
37                            if let Some(size) = attr.size {
38                                border = border.size(size as usize);
39                            };
40                            borders = borders.set(border);
41                            continue;
42                        }
43                        XMLElement::Bottom => {
44                            let attr = read_border(&attributes)?;
45                            let mut border = TableCellBorder::new(TableCellBorderPosition::Bottom)
46                                .border_type(attr.border_type)
47                                .color(attr.color);
48                            if let Some(size) = attr.size {
49                                border = border.size(size as usize);
50                            };
51                            borders = borders.set(border);
52                            continue;
53                        }
54                        XMLElement::Left | XMLElement::Start => {
55                            let attr = read_border(&attributes)?;
56                            let mut border = TableCellBorder::new(TableCellBorderPosition::Left)
57                                .border_type(attr.border_type)
58                                .color(attr.color);
59                            if let Some(size) = attr.size {
60                                border = border.size(size as usize);
61                            };
62                            borders = borders.set(border);
63                            continue;
64                        }
65                        XMLElement::InsideH => {
66                            let attr = read_border(&attributes)?;
67                            let mut border = TableCellBorder::new(TableCellBorderPosition::InsideH)
68                                .border_type(attr.border_type)
69                                .color(attr.color);
70                            if let Some(size) = attr.size {
71                                border = border.size(size as usize);
72                            };
73                            borders = borders.set(border);
74                            continue;
75                        }
76                        XMLElement::InsideV => {
77                            let attr = read_border(&attributes)?;
78                            let mut border = TableCellBorder::new(TableCellBorderPosition::InsideV)
79                                .border_type(attr.border_type)
80                                .color(attr.color);
81                            if let Some(size) = attr.size {
82                                border = border.size(size as usize);
83                            };
84                            borders = borders.set(border);
85                            continue;
86                        }
87                        XMLElement::Tl2br => {
88                            let attr = read_border(&attributes)?;
89                            let mut border = TableCellBorder::new(TableCellBorderPosition::Tl2br)
90                                .border_type(attr.border_type)
91                                .color(attr.color);
92                            if let Some(size) = attr.size {
93                                border = border.size(size as usize);
94                            };
95                            borders = borders.set(border);
96                            continue;
97                        }
98                        XMLElement::Tr2bl => {
99                            let attr = read_border(&attributes)?;
100                            let mut border = TableCellBorder::new(TableCellBorderPosition::Tr2bl)
101                                .border_type(attr.border_type)
102                                .color(attr.color);
103                            if let Some(size) = attr.size {
104                                border = border.size(size as usize);
105                            };
106                            borders = borders.set(border);
107                            continue;
108                        }
109                        _ => {}
110                    }
111                }
112                Ok(XmlEvent::EndElement { name, .. }) => {
113                    let e = XMLElement::from_str(&name.local_name).unwrap();
114                    if e == XMLElement::TableCellBorders {
115                        return Ok(borders);
116                    }
117                }
118                Err(_) => return Err(ReaderError::XMLReadError),
119                _ => {}
120            }
121        }
122    }
123}