docx_reader/reader/
paragraph_property.rs

1use std::io::Read;
2use std::str::FromStr;
3use xml::attribute::OwnedAttribute;
4use xml::reader::{EventReader, XmlEvent};
5
6use crate::types::*;
7
8use super::*;
9
10impl ElementReader for ParagraphProperty {
11	fn read<R: Read>(
12		r: &mut EventReader<R>,
13		attrs: &[OwnedAttribute],
14	) -> Result<Self, ReaderError> {
15		let mut p = ParagraphProperty::new();
16		loop {
17			let e = r.next();
18			match e {
19				Ok(XmlEvent::StartElement {
20					attributes, name, ..
21				}) => {
22					let e = XMLElement::from_str(&name.local_name).unwrap();
23					match e {
24						XMLElement::Indent => {
25							let (start, end, special, start_chars, hanging_chars, first_line_chars) =
26								read_indent(&attributes)?;
27							p = p.indent(start, special, end, start_chars);
28
29							if let Some(chars) = hanging_chars {
30								p = p.hanging_chars(chars);
31							}
32							if let Some(chars) = first_line_chars {
33								p = p.first_line_chars(chars);
34							}
35							continue;
36						}
37						XMLElement::Spacing => {
38							if let Ok(spacing) =
39								attributes::line_spacing::read_line_spacing(&attributes)
40							{
41								p = p.line_spacing(spacing);
42							}
43							continue;
44						}
45						XMLElement::Justification => {
46							if let Ok(v) = AlignmentType::from_str(&attributes[0].value) {
47								p = p.align(v);
48							}
49							continue;
50						}
51						XMLElement::ParagraphStyle => {
52							p = p.style(&attributes[0].value);
53							continue;
54						}
55						XMLElement::RunProperty => {
56							if let Ok(run_pr) = RunProperty::read(r, attrs) {
57								p.run_property = run_pr;
58							}
59							continue;
60						}
61						XMLElement::DivId => {
62							if let Some(val) = read_val(&attributes) {
63								p.div_id = Some(val)
64							}
65							continue;
66						}
67						XMLElement::NumberingProperty => {
68							if let Ok(num_pr) = NumberingProperty::read(r, attrs) {
69								p = p.numbering_property(num_pr);
70							}
71							continue;
72						}
73						XMLElement::OutlineLvl => {
74							if let Some(val) = read_val(&attributes) {
75								if let Ok(val) = usize::from_str(&val) {
76									p = p.outline_lvl(val);
77								}
78							}
79							continue;
80						}
81						XMLElement::KeepNext => {
82							if read_bool(&attributes) {
83								p.keep_next = Some(true);
84							}
85						}
86						XMLElement::KeepLines => {
87							if read_bool(&attributes) {
88								p.keep_lines = Some(true);
89							}
90						}
91						XMLElement::PageBreakBefore => {
92							if read_bool(&attributes) {
93								p.page_break_before = Some(true);
94							}
95						}
96						XMLElement::WidowControl => {
97							if read_bool(&attributes) {
98								p.widow_control = Some(true);
99							}
100						}
101						XMLElement::ParagraphPropertyChange => {
102							if let Ok(ppr_change) = ParagraphPropertyChange::read(r, &attributes) {
103								p.paragraph_property_change = Some(ppr_change);
104							}
105						}
106						XMLElement::SectionProperty => {
107							if let Ok(sp) = SectionProperty::read(r, &attributes) {
108								p.section_property = Some(sp);
109							}
110						}
111						XMLElement::Tabs => {
112							if let Ok(tabs) = Tabs::read(r, &attributes) {
113								for t in tabs.tabs {
114									p = p.add_tab(t);
115								}
116							}
117						}
118						_ => {}
119					}
120				}
121				Ok(XmlEvent::EndElement { name, .. }) => {
122					let e = XMLElement::from_str(&name.local_name).unwrap();
123					if e == XMLElement::ParagraphProperty {
124						return Ok(p);
125					}
126				}
127				Err(_) => return Err(ReaderError::XMLReadError),
128				_ => {}
129			}
130		}
131	}
132}