1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
use super::{Paragraph, TableCellProperty}; use crate::documents::BuildXML; use crate::types::*; use crate::xml_builder::*; #[derive(Debug, Clone)] pub struct TableCell { pub contents: Vec<TableCellContent>, pub property: TableCellProperty, pub has_numbering: bool, } #[derive(Debug, Clone)] pub enum TableCellContent { Paragraph(Paragraph), } impl TableCell { pub fn new() -> TableCell { Default::default() } pub fn add_paragraph(mut self, p: Paragraph) -> TableCell { if p.has_numbering { self.has_numbering = true } self.contents.push(TableCellContent::Paragraph(p)); self } pub fn vertical_merge(mut self, t: VMergeType) -> TableCell { self.property = self.property.vertical_merge(t); self } pub fn grid_span(mut self, v: usize) -> TableCell { self.property = self.property.grid_span(v); self } pub fn width(mut self, v: usize) -> TableCell { self.property = self.property.width(v, WidthType::DXA); self } } impl Default for TableCell { fn default() -> Self { let property = TableCellProperty::new(); let contents = vec![]; Self { property, contents, has_numbering: false, } } } impl BuildXML for TableCell { fn build(&self) -> Vec<u8> { 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::super::*; use super::*; #[cfg(test)] use pretty_assertions::assert_eq; use std::str; #[test] fn test_cell() { let b = TableCell::new().build(); assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:tc><w:tcPr /></w:tc>"#); } #[test] fn test_cell_add_p() { let b = TableCell::new() .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))) .build(); assert_eq!( str::from_utf8(&b).unwrap(), r#"<w:tc><w:tcPr /><w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p></w:tc>"# ); } }