docx_reader/reader/
hyperlink.rs1use std::io::Read;
2use std::str::FromStr;
3use xml::attribute::OwnedAttribute;
4use xml::reader::{EventReader, XmlEvent};
5
6use super::*;
7
8impl ElementReader for Hyperlink {
9 fn read<R: Read>(
10 r: &mut EventReader<R>,
11 attrs: &[OwnedAttribute],
12 ) -> Result<Self, ReaderError> {
13 let mut rid: Option<String> = read(attrs, "id");
14 let mut anchor: Option<String> = read(attrs, "anchor");
15 let history: Option<String> = read(attrs, "history");
16 let mut link = Hyperlink {
17 link: if anchor.is_some() {
18 HyperlinkData::Anchor {
19 anchor: anchor.take().unwrap(),
20 }
21 } else {
22 HyperlinkData::External {
23 rid: rid.take().unwrap_or_default(),
24 path: String::default(), }
26 },
27 history: history.map(|h| usize::from_str(&h).unwrap_or(1)),
28 children: vec![],
29 };
30
31 loop {
32 let e = r.next();
33 match e {
34 Ok(XmlEvent::StartElement {
35 attributes, name, ..
36 }) => {
37 let e = XMLElement::from_str(&name.local_name).unwrap();
38
39 match e {
40 XMLElement::Run => {
41 if let Ok(run) = Run::read(r, attrs) {
42 link = link.add_run(run);
43 }
44 continue;
45 }
46 XMLElement::Insert => {
47 if let Ok(ins) = Insert::read(r, &attributes) {
48 link = link.add_insert(ins);
49 }
50 continue;
51 }
52 XMLElement::Delete => {
53 if let Ok(del) = Delete::read(r, &attributes) {
54 link = link.add_delete(del);
55 }
56 continue;
57 }
58 _ => {}
59 }
60 }
61 Ok(XmlEvent::EndElement { name, .. }) => {
62 let e = XMLElement::from_str(&name.local_name).unwrap();
63 if e == XMLElement::Hyperlink {
64 return Ok(link);
65 }
66 }
67 Err(_) => return Err(ReaderError::XMLReadError),
68 _ => {}
69 }
70 }
71 }
72}