docx_reader/reader/
styles.rsuse std::io::Read;
use std::str::FromStr;
use xml::reader::{EventReader, XmlEvent};
use crate::reader::{FromXML, ReaderError};
use super::*;
impl FromXML for Styles {
fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
let mut parser = EventReader::new(reader);
let mut styles = Self::default();
loop {
let e = parser.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::Style => {
if let Ok(s) = Style::read(&mut parser, &attributes) {
styles = styles.add_style(s);
}
continue;
}
XMLElement::DocDefaults => {
if let Ok(d) = DocDefaults::read(&mut parser, &attributes) {
styles = styles.doc_defaults(d);
}
continue;
}
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::Styles = e {
break;
}
}
Err(_) => return Err(ReaderError::XMLReadError),
_ => {}
}
}
Ok(styles)
}
}