feat: Add row

main
bokuweb 2019-11-12 19:11:58 +09:00
parent 23b422c25f
commit f4a93bd4dd
5 changed files with 100 additions and 19 deletions

View File

@ -24,6 +24,8 @@ mod table_cell_width;
mod table_grid; mod table_grid;
mod table_indent; mod table_indent;
mod table_property; mod table_property;
mod table_row;
mod table_row_property;
mod table_width; mod table_width;
mod text; mod text;
@ -53,5 +55,7 @@ pub use table_cell_width::*;
pub use table_grid::*; pub use table_grid::*;
pub use table_indent::*; pub use table_indent::*;
pub use table_property::*; pub use table_property::*;
pub use table_row::*;
pub use table_row_property::*;
pub use table_width::*; pub use table_width::*;
pub use text::*; pub use text::*;

View File

@ -1,4 +1,4 @@
use super::{Paragraph, Run, TableCellProperty}; use super::{Paragraph, TableCellProperty};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
@ -14,8 +14,8 @@ pub enum TableCellContent {
} }
impl TableCell { impl TableCell {
pub fn new(w: usize) -> TableCell { pub fn new() -> TableCell {
let property = TableCellProperty::new(w); let property = TableCellProperty::new();
let contents = vec![]; let contents = vec![];
Self { property, contents } Self { property, contents }
} }
@ -42,6 +42,7 @@ impl BuildXML for TableCell {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::super::*;
use super::*; use super::*;
#[cfg(test)] #[cfg(test)]
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
@ -49,21 +50,21 @@ mod tests {
#[test] #[test]
fn test_cell() { fn test_cell() {
let b = TableCell::new(200).build(); let b = TableCell::new().build();
assert_eq!( assert_eq!(
str::from_utf8(&b).unwrap(), str::from_utf8(&b).unwrap(),
r#"<w:tc><w:tcPr><w:tcW w:w="200" w:type="dxa" /><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tcBorders></w:tcPr></w:tc>"# r#"<w:tc><w:tcPr><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tcBorders></w:tcPr></w:tc>"#
); );
} }
#[test] #[test]
fn test_cell_add_p() { fn test_cell_add_p() {
let b = TableCell::new(200) let b = TableCell::new()
.add_paragraph(Paragraph::new().add_run(Run::new("Hello"))) .add_paragraph(Paragraph::new().add_run(Run::new("Hello")))
.build(); .build();
assert_eq!( assert_eq!(
str::from_utf8(&b).unwrap(), str::from_utf8(&b).unwrap(),
r#"<w:tc><w:tcPr><w:tcW w:w="200" w:type="dxa" /><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tcBorders></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>"# r#"<w:tc><w:tcPr><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tcBorders></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>"#
); );
} }
} }

View File

@ -1,33 +1,33 @@
use super::{TableCellBorders, TableCellWidth}; use super::TableCellBorders;
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::types::*; // use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TableCellProperty { pub struct TableCellProperty {
width: TableCellWidth, // width: TableCellWidth,
borders: TableCellBorders, borders: TableCellBorders,
} }
impl TableCellProperty { impl TableCellProperty {
pub fn new(w: usize) -> TableCellProperty { pub fn new() -> TableCellProperty {
TableCellProperty { TableCellProperty {
width: TableCellWidth::new(w, WidthType::DXA), // width: TableCellWidth::new(w, WidthType::DXA),
borders: TableCellBorders::new(), borders: TableCellBorders::new(),
} }
} }
pub fn width(mut self, v: usize) -> TableCellProperty { // pub fn width(mut self, v: usize) -> TableCellProperty {
self.width = TableCellWidth::new(v, WidthType::DXA); // self.width = TableCellWidth::new(v, WidthType::DXA);
self // self
} // }
} }
impl BuildXML for TableCellProperty { impl BuildXML for TableCellProperty {
fn build(&self) -> Vec<u8> { fn build(&self) -> Vec<u8> {
XMLBuilder::new() XMLBuilder::new()
.open_table_cell_property() .open_table_cell_property()
.add_child(&self.width) // .add_child(&self.width)
.add_child(&self.borders) .add_child(&self.borders)
.close() .close()
.build() .build()
@ -44,11 +44,11 @@ mod tests {
#[test] #[test]
fn test_default() { fn test_default() {
let c = TableCellProperty::new(200); let c = TableCellProperty::new();
let b = c.build(); let b = c.build();
assert_eq!( assert_eq!(
str::from_utf8(&b).unwrap(), str::from_utf8(&b).unwrap(),
r#"<w:tcPr><w:tcW w:w="200" w:type="dxa" /><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tcBorders></w:tcPr>"# r#"<w:tcPr><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tcBorders></w:tcPr>"#
); );
} }
} }

View File

@ -0,0 +1,44 @@
use super::{TableCell, TableRowProperty};
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct TableRow {
property: TableRowProperty,
cells: Vec<TableCell>,
}
impl TableRow {
pub fn new(cells: Vec<TableCell>) -> TableRow {
let property = TableRowProperty::new();
Self { property, cells }
}
}
impl BuildXML for TableRow {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new()
.open_table_row()
.add_child(&self.property)
.add_children(&self.cells);
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 = TableRow::new(vec![TableCell::new()]).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:tr><w:trPr /><w:tc><w:tcPr><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tcBorders></w:tcPr></w:tc></w:tr>"#
);
}
}

View File

@ -0,0 +1,32 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct TableRowProperty {}
impl TableRowProperty {
pub fn new() -> TableRowProperty {
TableRowProperty {}
}
}
impl BuildXML for TableRowProperty {
fn build(&self) -> Vec<u8> {
XMLBuilder::new().open_table_row_property().close().build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_default() {
let b = TableRowProperty::new().build();
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:trPr />"#);
}
}