Skip to main content

docx_rs/reader/
style.rs

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