fix: tableRow and cell json type (#418)

main
bokuweb 2022-01-24 22:48:01 +09:00 committed by GitHub
parent 515812ffc8
commit c036c0e6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 3967 additions and 3616 deletions

View File

@ -1,3 +1,4 @@
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use super::*;
@ -8,17 +9,31 @@ use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Table {
pub rows: Vec<TableRow>,
pub rows: Vec<TableChild>,
pub grid: Vec<usize>,
pub has_numbering: bool,
pub property: TableProperty,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TableChild {
TableRow(TableRow),
}
impl BuildXML for TableChild {
fn build(&self) -> Vec<u8> {
match self {
TableChild::TableRow(v) => v.build(),
}
}
}
impl Table {
pub fn new(rows: Vec<TableRow>) -> Table {
let property = TableProperty::new();
let has_numbering = rows.iter().any(|c| c.has_numbering);
let grid = vec![];
let rows = rows.into_iter().map(TableChild::TableRow).collect();
Self {
property,
rows,
@ -31,6 +46,7 @@ impl Table {
let property = TableProperty::without_borders();
let has_numbering = rows.iter().any(|c| c.has_numbering);
let grid = vec![];
let rows = rows.into_iter().map(TableChild::TableRow).collect();
Self {
property,
rows,
@ -40,7 +56,7 @@ impl Table {
}
pub fn add_row(mut self, row: TableRow) -> Table {
self.rows.push(row);
self.rows.push(TableChild::TableRow(row));
self
}
@ -112,6 +128,22 @@ impl BuildXML for Table {
}
}
impl Serialize for TableChild {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
TableChild::TableRow(ref r) => {
let mut t = serializer.serialize_struct("TableRow", 2)?;
t.serialize_field("type", "tableRow")?;
t.serialize_field("data", r)?;
t.end()
}
}
}
}
#[cfg(test)]
mod tests {

View File

@ -1,3 +1,4 @@
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use super::{TableCell, TableRowProperty};
@ -7,15 +8,29 @@ use crate::{documents::BuildXML, HeightRule};
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TableRow {
pub cells: Vec<TableCell>,
pub cells: Vec<TableRowChild>,
pub has_numbering: bool,
pub property: TableRowProperty,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TableRowChild {
TableCell(TableCell),
}
impl BuildXML for TableRowChild {
fn build(&self) -> Vec<u8> {
match self {
TableRowChild::TableCell(v) => v.build(),
}
}
}
impl TableRow {
pub fn new(cells: Vec<TableCell>) -> TableRow {
let property = TableRowProperty::new();
let has_numbering = cells.iter().any(|c| c.has_numbering);
let cells = cells.into_iter().map(TableRowChild::TableCell).collect();
Self {
cells,
property,
@ -64,6 +79,22 @@ impl BuildXML for TableRow {
}
}
impl Serialize for TableRowChild {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
TableRowChild::TableCell(ref r) => {
let mut t = serializer.serialize_struct("TableCell", 2)?;
t.serialize_field("type", "tableCell")?;
t.serialize_field("data", r)?;
t.end()
}
}
}
}
#[cfg(test)]
mod tests {
@ -86,7 +117,7 @@ mod tests {
let r = TableRow::new(vec![TableCell::new()]);
assert_eq!(
serde_json::to_string(&r).unwrap(),
r#"{"cells":[{"children":[],"property":{"width":null,"borders":null,"gridSpan":null,"verticalMerge":null,"verticalAlign":null,"textDirection":null,"shading":null},"hasNumbering":false}],"hasNumbering":false,"property":{"gridAfter":null,"widthAfter":null,"gridBefore":null,"widthBefore":null,"rowHeight":null,"heightRule":null}}"#
r#"{"cells":[{"type":"tableCell","data":{"children":[],"property":{"width":null,"borders":null,"gridSpan":null,"verticalMerge":null,"verticalAlign":null,"textDirection":null,"shading":null},"hasNumbering":false}}],"hasNumbering":false,"property":{"gridAfter":null,"widthAfter":null,"gridBefore":null,"widthBefore":null,"rowHeight":null,"heightRule":null}}"#
);
}
}

View File

@ -586,8 +586,8 @@ impl Docx {
}
}
DocumentChild::Table(table) => {
for row in &table.rows {
for cell in &row.cells {
for TableChild::TableRow(row) in &table.rows {
for TableRowChild::TableCell(cell) in &row.cells {
for content in &cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {
@ -646,8 +646,8 @@ impl Docx {
}
}
DocumentChild::Table(table) => {
for row in &table.rows {
for cell in &row.cells {
for TableChild::TableRow(row) in &table.rows {
for TableRowChild::TableCell(cell) in &row.cells {
for content in &cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {
@ -739,8 +739,8 @@ impl Docx {
}
}
DocumentChild::Table(table) => {
for row in &mut table.rows {
for cell in &mut row.cells {
for TableChild::TableRow(row) in &mut table.rows {
for TableRowChild::TableCell(cell) in &mut row.cells {
for content in &mut cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {
@ -828,8 +828,8 @@ impl Docx {
}
}
DocumentChild::Table(table) => {
for row in &mut table.rows {
for cell in &mut row.cells {
for TableChild::TableRow(row) in &mut table.rows {
for TableRowChild::TableCell(cell) in &mut row.cells {
for content in &mut cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -36,13 +36,19 @@ export type TableRowPropertyJSON = {
};
export type TableCellJSON = {
children: TableCellChildJSON[];
property: TableCellPropertyJSON;
type: "tableCell";
data: {
children: TableCellChildJSON[];
property: TableCellPropertyJSON;
};
};
export type TableRowJSON = {
cells: TableCellJSON[];
property: TableRowPropertyJSON;
type: "tableRow";
data: {
cells: TableCellJSON[];
property: TableRowPropertyJSON;
};
};
export type TableCellMarginJSON = { val: number; widthType: WidthType };

View File

@ -20,7 +20,7 @@ impl Table {
#[wasm_bindgen]
impl Table {
pub fn add_row(mut self, row: TableRow) -> Table {
self.0.rows.push(row.take());
self.0.rows.push(docx_rs::TableChild::TableRow(row.take()));
self
}

View File

@ -19,7 +19,7 @@ impl TableRow {
#[wasm_bindgen]
impl TableRow {
pub fn add_cell(mut self, cell: TableCell) -> TableRow {
self.0.cells.push(cell.take());
self.0.cells.push(docx_rs::TableRowChild::TableCell(cell.take()));
self
}

File diff suppressed because it is too large Load Diff