docx_rs/documents/elements/
delete_instr_text.rs

1use serde::ser::{SerializeStruct, Serializer};
2use serde::Serialize;
3use std::io::Write;
4
5use crate::documents::*;
6use crate::xml_builder::*;
7
8#[derive(Debug, Clone, PartialEq)]
9pub enum DeleteInstrText {
10    TOC(InstrToC),
11    TC(InstrTC),
12    PAGEREF(InstrPAGEREF),
13    HYPERLINK(InstrHyperlink),
14    Unsupported(String),
15}
16
17impl BuildXML for DeleteInstrText {
18    fn build_to<W: Write>(
19        &self,
20        stream: xml::writer::EventWriter<W>,
21    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
22        XMLBuilder::from(stream)
23            .open_delete_instr_text()?
24            .apply(|b| match self {
25                DeleteInstrText::TOC(toc) => b.add_child(toc),
26                DeleteInstrText::TC(tc) => b.add_child(tc),
27                DeleteInstrText::PAGEREF(page_ref) => b.add_child(page_ref),
28                DeleteInstrText::HYPERLINK(_link) => todo!(),
29                DeleteInstrText::Unsupported(s) => b.plain_text(s),
30            })?
31            .close()?
32            .into_inner()
33    }
34}
35
36impl Serialize for DeleteInstrText {
37    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
38    where
39        S: Serializer,
40    {
41        match *self {
42            DeleteInstrText::TOC(ref s) => {
43                let mut t = serializer.serialize_struct("TOC", 2)?;
44                t.serialize_field("type", "toc")?;
45                t.serialize_field("data", s)?;
46                t.end()
47            }
48            DeleteInstrText::TC(ref s) => {
49                let mut t = serializer.serialize_struct("TC", 2)?;
50                t.serialize_field("type", "tc")?;
51                t.serialize_field("data", s)?;
52                t.end()
53            }
54            DeleteInstrText::PAGEREF(ref s) => {
55                let mut t = serializer.serialize_struct("PAGEREF", 2)?;
56                t.serialize_field("type", "pageref")?;
57                t.serialize_field("data", s)?;
58                t.end()
59            }
60            DeleteInstrText::HYPERLINK(ref s) => {
61                let mut t = serializer.serialize_struct("HYPERLINK", 2)?;
62                t.serialize_field("type", "hyperlink")?;
63                t.serialize_field("data", s)?;
64                t.end()
65            }
66            DeleteInstrText::Unsupported(ref s) => {
67                let mut t = serializer.serialize_struct("Unsupported", 2)?;
68                t.serialize_field("type", "unsupported")?;
69                t.serialize_field("data", s)?;
70                t.end()
71            }
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78
79    use super::*;
80    #[cfg(test)]
81    use pretty_assertions::assert_eq;
82    use std::str;
83
84    #[test]
85    fn test_delete_toc_instr() {
86        let b = DeleteInstrText::TOC(InstrToC::new().heading_styles_range(1, 3)).build();
87        assert_eq!(
88            str::from_utf8(&b).unwrap(),
89            r#"<w:delInstrText>TOC \o &quot;1-3&quot;</w:delInstrText>"#
90        );
91    }
92}