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

use super::*;

impl ElementReader for TableCell {
	fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
		let mut cell = TableCell::new();
		loop {
			let e = r.next();

			match e {
				Ok(XmlEvent::StartElement {
					attributes, name, ..
				}) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					match e {
						XMLElement::Paragraph => {
							let p = Paragraph::read(r, &attributes)?;
							cell = cell.add_paragraph(p);
							continue;
						}
						XMLElement::StructuredDataTag => {
							if let Ok(tag) = StructuredDataTag::read(r, &attributes) {
								cell = cell.add_structured_data_tag(tag);
							}
							continue;
						}
						XMLElement::TableCellProperty => {
							if let Ok(p) = TableCellProperty::read(r, &attributes) {
								cell.property = p;
							}
							continue;
						}
						XMLElement::Table => {
							if let Ok(table) = Table::read(r, &attributes) {
								cell = cell.add_table(table)
							}
						}
						_ => {}
					}
				}
				Ok(XmlEvent::EndElement { name, .. }) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					if e == XMLElement::TableCell {
						return Ok(cell);
					}
				}
				Err(_) => return Err(ReaderError::XMLReadError),
				_ => {}
			}
		}
	}
}