diff --git a/docx-core/src/documents/elements/color.rs b/docx-core/src/documents/elements/color.rs index a24b6c6..aff1e95 100644 --- a/docx-core/src/documents/elements/color.rs +++ b/docx-core/src/documents/elements/color.rs @@ -1,7 +1,7 @@ use crate::documents::BuildXML; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Color { val: String, } diff --git a/docx-core/src/documents/elements/indent.rs b/docx-core/src/documents/elements/indent.rs index d1c34ea..5f1924b 100644 --- a/docx-core/src/documents/elements/indent.rs +++ b/docx-core/src/documents/elements/indent.rs @@ -2,7 +2,7 @@ use crate::documents::BuildXML; use crate::types::*; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Indent { left: usize, special_indent: Option, diff --git a/docx-core/src/documents/elements/justification.rs b/docx-core/src/documents/elements/justification.rs index 47e326e..bf43447 100644 --- a/docx-core/src/documents/elements/justification.rs +++ b/docx-core/src/documents/elements/justification.rs @@ -9,7 +9,7 @@ use crate::xml_builder::*; // present without the val attribute, the default of the val attribute is centerGroup . This means that the instances // of mathematical text can be aligned with respect to each other, but the entire group of mathematical text is // centered as a whole. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Justification { val: String, } diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index b7a3db1..3560355 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -16,6 +16,7 @@ mod style; mod sz; mod sz_cs; mod table_borders; +mod table_cell; mod table_cell_borders; mod table_cell_margins; mod table_cell_property; @@ -44,6 +45,7 @@ pub use style::*; pub use sz::*; pub use sz_cs::*; pub use table_borders::*; +pub use table_cell::*; pub use table_cell_borders::*; pub use table_cell_margins::*; pub use table_cell_property::*; diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 5fc7d91..83f5eb5 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -3,7 +3,7 @@ use crate::documents::BuildXML; use crate::types::*; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Paragraph { runs: Vec, property: ParagraphProperty, diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index 60d4a84..b00b94a 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -3,7 +3,7 @@ use crate::documents::BuildXML; use crate::types::{AlignmentType, SpecialIndentType}; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ParagraphProperty { run_property: RunProperty, style: ParagraphStyle, diff --git a/docx-core/src/documents/elements/paragraph_style.rs b/docx-core/src/documents/elements/paragraph_style.rs index afe4e75..4b30508 100644 --- a/docx-core/src/documents/elements/paragraph_style.rs +++ b/docx-core/src/documents/elements/paragraph_style.rs @@ -1,7 +1,7 @@ use crate::documents::BuildXML; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ParagraphStyle { val: String, } diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index 6c9757f..376c3bf 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -2,7 +2,7 @@ use super::{RunProperty, Text}; use crate::documents::BuildXML; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Run { run_property: RunProperty, text: Text, diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index e73c90a..e7728b4 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -2,7 +2,7 @@ use super::{Color, Sz, SzCs}; use crate::documents::BuildXML; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct RunProperty { sz: Option, sz_cs: Option, diff --git a/docx-core/src/documents/elements/sz.rs b/docx-core/src/documents/elements/sz.rs index 6f79c10..ba0ba9b 100644 --- a/docx-core/src/documents/elements/sz.rs +++ b/docx-core/src/documents/elements/sz.rs @@ -1,7 +1,7 @@ use crate::documents::BuildXML; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Sz { val: usize, } diff --git a/docx-core/src/documents/elements/sz_cs.rs b/docx-core/src/documents/elements/sz_cs.rs index 15fd6fb..3f99e64 100644 --- a/docx-core/src/documents/elements/sz_cs.rs +++ b/docx-core/src/documents/elements/sz_cs.rs @@ -1,7 +1,7 @@ use crate::documents::BuildXML; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SzCs { val: usize, } diff --git a/docx-core/src/documents/elements/table_cell.rs b/docx-core/src/documents/elements/table_cell.rs new file mode 100644 index 0000000..2563b4e --- /dev/null +++ b/docx-core/src/documents/elements/table_cell.rs @@ -0,0 +1,69 @@ +use super::{Paragraph, Run, TableCellProperty}; +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct TableCell { + property: TableCellProperty, + contents: Vec, +} + +#[derive(Debug, Clone)] +pub enum TableCellContent { + Paragraph(Paragraph), +} + +impl TableCell { + pub fn new(w: usize) -> TableCell { + let property = TableCellProperty::new(w); + let contents = vec![]; + Self { property, contents } + } + + pub fn add_paragraph(mut self, p: Paragraph) -> TableCell { + self.contents.push(TableCellContent::Paragraph(p)); + self + } +} + +impl BuildXML for TableCell { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + let mut b = b.open_table_cell().add_child(&self.property); + for c in &self.contents { + match c { + TableCellContent::Paragraph(p) => b = b.add_child(p), + } + } + b.close().build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_cell() { + let b = TableCell::new(200).build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } + + #[test] + fn test_cell_add_p() { + let b = TableCell::new(200) + .add_paragraph(Paragraph::new().add_run(Run::new("Hello"))) + .build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#"Hello"# + ); + } +} diff --git a/docx-core/src/documents/elements/table_cell_property.rs b/docx-core/src/documents/elements/table_cell_property.rs index e168ec2..2a7080b 100644 --- a/docx-core/src/documents/elements/table_cell_property.rs +++ b/docx-core/src/documents/elements/table_cell_property.rs @@ -3,7 +3,7 @@ use crate::documents::BuildXML; use crate::types::*; use crate::xml_builder::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct TableCellProperty { width: TableCellWidth, borders: TableCellBorders,