docx_reader/documents/elements/
instr_pageref.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
use serde::Serialize;

// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_PAGEREFPAGEREF_topic_ID0EHXK1.html
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct InstrPAGEREF {
	pub page_ref: String,
	pub hyperlink: bool,
	pub relative_position: bool,
}

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

	pub fn hyperlink(mut self) -> Self {
		self.hyperlink = true;
		self
	}

	pub fn relative_position(mut self) -> Self {
		self.relative_position = true;
		self
	}
}

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

	fn from_str(instr: &str) -> Result<Self, Self::Err> {
		let mut s = instr.split(' ');
		let text = s.next();
		let mut page_ref = InstrPAGEREF::new(text.unwrap_or_default());
		loop {
			if let Some(i) = s.next() {
				match i {
					"\\h" => page_ref = page_ref.hyperlink(),
					"\\p" => page_ref = page_ref.relative_position(),
					_ => {}
				}
			} else {
				return Ok(page_ref);
			}
		}
	}
}