docx_rs/documents/elements/
table_row_property.rs

1use serde::Serialize;
2use std::io::Write;
3
4use super::*;
5use crate::xml_builder::*;
6use crate::{documents::BuildXML, HeightRule};
7
8#[derive(Debug, Clone, PartialEq, Serialize, Default)]
9#[serde(rename_all = "camelCase")]
10pub struct TableRowProperty {
11    grid_after: Option<u32>,
12    width_after: Option<f32>,
13    grid_before: Option<u32>,
14    width_before: Option<f32>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    row_height: Option<f32>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    height_rule: Option<HeightRule>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub del: Option<Delete>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub ins: Option<Insert>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub cant_split: Option<CantSplit>,
25}
26
27impl TableRowProperty {
28    pub fn new() -> TableRowProperty {
29        Default::default()
30    }
31
32    pub fn grid_after(mut self, after: u32) -> Self {
33        self.grid_after = Some(after);
34        self
35    }
36
37    pub fn width_after(mut self, w: f32) -> Self {
38        self.width_after = Some(w);
39        self
40    }
41
42    pub fn grid_before(mut self, before: u32) -> Self {
43        self.grid_before = Some(before);
44        self
45    }
46
47    pub fn width_before(mut self, w: f32) -> Self {
48        self.width_before = Some(w);
49        self
50    }
51
52    pub fn row_height(mut self, h: f32) -> Self {
53        self.row_height = Some(h);
54        self
55    }
56
57    pub fn height_rule(mut self, r: HeightRule) -> Self {
58        self.height_rule = Some(r);
59        self
60    }
61
62    pub fn delete(mut self, d: Delete) -> Self {
63        self.del = Some(d);
64        self
65    }
66
67    pub fn insert(mut self, i: Insert) -> Self {
68        self.ins = Some(i);
69        self
70    }
71
72    pub fn cant_split(mut self) -> Self {
73        self.cant_split = Some(CantSplit::default());
74        self
75    }
76}
77
78impl BuildXML for TableRowProperty {
79    fn build_to<W: Write>(
80        &self,
81        stream: xml::writer::EventWriter<W>,
82    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
83        XMLBuilder::from(stream)
84            .open_table_row_property()?
85            .add_optional_child(&self.del)?
86            .add_optional_child(&self.ins)?
87            .add_optional_child(&self.cant_split)?
88            .apply_opt(self.row_height, |h, b| {
89                b.table_row_height(
90                    &format!("{}", h),
91                    &self.height_rule.unwrap_or_default().to_string(),
92                )
93            })?
94            .close()?
95            .into_inner()
96    }
97}
98
99#[cfg(test)]
100mod tests {
101
102    use super::*;
103    #[cfg(test)]
104    use pretty_assertions::assert_eq;
105    use std::str;
106
107    #[test]
108    fn test_default() {
109        let b = TableRowProperty::new().build();
110        assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:trPr />"#);
111    }
112
113    #[test]
114    fn test_cant_split() {
115        let b = TableRowProperty::new().cant_split().build();
116        assert_eq!(
117            str::from_utf8(&b).unwrap(),
118            r#"<w:trPr><w:cantSplit /></w:trPr>"#
119        );
120    }
121}