use serde::ser::{SerializeStruct, Serializer}; use serde::Serialize; use super::*; use crate::documents::BuildXML; use crate::xml_builder::*; #[derive(Debug, Clone, PartialEq, Serialize, Default)] #[serde(rename_all = "camelCase")] pub struct Footer { pub has_numbering: bool, pub children: Vec, } impl Footer { pub fn new() -> Footer { Default::default() } pub fn add_paragraph(mut self, p: Paragraph) -> Self { if p.has_numbering { self.has_numbering = true } self.children.push(FooterChild::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(FooterChild::Table(Box::new(t))); self } } #[derive(Debug, Clone, PartialEq)] pub enum FooterChild { Paragraph(Box), Table(Box), } impl Serialize for FooterChild { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match *self { FooterChild::Paragraph(ref p) => { let mut t = serializer.serialize_struct("Paragraph", 2)?; t.serialize_field("type", "paragraph")?; t.serialize_field("data", p)?; t.end() } FooterChild::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 Footer { fn build(&self) -> Vec { let mut b = XMLBuilder::new(); b = b.declaration(Some(true)).open_footer(); for c in &self.children { match c { FooterChild::Paragraph(p) => b = b.add_child(p), FooterChild::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 = Footer::new(); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), r#" "# ); } }