docx_rs/documents/
header.rs

1use serde::ser::{SerializeStruct, Serializer};
2use serde::Serialize;
3use std::io::Write;
4
5use super::*;
6use crate::documents::BuildXML;
7use crate::xml_builder::*;
8
9#[derive(Debug, Clone, PartialEq, Serialize, Default)]
10#[serde(rename_all = "camelCase")]
11pub struct Header {
12    pub has_numbering: bool,
13    pub children: Vec<HeaderChild>,
14}
15
16impl Header {
17    pub fn new() -> Header {
18        Default::default()
19    }
20
21    pub fn add_paragraph(mut self, p: Paragraph) -> Self {
22        if p.has_numbering {
23            self.has_numbering = true
24        }
25        self.children.push(HeaderChild::Paragraph(Box::new(p)));
26        self
27    }
28
29    pub fn add_table(mut self, t: Table) -> Self {
30        if t.has_numbering {
31            self.has_numbering = true
32        }
33        self.children.push(HeaderChild::Table(Box::new(t)));
34        self
35    }
36
37    /// reader only
38    pub(crate) fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
39        if t.has_numbering {
40            self.has_numbering = true
41        }
42        self.children
43            .push(HeaderChild::StructuredDataTag(Box::new(t)));
44        self
45    }
46}
47
48#[derive(Debug, Clone, PartialEq)]
49pub enum HeaderChild {
50    Paragraph(Box<Paragraph>),
51    Table(Box<Table>),
52    StructuredDataTag(Box<StructuredDataTag>),
53}
54
55impl Serialize for HeaderChild {
56    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
57    where
58        S: Serializer,
59    {
60        match *self {
61            HeaderChild::Paragraph(ref p) => {
62                let mut t = serializer.serialize_struct("Paragraph", 2)?;
63                t.serialize_field("type", "paragraph")?;
64                t.serialize_field("data", p)?;
65                t.end()
66            }
67            HeaderChild::Table(ref c) => {
68                let mut t = serializer.serialize_struct("Table", 2)?;
69                t.serialize_field("type", "table")?;
70                t.serialize_field("data", c)?;
71                t.end()
72            }
73            HeaderChild::StructuredDataTag(ref r) => {
74                let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
75                t.serialize_field("type", "structuredDataTag")?;
76                t.serialize_field("data", r)?;
77                t.end()
78            }
79        }
80    }
81}
82
83impl BuildXML for Header {
84    fn build_to<W: Write>(
85        &self,
86        stream: xml::writer::EventWriter<W>,
87    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
88        XMLBuilder::from(stream)
89            .declaration(Some(true))?
90            .open_header()?
91            .apply_each(&self.children, |c, b| match c {
92                HeaderChild::Paragraph(p) => b.add_child(&p),
93                HeaderChild::Table(t) => b.add_child(&t),
94                HeaderChild::StructuredDataTag(t) => b.add_child(&t),
95            })?
96            .close()?
97            .into_inner()
98    }
99}
100
101#[cfg(test)]
102mod tests {
103
104    use super::*;
105    #[cfg(test)]
106    use pretty_assertions::assert_eq;
107    use std::str;
108
109    #[test]
110    fn test_settings() {
111        let c = Header::new();
112        let b = c.build();
113        assert_eq!(
114            str::from_utf8(&b).unwrap(),
115            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:hdr xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14" />"#
116        );
117    }
118}