use super::{Paragraph, Table}; use crate::documents::BuildXML; use crate::xml_builder::*; #[derive(Debug)] pub struct Document { pub(crate) children: Vec, } #[derive(Debug, Clone)] pub enum DocumentChild { Paragraph(Paragraph), Table(Table), } impl Document { pub fn new() -> Document { Default::default() } pub fn add_paragraph(mut self, p: Paragraph) -> Self { self.children.push(DocumentChild::Paragraph(p)); self } pub fn add_table(mut self, t: Table) -> Self { self.children.push(DocumentChild::Table(t)); self } } impl Default for Document { fn default() -> Self { Self { children: Vec::new(), } } } impl BuildXML for Document { fn build(&self) -> Vec { let mut b = XMLBuilder::new() .declaration(Some(true)) .open_document() .open_body(); for c in &self.children { match c { DocumentChild::Paragraph(p) => b = b.add_child(p), DocumentChild::Table(t) => b = b.add_child(t), } } b.close().close().build() } } #[cfg(test)] mod tests { use super::super::Run; use super::*; #[cfg(test)] use pretty_assertions::assert_eq; use std::str; #[test] fn test_document() { let b = Document::new() .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))) .build(); assert_eq!( str::from_utf8(&b).unwrap(), r#" Hello "# ); } }