Skip to main content

docx_rs/reader/
paragraph_property.rs

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