docx_rs/documents/elements/
instr_hyperlink.rs

1use serde::Serialize;
2
3// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_HYPERLINKHYPERLINK_topic_ID0EFYG1.html
4#[derive(Serialize, Debug, Clone, PartialEq, Default)]
5#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
6#[cfg_attr(feature = "wasm", ts(export))]
7#[serde(rename_all = "camelCase")]
8pub struct InstrHyperlink {
9    pub target: String,
10    // \l
11    pub anchor: bool,
12}
13
14impl InstrHyperlink {
15    pub fn new(target: impl Into<String>) -> Self {
16        Self {
17            target: target.into(),
18            ..Default::default()
19        }
20    }
21}
22
23// impl BuildXML for instrHyperlink {
24//     fn build(&self) -> Vec<u8> {
25// TODO:
26//     }
27// }
28
29impl std::str::FromStr for InstrHyperlink {
30    type Err = ();
31
32    fn from_str(instr: &str) -> Result<Self, Self::Err> {
33        let mut s = instr.split(' ');
34        let mut target = "".to_string();
35        let mut anchor = false;
36        loop {
37            if let Some(i) = s.next() {
38                match i {
39                    "\\l" => {
40                        anchor = true;
41                    }
42                    "\\m" => {
43                        // TODO:
44                    }
45                    "\\n" => {
46                        // TODO:
47                    }
48                    "\\o" => {
49                        // TODO: Support later
50                        let _ = s.next();
51                    }
52                    "\\t" => {
53                        // TODO: Support later
54                        let _ = s.next();
55                    }
56                    _ => {
57                        target = i.replace("&quot;", "").replace('\"', "").to_string();
58                    }
59                }
60            } else {
61                // FIXME: For now, return error if target is not found
62                if target.is_empty() {
63                    return Err(());
64                }
65                return Ok(Self { target, anchor });
66            }
67        }
68    }
69}