docx_rust/document/
endnotes.rs

1//! EndNotes part
2//!
3//! The corresponding ZIP item is `/word/endnotes.xml`.
4//!
5
6use hard_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter};
7use std::borrow::Cow;
8use std::io::Write;
9
10use crate::__xml_test_suites;
11use crate::schema::{SCHEMA_MAIN, SCHEMA_WORDML_14};
12
13use crate::document::BodyContent;
14
15use super::NoteSeparator;
16
17#[derive(Debug, Default, XmlRead, Clone)]
18#[cfg_attr(test, derive(PartialEq))]
19#[xml(tag = "w:endnotes")]
20pub struct EndNotes<'a> {
21    #[xml(child = "w:endnote")]
22    pub content: Vec<EndNote<'a>>,
23}
24
25#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
26#[cfg_attr(test, derive(PartialEq))]
27#[xml(tag = "w:endnote")]
28pub struct EndNote<'a> {
29    #[xml(attr = "w:type")]
30    pub ty: Option<NoteSeparator>,
31    #[xml(attr = "w:id")]
32    pub id: Option<isize>,
33    #[xml(child = "w:sdt", child = "w:p", child = "w:tbl", child = "w:sectPr")]
34    pub content: Vec<BodyContent<'a>>,
35}
36
37#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
38#[cfg_attr(test, derive(PartialEq))]
39#[xml(tag = "w:endnoteRef")]
40pub struct EndnoteRef;
41
42#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
43#[cfg_attr(test, derive(PartialEq))]
44#[xml(tag = "w:endnoteReference")]
45pub struct EndnoteReference<'a> {
46    /// Specifies the HeaderReference type of this HeaderReference.
47    #[xml(attr = "w:customMarkFollows")]
48    pub supress_reference_mark: Option<bool>,
49    #[xml(attr = "w:id")]
50    pub id: Option<Cow<'a, str>>,
51}
52
53impl<'a> EndNote<'a> {
54    pub fn push<T: Into<BodyContent<'a>>>(&mut self, content: T) -> &mut Self {
55        self.content.push(content.into());
56        self
57    }
58}
59
60impl<'a> XmlWrite for EndNotes<'a> {
61    fn to_writer<W: Write>(&self, writer: &mut XmlWriter<W>) -> XmlResult<()> {
62        let EndNotes { content } = self;
63
64        log::debug!("[EndNotes] Started writing.");
65        let _ = write!(writer.inner, "{}", crate::schema::SCHEMA_XML);
66
67        writer.write_element_start("w:endnotes")?;
68
69        writer.write_attribute("xmlns:w", SCHEMA_MAIN)?;
70
71        writer.write_attribute("xmlns:w14", SCHEMA_WORDML_14)?;
72
73        writer.write_element_end_open()?;
74
75        for c in content {
76            c.to_writer(writer)?;
77        }
78
79        writer.write_element_end_close("w:endnotes")?;
80
81        log::debug!("[Document] Finished writing.");
82
83        Ok(())
84    }
85}
86
87__xml_test_suites!(
88    EndNotes,
89    EndNotes::default(),
90    format!(
91        r#"{}<w:endnotes xmlns:w="{}" xmlns:w14="{}"></w:endnotes>"#,
92        crate::schema::SCHEMA_XML,
93        SCHEMA_MAIN,
94        SCHEMA_WORDML_14
95    )
96    .as_str(),
97);