docx_rs/documents/elements/
num_pages.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::*;
5use crate::types::*;
6
7#[derive(Serialize, Debug, Clone, PartialEq)]
8pub struct NumPages {
9    pub instr: InstrNUMPAGES,
10}
11
12impl Default for NumPages {
13    fn default() -> Self {
14        Self {
15            instr: InstrNUMPAGES {},
16        }
17    }
18}
19
20impl NumPages {
21    pub fn new() -> Self {
22        Self::default()
23    }
24}
25
26impl BuildXML for NumPages {
27    fn build_to<W: Write>(
28        &self,
29        stream: xml::writer::EventWriter<W>,
30    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
31        Run::new()
32            .add_field_char(FieldCharType::Begin, false)
33            .add_instr_text(InstrText::NUMPAGES(self.instr.clone()))
34            .add_field_char(FieldCharType::Separate, false)
35            .add_text("1")
36            .add_field_char(FieldCharType::End, false)
37            .build_to(stream)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43
44    use super::*;
45    #[cfg(test)]
46    use pretty_assertions::assert_eq;
47    use std::str;
48
49    #[test]
50    fn test_num_pages() {
51        let b = NumPages::new().build();
52        assert_eq!(
53            str::from_utf8(&b).unwrap(),
54            r#"<w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="false" /><w:instrText>NUMPAGES</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /><w:t xml:space="preserve">1</w:t><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r>"#
55        );
56    }
57}