2020-08-13 11:27:45 +03:00
|
|
|
use serde::ser::{SerializeStruct, Serializer};
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::documents::BuildXML;
|
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Header {
|
2021-11-25 16:33:13 +02:00
|
|
|
pub has_numbering: bool,
|
2020-08-13 11:27:45 +03:00
|
|
|
pub children: Vec<HeaderChild>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Header {
|
|
|
|
pub fn new() -> Header {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
|
2021-11-25 16:33:13 +02:00
|
|
|
if p.has_numbering {
|
|
|
|
self.has_numbering = true
|
|
|
|
}
|
2020-08-13 11:27:45 +03:00
|
|
|
self.children.push(HeaderChild::Paragraph(p));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_table(mut self, t: Table) -> Self {
|
2021-11-25 16:33:13 +02:00
|
|
|
if t.has_numbering {
|
|
|
|
self.has_numbering = true
|
|
|
|
}
|
2020-08-13 11:27:45 +03:00
|
|
|
self.children.push(HeaderChild::Table(t));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Header {
|
|
|
|
fn default() -> Self {
|
2021-11-25 16:33:13 +02:00
|
|
|
Self {
|
|
|
|
children: vec![],
|
|
|
|
has_numbering: false,
|
|
|
|
}
|
2020-08-13 11:27:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum HeaderChild {
|
|
|
|
Paragraph(Paragraph),
|
|
|
|
Table(Table),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for HeaderChild {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
HeaderChild::Paragraph(ref p) => {
|
|
|
|
let mut t = serializer.serialize_struct("Paragraph", 2)?;
|
|
|
|
t.serialize_field("type", "paragraph")?;
|
|
|
|
t.serialize_field("data", p)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
HeaderChild::Table(ref c) => {
|
|
|
|
let mut t = serializer.serialize_struct("Table", 2)?;
|
|
|
|
t.serialize_field("type", "table")?;
|
|
|
|
t.serialize_field("data", c)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuildXML for Header {
|
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
let mut b = XMLBuilder::new();
|
|
|
|
b = b.declaration(Some(true)).open_header();
|
|
|
|
|
|
|
|
for c in &self.children {
|
|
|
|
match c {
|
|
|
|
HeaderChild::Paragraph(p) => b = b.add_child(p),
|
|
|
|
HeaderChild::Table(t) => b = b.add_child(t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.close().build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_settings() {
|
|
|
|
let c = Header::new();
|
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
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" />"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|