docx_reader/reader/
table_cell_borders.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::io::Read;
use std::str::FromStr;
use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};

use crate::types::*;

use super::*;

impl ElementReader for TableCellBorders {
	fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
		let mut borders = TableCellBorders::with_empty();
		loop {
			let e = r.next();
			match e {
				Ok(XmlEvent::StartElement {
					attributes, name, ..
				}) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					match e {
						XMLElement::Top => {
							let attr = read_border(&attributes)?;
							let mut border = TableCellBorder::new(TableCellBorderPosition::Top)
								.border_type(attr.border_type)
								.color(attr.color);
							if let Some(size) = attr.size {
								border = border.size(size as usize);
							};
							borders = borders.set(border);
							continue;
						}
						XMLElement::Right | XMLElement::End => {
							let attr = read_border(&attributes)?;
							let mut border = TableCellBorder::new(TableCellBorderPosition::Right)
								.border_type(attr.border_type)
								.color(attr.color);
							if let Some(size) = attr.size {
								border = border.size(size as usize);
							};
							borders = borders.set(border);
							continue;
						}
						XMLElement::Bottom => {
							let attr = read_border(&attributes)?;
							let mut border = TableCellBorder::new(TableCellBorderPosition::Bottom)
								.border_type(attr.border_type)
								.color(attr.color);
							if let Some(size) = attr.size {
								border = border.size(size as usize);
							};
							borders = borders.set(border);
							continue;
						}
						XMLElement::Left | XMLElement::Start => {
							let attr = read_border(&attributes)?;
							let mut border = TableCellBorder::new(TableCellBorderPosition::Left)
								.border_type(attr.border_type)
								.color(attr.color);
							if let Some(size) = attr.size {
								border = border.size(size as usize);
							};
							borders = borders.set(border);
							continue;
						}
						XMLElement::InsideH => {
							let attr = read_border(&attributes)?;
							let mut border = TableCellBorder::new(TableCellBorderPosition::InsideH)
								.border_type(attr.border_type)
								.color(attr.color);
							if let Some(size) = attr.size {
								border = border.size(size as usize);
							};
							borders = borders.set(border);
							continue;
						}
						XMLElement::InsideV => {
							let attr = read_border(&attributes)?;
							let mut border = TableCellBorder::new(TableCellBorderPosition::InsideV)
								.border_type(attr.border_type)
								.color(attr.color);
							if let Some(size) = attr.size {
								border = border.size(size as usize);
							};
							borders = borders.set(border);
							continue;
						}
						XMLElement::Tl2br => {
							let attr = read_border(&attributes)?;
							let mut border = TableCellBorder::new(TableCellBorderPosition::Tl2br)
								.border_type(attr.border_type)
								.color(attr.color);
							if let Some(size) = attr.size {
								border = border.size(size as usize);
							};
							borders = borders.set(border);
							continue;
						}
						XMLElement::Tr2bl => {
							let attr = read_border(&attributes)?;
							let mut border = TableCellBorder::new(TableCellBorderPosition::Tr2bl)
								.border_type(attr.border_type)
								.color(attr.color);
							if let Some(size) = attr.size {
								border = border.size(size as usize);
							};
							borders = borders.set(border);
							continue;
						}
						_ => {}
					}
				}
				Ok(XmlEvent::EndElement { name, .. }) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					if e == XMLElement::TableCellBorders {
						return Ok(borders);
					}
				}
				Err(_) => return Err(ReaderError::XMLReadError),
				_ => {}
			}
		}
	}
}