docx_rust/document/
hyperlink.rs1#![allow(unused_must_use)]
2
3use hard_xml::{XmlRead, XmlWrite};
4use std::borrow::Cow;
5
6use crate::{__setter, __xml_test_suites, document::bidir::BidirectionalEmbedding, document::Run};
7
8#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
10#[cfg_attr(test, derive(PartialEq))]
11#[xml(tag = "w:hyperlink")]
12pub struct Hyperlink<'a> {
13 #[xml(attr = "r:id")]
15 pub id: Option<Cow<'a, str>>,
16 #[xml(attr = "w:anchor")]
18 pub anchor: Option<Cow<'a, str>>,
19 #[xml(child = "w:r")]
20 pub content: Option<Run<'a>>,
22 #[xml(child = "w:dir")]
23 pub bidirectional_embedding: Option<BidirectionalEmbedding<'a>>,
25}
26
27impl<'a> Hyperlink<'a> {
28 __setter!(id: Option<Cow<'a, str>>);
29 __setter!(anchor: Option<Cow<'a, str>>);
30 __setter!(content: Option<Run<'a>>);
31
32 pub fn text(&self) -> String {
33 self.iter_text()
34 .map(|c| c.to_string())
35 .collect::<Vec<_>>()
36 .join("")
37 }
38
39 pub fn iter_text(&self) -> Box<dyn Iterator<Item = &Cow<'a, str>> + '_> {
40 Box::new(
41 self.content.iter().flat_map(|run| run.iter_text()).chain(
42 self.bidirectional_embedding
43 .iter()
44 .flat_map(|bidi| bidi.iter_text()),
45 ),
46 )
47 }
48
49 pub fn iter_text_mut(&mut self) -> Box<dyn Iterator<Item = &mut Cow<'a, str>> + '_> {
50 Box::new(
51 self.content
52 .iter_mut()
53 .flat_map(|run| run.iter_text_mut())
54 .chain(
55 self.bidirectional_embedding
56 .iter_mut()
57 .flat_map(|bidi| bidi.iter_text_mut()),
58 ),
59 )
60 }
61}
62
63__xml_test_suites!(
64 Hyperlink,
65 Hyperlink::default(),
66 r#"<w:hyperlink/>"#,
67 Hyperlink::default().id("id"),
68 r#"<w:hyperlink r:id="id"/>"#,
69 Hyperlink::default().anchor("anchor"),
70 r#"<w:hyperlink w:anchor="anchor"/>"#,
71 Hyperlink::default().content(Run::default()),
72 r#"<w:hyperlink><w:r/></w:hyperlink>"#,
73);