docx-rs/docx-core/src/documents/elements/table_row.rs

51 lines
1.1 KiB
Rust
Raw Normal View History

2019-11-12 12:11:58 +02:00
use super::{TableCell, TableRowProperty};
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct TableRow {
pub cells: Vec<TableCell>,
pub(crate) has_numbering: bool,
2019-11-12 12:11:58 +02:00
property: TableRowProperty,
}
impl TableRow {
pub fn new(cells: Vec<TableCell>) -> TableRow {
2019-11-12 12:11:58 +02:00
let property = TableRowProperty::new();
let has_numbering = cells.iter().any(|c| c.has_numbering);
Self {
property,
cells,
has_numbering,
}
2019-11-12 12:11:58 +02:00
}
}
impl BuildXML for TableRow {
2019-11-12 12:11:58 +02:00
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(),
2019-11-13 06:55:58 +02:00
r#"<w:tr><w:trPr /><w:tc><w:tcPr /></w:tc></w:tr>"#
2019-11-12 12:11:58 +02:00
);
}
}