docx_reader/reader/
style.rs

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