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
#![allow(unused_must_use)]
use std::borrow::Cow;

use super::SDT;

use strong_xml::{XmlRead, XmlWrite};

use crate::{__setter, __xml_test_suites, document::TableCell, formatting::TableRowProperty};

/// Table Row
///
/// ```rust
/// use docx_rust::document::*;
/// use docx_rust::formatting::*;
///
/// let row = TableRow::default()
///     .property(TableRowProperty::default())
///     .push_cell(Paragraph::default())
///     .push_cell(
///         TableCell::paragraph(Paragraph::default())
///             .property(TableCellProperty::default())
///     );
/// ```
#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:tr")]
pub struct TableRow<'a> {
    #[xml(default, child = "w:trPr")]
    pub property: TableRowProperty,
    #[xml(child = "w:tc", child = "w:sdt")]
    pub cells: Vec<TableRowContent<'a>>,
}

#[derive(Debug, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum TableRowContent<'a> {
    #[xml(tag = "w:tc")]
    TableCell(TableCell<'a>),
    #[xml(tag = "w:sdt")]
    SDT(SDT<'a>),
}

impl<'a> From<TableCell<'a>> for TableRowContent<'a> {
    fn from(value: TableCell<'a>) -> Self {
        TableRowContent::TableCell(value)
    }
}

impl<'a> From<crate::document::Paragraph<'a>> for TableRowContent<'a> {
    fn from(value: crate::document::Paragraph<'a>) -> Self {
        let tc = TableCell::paragraph(value);
        TableRowContent::TableCell(tc)
    }
}

impl<'a> TableRow<'a> {
    __setter!(property: TableRowProperty);

    pub fn push_cell<T: Into<TableRowContent<'a>>>(mut self, cell: T) -> Self {
        self.cells.push(cell.into());
        self
    }

    pub fn iter_text(&self) -> impl Iterator<Item = &Cow<'a, str>> {
        self.cells
            .iter()
            .filter_map(|content| match content {
                //Some(content.iter_text())
                TableRowContent::TableCell(tc) => Some(tc.iter_text()),
                TableRowContent::SDT(_) => None,
            })
            .flatten()
    }

    pub fn iter_text_mut(&mut self) -> impl Iterator<Item = &mut Cow<'a, str>> {
        self.cells
            .iter_mut()
            .filter_map(|content| match content {
                //Some(content.iter_text_mut())
                TableRowContent::TableCell(tc) => Some(tc.iter_text_mut()),
                TableRowContent::SDT(_) => None,
            })
            .flatten()
    }
}

#[cfg(test)]
use crate::document::Paragraph;

__xml_test_suites!(
    TableRow,
    TableRow::default(),
    "<w:tr><w:trPr/></w:tr>",
    TableRow::default().push_cell(Paragraph::default()),
    "<w:tr><w:trPr/><w:tc><w:tcPr/><w:p/></w:tc></w:tr>",
);