docx_reader/reader/
table_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
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 TableBorders {
	fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
		let mut borders = TableBorders::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 = TableBorder::new(TableBorderPosition::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 => {
							let attr = read_border(&attributes)?;
							let mut border = TableBorder::new(TableBorderPosition::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 = TableBorder::new(TableBorderPosition::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 => {
							let attr = read_border(&attributes)?;
							let mut border = TableBorder::new(TableBorderPosition::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 = TableBorder::new(TableBorderPosition::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 = TableBorder::new(TableBorderPosition::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;
						}
						_ => {}
					}
				}
				Ok(XmlEvent::EndElement { name, .. }) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					if e == XMLElement::TableBorders {
						return Ok(borders);
					}
				}
				Err(_) => return Err(ReaderError::XMLReadError),
				_ => {}
			}
		}
	}
}