docx_rs/reader/
settings.rs1use std::io::Read;
2use std::str::FromStr;
3use xml::reader::{EventReader, XmlEvent};
4
5use super::*;
6use crate::reader::{FromXML, ReaderError};
7use crate::types::CharacterSpacingValues;
8
9impl FromXML for Settings {
10 fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
11 let mut parser = EventReader::new(reader);
12 let mut settings = Self::default();
13
14 loop {
15 let e = parser.next();
16 match e {
17 Ok(XmlEvent::StartElement {
18 attributes, name, ..
19 }) => {
20 let e = XMLElement::from_str(&name.local_name).unwrap();
21 match e {
22 XMLElement::DocId => {
23 for a in attributes {
24 if let Some(prefix) = a.name.prefix {
25 let local_name = &a.name.local_name;
26 if local_name == "val" && prefix == "w15" {
28 settings = settings
29 .doc_id(a.value.to_owned().replace(['{', '}'], ""));
30 }
31 }
32 }
33 }
34 XMLElement::DocVar => {
35 let name = attributes::read_name(&attributes);
36 let val = attributes::read_val(&attributes);
37 if let Some(name) = name {
38 if let Some(val) = val {
39 settings = settings.add_doc_var(name, val);
40 }
41 }
42 }
43 XMLElement::DefaultTabStop => {
44 let val = attributes::read_val(&attributes);
45 if let Some(val) = val {
46 if let Ok(val) = f32::from_str(&val) {
47 settings = settings.default_tab_stop(val as usize);
48 }
49 }
50 }
51 XMLElement::EvenAndOddHeaders => {
52 let val = attributes::read_bool(&attributes);
53 if val {
54 settings = settings.even_and_odd_headers();
55 }
56 }
57 XMLElement::AdjustLineHeightInTable => {
58 settings = settings.adjust_line_height_in_table();
59 }
60 XMLElement::CharacterSpacingControl => {
61 let val = read_val(&attributes);
62 if let Some(val) = val {
63 if let Ok(v) = CharacterSpacingValues::from_str(&val) {
64 settings = settings.character_spacing_control(v);
65 }
66 }
67 }
68 _ => {}
69 }
70 }
71 Ok(XmlEvent::EndElement { name, .. }) => {
72 let e = XMLElement::from_str(&name.local_name).unwrap();
73 if let XMLElement::Settings = e {
74 break;
75 }
76 }
77 Ok(XmlEvent::EndDocument { .. }) => break,
78 Err(_) => return Err(ReaderError::XMLReadError),
79 _ => {}
80 }
81 }
82 Ok(settings)
83 }
84}