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
use serde::Serialize;

use crate::documents::BuildXML;
use crate::types::*;
use crate::xml_builder::*;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TableCellMargins {
    top: usize,
    left: usize,
    bottom: usize,
    right: usize,
}

impl Default for TableCellMargins {
    fn default() -> TableCellMargins {
        TableCellMargins {
            top: 55,
            left: 54,
            bottom: 55,
            right: 55,
        }
    }
}

impl TableCellMargins {
    pub fn new() -> TableCellMargins {
        Default::default()
    }

    pub fn margin(self, top: usize, left: usize, bottom: usize, right: usize) -> TableCellMargins {
        TableCellMargins {
            top,
            left,
            bottom,
            right,
        }
    }
}

impl BuildXML for TableCellMargins {
    fn build(&self) -> Vec<u8> {
        XMLBuilder::new()
            .open_table_cell_margins()
            .margin_top(self.top, WidthType::DXA)
            .margin_left(self.left, WidthType::DXA)
            .margin_bottom(self.bottom, WidthType::DXA)
            .margin_right(self.right, WidthType::DXA)
            .close()
            .build()
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    #[cfg(test)]
    use pretty_assertions::assert_eq;
    use std::str;

    #[test]
    fn test_table_cell_margin() {
        let b = TableCellMargins::new().build();
        assert_eq!(
            str::from_utf8(&b).unwrap(),
            r#"<w:tblCellMar>
  <w:top w:w="55" w:type="dxa" />
  <w:left w:w="54" w:type="dxa" />
  <w:bottom w:w="55" w:type="dxa" />
  <w:right w:w="55" w:type="dxa" />
</w:tblCellMar>"#
        );
    }

    #[test]
    fn test_table_cell_margin_setter() {
        let b = TableCellMargins::new().margin(10, 20, 30, 40).build();
        assert_eq!(
            str::from_utf8(&b).unwrap(),
            r#"<w:tblCellMar>
  <w:top w:w="10" w:type="dxa" />
  <w:left w:w="20" w:type="dxa" />
  <w:bottom w:w="30" w:type="dxa" />
  <w:right w:w="40" w:type="dxa" />
</w:tblCellMar>"#
        );
    }
}