docx_reader/documents/elements/
hyperlink.rsuse 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,
#[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
}
}