docx_reader/reader/
rels.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
40
use std::io::Read;
use xml::reader::{EventReader, XmlEvent};

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

use super::*;

impl FromXML for Rels {
	fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
		let parser = EventReader::new(reader);
		let mut s = Self::default();
		let mut depth = 0;
		for e in parser {
			match e {
				Ok(XmlEvent::StartElement { attributes, .. }) => {
					if depth == 1 {
						let mut rel_type = "".to_owned();
						let mut target = "".to_owned();
						for attr in attributes {
							let name: &str = &attr.name.local_name;
							if name == "Type" {
								rel_type = attr.value.clone();
							} else if name == "Target" {
								target = attr.value.clone();
							}
						}
						s = s.add_rel(rel_type, target);
					}
					depth += 1;
				}
				Ok(XmlEvent::EndElement { .. }) => {
					depth -= 1;
				}
				Err(_) => return Err(ReaderError::XMLReadError),
				_ => {}
			}
		}
		Ok(s)
	}
}