docx_rs/documents/elements/
page_margin.rs

1use crate::documents::BuildXML;
2use crate::types::PageMargin;
3use crate::xml_builder::*;
4use std::io::Write;
5
6// These values were based on microsoft office word2019 windows edition.
7// <w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0"/>
8impl Default for PageMargin {
9    fn default() -> PageMargin {
10        PageMargin {
11            top: 1985,
12            left: 1701,
13            bottom: 1701,
14            right: 1701,
15            header: 851,
16            footer: 992,
17            gutter: 0,
18        }
19    }
20}
21
22impl PageMargin {
23    pub fn new() -> PageMargin {
24        Default::default()
25    }
26
27    pub fn top(self, v: i32) -> PageMargin {
28        PageMargin { top: v, ..self }
29    }
30
31    pub fn left(self, v: i32) -> PageMargin {
32        PageMargin { left: v, ..self }
33    }
34
35    pub fn bottom(self, v: i32) -> PageMargin {
36        PageMargin { bottom: v, ..self }
37    }
38
39    pub fn right(self, v: i32) -> PageMargin {
40        PageMargin { right: v, ..self }
41    }
42
43    pub fn header(self, v: i32) -> PageMargin {
44        PageMargin { header: v, ..self }
45    }
46
47    pub fn footer(self, v: i32) -> PageMargin {
48        PageMargin { footer: v, ..self }
49    }
50
51    pub fn gutter(self, v: i32) -> PageMargin {
52        PageMargin { gutter: v, ..self }
53    }
54}
55
56impl BuildXML for PageMargin {
57    fn build_to<W: Write>(
58        &self,
59        stream: xml::writer::EventWriter<W>,
60    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
61        XMLBuilder::from(stream)
62            .page_margin(
63                &format!("{}", self.top),
64                &format!("{}", self.right),
65                &format!("{}", self.bottom),
66                &format!("{}", self.left),
67                &format!("{}", self.header),
68                &format!("{}", self.footer),
69                &format!("{}", self.gutter),
70            )?
71            .into_inner()
72    }
73}
74
75#[cfg(test)]
76mod tests {
77
78    use super::*;
79    #[cfg(test)]
80    use pretty_assertions::assert_eq;
81    use std::str;
82
83    #[test]
84    fn test_page_margin_default() {
85        let b = PageMargin::new().build();
86        assert_eq!(
87            str::from_utf8(&b).unwrap(),
88            r#"<w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" />"#
89        );
90    }
91}