docx_rust/document/
footer.rs

1//! Footer part
2//!
3//! The corresponding ZIP item is `/word/footer{n}.xml`.
4//!
5
6use hard_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter};
7use std::io::Write;
8
9use crate::__xml_test_suites;
10use crate::schema::{SCHEMA_MAIN, SCHEMA_WORDML_14};
11
12use crate::document::BodyContent;
13
14/// The root element of the main document part.
15#[derive(Debug, Default, XmlRead, Clone)]
16#[cfg_attr(test, derive(PartialEq))]
17#[xml(tag = "w:ftr")]
18pub struct Footer<'a> {
19    #[xml(child = "w:sdt", child = "w:p", child = "w:tbl", child = "w:sectPr")]
20    pub content: Vec<BodyContent<'a>>,
21}
22
23impl<'a> Footer<'a> {
24    pub fn push<T: Into<BodyContent<'a>>>(&mut self, content: T) -> &mut Self {
25        self.content.push(content.into());
26        self
27    }
28}
29
30impl<'a> XmlWrite for Footer<'a> {
31    fn to_writer<W: Write>(&self, writer: &mut XmlWriter<W>) -> XmlResult<()> {
32        let Footer { content } = self;
33
34        log::debug!("[Footer] Started writing.");
35        let _ = write!(writer.inner, "{}", crate::schema::SCHEMA_XML);
36
37        writer.write_element_start("w:ftr")?;
38
39        writer.write_attribute("xmlns:w", SCHEMA_MAIN)?;
40
41        writer.write_attribute("xmlns:w14", SCHEMA_WORDML_14)?;
42
43        writer.write_element_end_open()?;
44
45        for c in content {
46            c.to_writer(writer)?;
47        }
48
49        writer.write_element_end_close("w:ftr")?;
50
51        log::debug!("[Document] Finished writing.");
52
53        Ok(())
54    }
55}
56
57__xml_test_suites!(
58    Footer,
59    Footer::default(),
60    format!(
61        r#"{}<w:ftr xmlns:w="{}" xmlns:w14="{}"></w:ftr>"#,
62        crate::schema::SCHEMA_XML,
63        SCHEMA_MAIN,
64        SCHEMA_WORDML_14
65    )
66    .as_str(),
67);