docx_rs/reader/
style.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use xml::attribute::OwnedAttribute;
5use xml::reader::{EventReader, XmlEvent};
6
7use super::*;
8
9use crate::types::*;
10
11impl ElementReader for Style {
12    fn read<R: Read>(
13        r: &mut EventReader<R>,
14        attrs: &[OwnedAttribute],
15    ) -> Result<Self, ReaderError> {
16        let mut id = "".to_owned();
17        let mut style_type = StyleType::Paragraph;
18        for a in attrs {
19            let local_name = &a.name.local_name;
20            if local_name == "styleId" {
21                id = a.value.clone();
22            } else if local_name == "type" {
23                style_type = StyleType::from_str(&a.value)?;
24            }
25        }
26        let mut style = Style::new(id, style_type);
27        loop {
28            let e = r.next();
29            match e {
30                Ok(XmlEvent::StartElement {
31                    attributes, name, ..
32                }) => {
33                    let e = XMLElement::from_str(&name.local_name).unwrap();
34                    match e {
35                        XMLElement::Name => {
36                            style = style.name(attributes[0].value.clone());
37                            continue;
38                        }
39                        XMLElement::BasedOn => {
40                            if let Some(v) = read_val(&attributes) {
41                                style = style.based_on(v);
42                            }
43                            continue;
44                        }
45                        XMLElement::Link => {
46                            if let Some(v) = read_val(&attributes) {
47                                style = style.link(v);
48                            }
49                            continue;
50                        }
51                        // pPr
52                        XMLElement::ParagraphProperty => {
53                            if let Ok(pr) = ParagraphProperty::read(r, attrs) {
54                                style.paragraph_property = pr;
55                            }
56                            continue;
57                        }
58                        // rPr
59                        XMLElement::RunProperty => {
60                            let p = RunProperty::read(r, &attributes)?;
61                            style.run_property = p;
62                        }
63                        XMLElement::TableProperty => {
64                            if let Ok(p) = TableProperty::read(r, &attributes) {
65                                style = style.table_property(p);
66                            }
67                        }
68                        XMLElement::TableCellProperty => {
69                            if let Ok(p) = TableCellProperty::read(r, &attributes) {
70                                style = style.table_cell_property(p);
71                            }
72                        }
73                        _ => {}
74                    }
75                }
76                Ok(XmlEvent::EndElement { name, .. }) => {
77                    let e = XMLElement::from_str(&name.local_name).unwrap();
78                    if let XMLElement::Style = e {
79                        return Ok(style);
80                    }
81                }
82                Err(_) => return Err(ReaderError::XMLReadError),
83                _ => {}
84            }
85        }
86    }
87}