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

use super::*;

impl ElementReader for Paragraph {
	fn read<R: Read>(
		r: &mut EventReader<R>,
		attrs: &[OwnedAttribute],
	) -> Result<Self, ReaderError> {
		let mut p = Paragraph::new();
		if let Some(para_id) = read(attrs, "paraId") {
			p = p.id(para_id);
		}
		loop {
			let e = r.next();
			match e {
				Ok(XmlEvent::StartElement {
					attributes, name, ..
				}) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();

					match e {
						XMLElement::Run => {
							let run = Run::read(r, &attributes)?;
							p = p.add_run(run);
							continue;
						}
						XMLElement::Hyperlink => {
							let link = Hyperlink::read(r, &attributes)?;
							p = p.add_hyperlink(link);
							continue;
						}
						XMLElement::Insert => {
							let ins = Insert::read(r, &attributes)?;
							p = p.add_insert(ins);
							continue;
						}
						XMLElement::Delete => {
							let del = Delete::read(r, &attributes)?;
							p = p.add_delete(del);
							continue;
						}
						XMLElement::ParagraphProperty => {
							if let Ok(pr) = ParagraphProperty::read(r, &attributes) {
								p.has_numbering = pr.numbering_property.is_some();
								p.property = pr;
							}
							continue;
						}
						_ => {}
					}
				}
				Ok(XmlEvent::EndElement { name, .. }) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					if e == XMLElement::Paragraph {
						return Ok(p);
					}
				}
				Err(_) => return Err(ReaderError::XMLReadError),
				_ => {}
			}
		}
	}
}