docx_reader/documents/
header.rsuse serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use super::*;
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Header {
pub has_numbering: bool,
pub children: Vec<HeaderChild>,
}
impl Header {
pub fn new() -> Header {
Default::default()
}
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
if p.has_numbering {
self.has_numbering = true
}
self.children.push(HeaderChild::Paragraph(Box::new(p)));
self
}
pub fn add_table(mut self, t: Table) -> Self {
if t.has_numbering {
self.has_numbering = true
}
self.children.push(HeaderChild::Table(Box::new(t)));
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum HeaderChild {
Paragraph(Box<Paragraph>),
Table(Box<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()
}
}
}
}