From 7153db3ed8a4e669b1335ccb726edbfc05ef2ca1 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 12 Nov 2019 19:21:08 +0900 Subject: [PATCH] feat: Add table --- docx-core/src/documents/document.rs | 36 +++++++++----- docx-core/src/documents/elements/mod.rs | 2 + docx-core/src/documents/elements/table.rs | 49 +++++++++++++++++++ .../src/documents/elements/table_property.rs | 2 +- 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 docx-core/src/documents/elements/table.rs diff --git a/docx-core/src/documents/document.rs b/docx-core/src/documents/document.rs index 3695b98..3bda466 100644 --- a/docx-core/src/documents/document.rs +++ b/docx-core/src/documents/document.rs @@ -1,10 +1,16 @@ -use super::Paragraph; +use super::{Paragraph, Table}; use crate::documents::BuildXML; use crate::xml_builder::*; #[derive(Debug)] pub struct Document { - paragraphs: Vec, + children: Vec, +} + +#[derive(Debug, Clone)] +pub enum DocumentContent { + Paragraph(Paragraph), + Table(Table), } impl Document { @@ -13,7 +19,12 @@ impl Document { } pub fn add_paragraph(mut self, p: Paragraph) -> Self { - self.paragraphs.push(p); + self.children.push(DocumentContent::Paragraph(p)); + self + } + + pub fn add_table(mut self, t: Table) -> Self { + self.children.push(DocumentContent::Table(t)); self } } @@ -21,21 +32,24 @@ impl Document { impl Default for Document { fn default() -> Self { Self { - paragraphs: Vec::new(), + children: Vec::new(), } } } impl BuildXML for Document { fn build(&self) -> Vec { - let b = XMLBuilder::new(); - b.declaration(Some(true)) + let mut b = XMLBuilder::new() + .declaration(Some(true)) .open_document() - .open_body() - .add_children(&self.paragraphs) - .close() - .close() - .build() + .open_body(); + for c in &self.children { + match c { + DocumentContent::Paragraph(p) => b = b.add_child(p), + DocumentContent::Table(t) => b = b.add_child(t), + } + } + b.close().close().build() } } diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 4e991d0..a3187f0 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -15,6 +15,7 @@ mod run_property_default; mod style; mod sz; mod sz_cs; +mod table; mod table_borders; mod table_cell; mod table_cell_borders; @@ -46,6 +47,7 @@ pub use run_property_default::*; pub use style::*; pub use sz::*; pub use sz_cs::*; +pub use table::*; pub use table_borders::*; pub use table_cell::*; pub use table_cell_borders::*; diff --git a/docx-core/src/documents/elements/table.rs b/docx-core/src/documents/elements/table.rs new file mode 100644 index 0000000..fdcfdab --- /dev/null +++ b/docx-core/src/documents/elements/table.rs @@ -0,0 +1,49 @@ +use super::{TableProperty, TableRow}; +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct Table { + property: TableProperty, + rows: Vec, +} + +impl Table { + pub fn new(rows: Vec) -> Table { + let property = TableProperty::new(); + Self { property, rows } + } +} + +impl BuildXML for Table { + fn build(&self) -> Vec { + let b = XMLBuilder::new() + .open_table() + .add_child(&self.property) + .add_children(&self.rows); + b.close().build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_row() { + let b = Table::new(vec![TableRow::new(vec![])]).build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#" + + + + +"# + ); + } +} diff --git a/docx-core/src/documents/elements/table_property.rs b/docx-core/src/documents/elements/table_property.rs index 0144773..a0c1fa2 100644 --- a/docx-core/src/documents/elements/table_property.rs +++ b/docx-core/src/documents/elements/table_property.rs @@ -3,7 +3,7 @@ use crate::documents::BuildXML; use crate::types::*; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct TableProperty { width: TableWidth, justification: Justification,