docx_reader/documents/elements/
instr_hyperlink.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use serde::Serialize;

// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_HYPERLINKHYPERLINK_topic_ID0EFYG1.html
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct InstrHyperlink {
	pub target: String,
	// \l
	pub anchor: bool,
}

impl InstrHyperlink {
	pub fn new(target: impl Into<String>) -> Self {
		Self {
			target: target.into(),
			..Default::default()
		}
	}
}

impl std::str::FromStr for InstrHyperlink {
	type Err = ();

	fn from_str(instr: &str) -> Result<Self, Self::Err> {
		let mut s = instr.split(' ');
		let mut target = "".to_string();
		let mut anchor = false;
		loop {
			if let Some(i) = s.next() {
				match i {
					"\\l" => {
						anchor = true;
					}
					"\\m" => {
						// TODO:
					}
					"\\n" => {
						// TODO:
					}
					"\\o" => {
						// TODO: Support later
						let _ = s.next();
					}
					"\\t" => {
						// TODO: Support later
						let _ = s.next();
					}
					_ => {
						target = i.replace("&quot;", "").replace("\"", "").to_string();
					}
				}
			} else {
				// FIXME: For now, return error if target is not found
				if target.is_empty() {
					return Err(());
				}
				return Ok(Self { target, anchor });
			}
		}
	}
}