docx_rs/reader/
paragraph_property.rs

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