docx_rs/documents/elements/
doc_defaults.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::{documents::BuildXML, RunProperty};
5use crate::{xml_builder::*, LineSpacing, ParagraphProperty, ParagraphPropertyDefault};
6
7use super::run_property_default::*;
8use super::RunFonts;
9
10#[derive(Debug, Clone, PartialEq, Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct DocDefaults {
13    run_property_default: RunPropertyDefault,
14    paragraph_property_default: ParagraphPropertyDefault,
15}
16
17impl DocDefaults {
18    pub fn new() -> DocDefaults {
19        Default::default()
20    }
21
22    pub fn size(mut self, size: usize) -> Self {
23        self.run_property_default = self.run_property_default.size(size);
24        self
25    }
26
27    pub fn spacing(mut self, spacing: i32) -> Self {
28        self.run_property_default = self.run_property_default.spacing(spacing);
29        self
30    }
31
32    pub fn fonts(mut self, font: RunFonts) -> Self {
33        self.run_property_default = self.run_property_default.fonts(font);
34        self
35    }
36
37    pub fn line_spacing(mut self, spacing: LineSpacing) -> Self {
38        self.paragraph_property_default = self.paragraph_property_default.line_spacing(spacing);
39        self
40    }
41
42    pub(crate) fn run_property(mut self, p: RunProperty) -> Self {
43        self.run_property_default = self.run_property_default.run_property(p);
44        self
45    }
46
47    pub(crate) fn paragraph_property(mut self, p: ParagraphProperty) -> Self {
48        self.paragraph_property_default = self.paragraph_property_default.paragraph_property(p);
49        self
50    }
51}
52
53impl Default for DocDefaults {
54    fn default() -> Self {
55        let run_property_default = RunPropertyDefault::new();
56        let paragraph_property_default = ParagraphPropertyDefault::new();
57        DocDefaults {
58            run_property_default,
59            paragraph_property_default,
60        }
61    }
62}
63
64impl BuildXML for DocDefaults {
65    fn build_to<W: Write>(
66        &self,
67        stream: xml::writer::EventWriter<W>,
68    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
69        XMLBuilder::from(stream)
70            .open_doc_defaults()?
71            .add_child(&self.run_property_default)?
72            .add_child(&self.paragraph_property_default)?
73            .close()?
74            .into_inner()
75    }
76}
77
78#[cfg(test)]
79mod tests {
80
81    use super::*;
82    #[cfg(test)]
83    use pretty_assertions::assert_eq;
84    use std::str;
85
86    #[test]
87    fn test_build() {
88        let c = DocDefaults::new();
89        let b = c.build();
90        assert_eq!(
91            str::from_utf8(&b).unwrap(),
92            r#"<w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault><w:pPrDefault><w:pPr><w:rPr /></w:pPr></w:pPrDefault></w:docDefaults>"#
93        );
94    }
95}