docx_reader/reader/
paragraph_property.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
129
130
131
132
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 ParagraphProperty {
	fn read<R: Read>(
		r: &mut EventReader<R>,
		attrs: &[OwnedAttribute],
	) -> Result<Self, ReaderError> {
		let mut p = ParagraphProperty::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::Indent => {
							let (start, end, special, start_chars, hanging_chars, first_line_chars) =
								read_indent(&attributes)?;
							p = p.indent(start, special, end, start_chars);

							if let Some(chars) = hanging_chars {
								p = p.hanging_chars(chars);
							}
							if let Some(chars) = first_line_chars {
								p = p.first_line_chars(chars);
							}
							continue;
						}
						XMLElement::Spacing => {
							if let Ok(spacing) =
								attributes::line_spacing::read_line_spacing(&attributes)
							{
								p = p.line_spacing(spacing);
							}
							continue;
						}
						XMLElement::Justification => {
							if let Ok(v) = AlignmentType::from_str(&attributes[0].value) {
								p = p.align(v);
							}
							continue;
						}
						XMLElement::ParagraphStyle => {
							p = p.style(&attributes[0].value);
							continue;
						}
						XMLElement::RunProperty => {
							if let Ok(run_pr) = RunProperty::read(r, attrs) {
								p.run_property = run_pr;
							}
							continue;
						}
						XMLElement::DivId => {
							if let Some(val) = read_val(&attributes) {
								p.div_id = Some(val)
							}
							continue;
						}
						XMLElement::NumberingProperty => {
							if let Ok(num_pr) = NumberingProperty::read(r, attrs) {
								p = p.numbering_property(num_pr);
							}
							continue;
						}
						XMLElement::OutlineLvl => {
							if let Some(val) = read_val(&attributes) {
								if let Ok(val) = usize::from_str(&val) {
									p = p.outline_lvl(val);
								}
							}
							continue;
						}
						XMLElement::KeepNext => {
							if read_bool(&attributes) {
								p.keep_next = Some(true);
							}
						}
						XMLElement::KeepLines => {
							if read_bool(&attributes) {
								p.keep_lines = Some(true);
							}
						}
						XMLElement::PageBreakBefore => {
							if read_bool(&attributes) {
								p.page_break_before = Some(true);
							}
						}
						XMLElement::WidowControl => {
							if read_bool(&attributes) {
								p.widow_control = Some(true);
							}
						}
						XMLElement::ParagraphPropertyChange => {
							if let Ok(ppr_change) = ParagraphPropertyChange::read(r, &attributes) {
								p.paragraph_property_change = Some(ppr_change);
							}
						}
						XMLElement::SectionProperty => {
							if let Ok(sp) = SectionProperty::read(r, &attributes) {
								p.section_property = Some(sp);
							}
						}
						XMLElement::Tabs => {
							if let Ok(tabs) = Tabs::read(r, &attributes) {
								for t in tabs.tabs {
									p = p.add_tab(t);
								}
							}
						}
						_ => {}
					}
				}
				Ok(XmlEvent::EndElement { name, .. }) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					if e == XMLElement::ParagraphProperty {
						return Ok(p);
					}
				}
				Err(_) => return Err(ReaderError::XMLReadError),
				_ => {}
			}
		}
	}
}