docx_reader/reader/
table_row.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
124
125
126
127
128
use std::io::Read;
use std::str::FromStr;
use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};

use crate::HeightRule;

use super::*;

impl ElementReader for TableRow {
	fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
		let mut cells = vec![];
		let mut grid_after = None;
		let mut width_after = None;
		let mut grid_before = None;
		let mut width_before = None;
		let mut row_height = None;
		let mut del = None;
		let mut ins = None;
		let mut height_rule = None;
		loop {
			let e = r.next();
			match e {
				Ok(XmlEvent::StartElement {
					attributes, name, ..
				}) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();

					ignore::ignore_element(e.clone(), XMLElement::TableRowPropertyChange, r);

					match e {
						XMLElement::TableCell => {
							cells.push(TableCell::read(r, &attributes)?);
							continue;
						}
						XMLElement::GridAfter => {
							if let Some(v) = read_val(&attributes) {
								grid_after = Some(u32::from_str(&v)?);
							}
						}
						XMLElement::WidthAfter => {
							if let Ok(v) = read_width(&attributes) {
								width_after = Some(v.0 as f32);
							}
						}
						XMLElement::GridBefore => {
							if let Some(v) = read_val(&attributes) {
								grid_before = Some(u32::from_str(&v)?);
							}
						}
						XMLElement::WidthBefore => {
							if let Ok(v) = read_width(&attributes) {
								width_before = Some(v.0 as f32);
							}
						}
						XMLElement::TableRowHeight => {
							if let Some(v) = read_val(&attributes) {
								let h = f32::from_str(&v);
								if let Ok(h) = h {
									row_height = Some(h);
								}
							}

							if let Some(v) = read(&attributes, "hRule") {
								let h = HeightRule::from_str(&v);
								if let Ok(h) = h {
									height_rule = Some(h);
								}
							}
						}
						XMLElement::Delete => {
							if let Ok(d) = Delete::read(r, &attributes) {
								del = Some(d);
							}
						}
						XMLElement::Insert => {
							if let Ok(i) = Insert::read(r, &attributes) {
								ins = Some(i);
							}
						}
						_ => {}
					}
				}
				Ok(XmlEvent::EndElement { name, .. }) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					if e == XMLElement::TableRow {
						let mut row = TableRow::new(cells);
						if let Some(grid_after) = grid_after {
							row = row.grid_after(grid_after);
						}

						if let Some(grid_before) = grid_before {
							row = row.grid_before(grid_before);
						}

						if let Some(row_height) = row_height {
							row = row.row_height(row_height);
						}

						if let Some(width_after) = width_after {
							row = row.width_after(width_after);
						}

						if let Some(width_before) = width_before {
							row = row.width_before(width_before);
						}

						if let Some(height_rule) = height_rule {
							row = row.height_rule(height_rule);
						}

						if let Some(del) = del {
							row = row.delete(del);
						}

						if let Some(ins) = ins {
							row = row.insert(ins);
						}

						return Ok(row);
					}
				}
				Err(_) => return Err(ReaderError::XMLReadError),
				_ => {}
			}
		}
	}
}