docx_reader/reader/
web_settings.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::io::Read;
use std::str::FromStr;
use xml::reader::{EventReader, XmlEvent};

use crate::reader::{FromXML, ReaderError};

use super::*;

impl FromXML for WebSettings {
	fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
		let mut parser = EventReader::new(reader);
		let mut settings = Self::default();
		loop {
			let e = parser.next();
			match e {
				Ok(XmlEvent::StartElement {
					attributes, name, ..
				}) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					if let XMLElement::Div = e {
						if let Ok(div) = Div::read(&mut parser, &attributes) {
							settings.divs.push(div);
						}
					}
				}
				Ok(XmlEvent::EndElement { name, .. }) => {
					let e = XMLElement::from_str(&name.local_name).unwrap();
					if let XMLElement::WebSettings = e {
						break;
					}
				}
				Ok(XmlEvent::EndDocument { .. }) => break,
				Err(_) => return Err(ReaderError::XMLReadError),
				_ => {}
			}
		}
		Ok(settings)
	}
}