docx_reader/documents/elements/
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
62
63
64
65
66
67
68
69
70
71
use serde::Serialize;

use super::*;
use crate::types::*;
use crate::{create_hyperlink_rid, generate_hyperlink_id};

#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
pub enum HyperlinkData {
	External {
		rid: String,
		// path is writer only
		#[serde(skip_serializing_if = "String::is_empty")]
		path: String,
	},
	Anchor {
		anchor: String,
	},
}

#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Hyperlink {
	#[serde(flatten)]
	pub link: HyperlinkData,
	pub history: Option<usize>,
	pub children: Vec<ParagraphChild>,
}

impl Hyperlink {
	pub fn new(value: impl Into<String>, t: HyperlinkType) -> Self {
		let link = {
			match t {
				HyperlinkType::External => HyperlinkData::External {
					rid: create_hyperlink_rid(generate_hyperlink_id()),
					path: value.into(),
				},
				HyperlinkType::Anchor => HyperlinkData::Anchor {
					anchor: value.into(),
				},
			}
		};
		Hyperlink {
			link,
			history: None,
			children: vec![],
		}
	}

	pub fn add_run(mut self, run: Run) -> Self {
		self.children.push(ParagraphChild::Run(Box::new(run)));
		self
	}

	pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
		self.children
			.push(ParagraphChild::StructuredDataTag(Box::new(t)));
		self
	}

	pub fn add_insert(mut self, insert: Insert) -> Self {
		self.children.push(ParagraphChild::Insert(insert));
		self
	}

	pub fn add_delete(mut self, delete: Delete) -> Self {
		self.children.push(ParagraphChild::Delete(delete));
		self
	}
}