docx_rs/reader/
styles.rs

1use std::io::Read;
2use xml::reader::{EventReader, XmlEvent};
3
4use super::*;
5use crate::reader::{FromXML, ReaderError};
6
7use std::str::FromStr;
8
9impl FromXML for Styles {
10    fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
11        let mut parser = EventReader::new(reader);
12        let mut styles = Self::default();
13        loop {
14            let e = parser.next();
15            match e {
16                Ok(XmlEvent::StartElement {
17                    attributes, name, ..
18                }) => {
19                    let e = XMLElement::from_str(&name.local_name).unwrap();
20                    match e {
21                        XMLElement::Style => {
22                            if let Ok(s) = Style::read(&mut parser, &attributes) {
23                                styles = styles.add_style(s);
24                            }
25                            continue;
26                        }
27                        XMLElement::DocDefaults => {
28                            if let Ok(d) = DocDefaults::read(&mut parser, &attributes) {
29                                styles = styles.doc_defaults(d);
30                            }
31                            continue;
32                        }
33                        _ => {}
34                    }
35                }
36                Ok(XmlEvent::EndElement { name, .. }) => {
37                    let e = XMLElement::from_str(&name.local_name).unwrap();
38                    if let XMLElement::Styles = e {
39                        break;
40                    }
41                }
42                Err(_) => return Err(ReaderError::XMLReadError),
43                _ => {}
44            }
45        }
46        Ok(styles)
47    }
48}
49
50#[cfg(test)]
51mod tests {
52
53    use super::*;
54    use crate::types::*;
55    #[cfg(test)]
56    use pretty_assertions::assert_eq;
57
58    #[test]
59    fn test_from_xml() {
60        let xml = r#"<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
61    <w:style w:type="character" w:styleId="FootnoteTextChar">
62        <w:name w:val="Footnote Text Char"></w:name>
63        <w:rPr>
64            <w:sz w:val="20"></w:sz>
65            <w:szCs w:val="20"></w:szCs>
66        </w:rPr>
67        <w:uiPriority w:val="99"></w:uiPriority>
68        <w:unhideWhenUsed></w:unhideWhenUsed>
69        <w:basedOn w:val="DefaultParagraphFont"></w:basedOn>
70        <w:link w:val="FootnoteText"></w:link>
71        <w:uiPriority w:val="99"></w:uiPriority>
72        <w:semiHidden></w:semiHidden>
73    </w:style>
74</w:styles>"#;
75        let s = Styles::from_xml(xml.as_bytes()).unwrap();
76        let mut styles = Styles::new();
77        styles = styles.add_style(
78            Style::new("FootnoteTextChar", StyleType::Character)
79                .name("Footnote Text Char")
80                .size(20)
81                .based_on("DefaultParagraphFont")
82                .link("FootnoteText"),
83        );
84        assert_eq!(s, styles);
85    }
86}