feat: Add cell

main
bokuweb 2019-11-12 18:57:16 +09:00
parent e387c29edf
commit 23b422c25f
13 changed files with 82 additions and 11 deletions

View File

@ -1,7 +1,7 @@
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Color { pub struct Color {
val: String, val: String,
} }

View File

@ -2,7 +2,7 @@ use crate::documents::BuildXML;
use crate::types::*; use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Indent { pub struct Indent {
left: usize, left: usize,
special_indent: Option<SpecialIndentType>, special_indent: Option<SpecialIndentType>,

View File

@ -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 // 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 // of mathematical text can be aligned with respect to each other, but the entire group of mathematical text is
// centered as a whole. // centered as a whole.
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Justification { pub struct Justification {
val: String, val: String,
} }

View File

@ -16,6 +16,7 @@ mod style;
mod sz; mod sz;
mod sz_cs; mod sz_cs;
mod table_borders; mod table_borders;
mod table_cell;
mod table_cell_borders; mod table_cell_borders;
mod table_cell_margins; mod table_cell_margins;
mod table_cell_property; mod table_cell_property;
@ -44,6 +45,7 @@ pub use style::*;
pub use sz::*; pub use sz::*;
pub use sz_cs::*; pub use sz_cs::*;
pub use table_borders::*; pub use table_borders::*;
pub use table_cell::*;
pub use table_cell_borders::*; pub use table_cell_borders::*;
pub use table_cell_margins::*; pub use table_cell_margins::*;
pub use table_cell_property::*; pub use table_cell_property::*;

View File

@ -3,7 +3,7 @@ use crate::documents::BuildXML;
use crate::types::*; use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Paragraph { pub struct Paragraph {
runs: Vec<Run>, runs: Vec<Run>,
property: ParagraphProperty, property: ParagraphProperty,

View File

@ -3,7 +3,7 @@ use crate::documents::BuildXML;
use crate::types::{AlignmentType, SpecialIndentType}; use crate::types::{AlignmentType, SpecialIndentType};
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct ParagraphProperty { pub struct ParagraphProperty {
run_property: RunProperty, run_property: RunProperty,
style: ParagraphStyle, style: ParagraphStyle,

View File

@ -1,7 +1,7 @@
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct ParagraphStyle { pub struct ParagraphStyle {
val: String, val: String,
} }

View File

@ -2,7 +2,7 @@ use super::{RunProperty, Text};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Run { pub struct Run {
run_property: RunProperty, run_property: RunProperty,
text: Text, text: Text,

View File

@ -2,7 +2,7 @@ use super::{Color, Sz, SzCs};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct RunProperty { pub struct RunProperty {
sz: Option<Sz>, sz: Option<Sz>,
sz_cs: Option<SzCs>, sz_cs: Option<SzCs>,

View File

@ -1,7 +1,7 @@
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Sz { pub struct Sz {
val: usize, val: usize,
} }

View File

@ -1,7 +1,7 @@
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct SzCs { pub struct SzCs {
val: usize, val: usize,
} }

View File

@ -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<TableCellContent>,
}
#[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<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::*;
#[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#"<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>"#
);
}
#[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#"<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>"#
);
}
}

View File

@ -3,7 +3,7 @@ use crate::documents::BuildXML;
use crate::types::*; use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct TableCellProperty { pub struct TableCellProperty {
width: TableCellWidth, width: TableCellWidth,
borders: TableCellBorders, borders: TableCellBorders,