docx_rs/documents/elements/
table_cell_margins.rs1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::types::*;
6use crate::xml_builder::*;
7use crate::CellMargin;
8
9#[derive(Debug, Clone, PartialEq, Serialize)]
10#[serde(rename_all = "camelCase")]
11pub struct TableCellMargins {
12 top: CellMargin,
13 left: CellMargin,
14 bottom: CellMargin,
15 right: CellMargin,
16}
17
18impl Default for TableCellMargins {
19 fn default() -> TableCellMargins {
20 TableCellMargins {
21 top: CellMargin {
22 val: 0,
23 width_type: WidthType::Dxa,
24 },
25 left: CellMargin::default(),
26 bottom: CellMargin {
27 val: 0,
28 width_type: WidthType::Dxa,
29 },
30 right: CellMargin::default(),
31 }
32 }
33}
34
35impl TableCellMargins {
36 pub fn new() -> TableCellMargins {
37 Default::default()
38 }
39
40 pub fn margin(self, top: usize, right: usize, bottom: usize, left: usize) -> TableCellMargins {
41 TableCellMargins {
42 top: CellMargin::new(top, WidthType::Dxa),
43 left: CellMargin::new(left, WidthType::Dxa),
44 bottom: CellMargin::new(bottom, WidthType::Dxa),
45 right: CellMargin::new(right, WidthType::Dxa),
46 }
47 }
48
49 pub fn margin_top(mut self, v: usize, t: WidthType) -> Self {
50 self.top = CellMargin::new(v, t);
51 self
52 }
53
54 pub fn margin_right(mut self, v: usize, t: WidthType) -> Self {
55 self.right = CellMargin::new(v, t);
56 self
57 }
58
59 pub fn margin_left(mut self, v: usize, t: WidthType) -> Self {
60 self.left = CellMargin::new(v, t);
61 self
62 }
63
64 pub fn margin_bottom(mut self, v: usize, t: WidthType) -> Self {
65 self.bottom = CellMargin::new(v, t);
66 self
67 }
68}
69
70impl BuildXML for TableCellMargins {
71 fn build_to<W: Write>(
72 &self,
73 stream: xml::writer::EventWriter<W>,
74 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
75 XMLBuilder::from(stream)
76 .open_table_cell_margins()?
77 .margin_top(self.top.val as i32, self.top.width_type)?
78 .margin_left(self.left.val as i32, self.left.width_type)?
79 .margin_bottom(self.bottom.val as i32, self.bottom.width_type)?
80 .margin_right(self.right.val as i32, self.right.width_type)?
81 .close()?
82 .into_inner()
83 }
84}
85
86#[cfg(test)]
87mod tests {
88
89 use super::*;
90 #[cfg(test)]
91 use pretty_assertions::assert_eq;
92 use std::str;
93
94 #[test]
95 fn test_table_cell_margin() {
96 let b = TableCellMargins::new().build();
97 assert_eq!(
98 str::from_utf8(&b).unwrap(),
99 r#"<w:tblCellMar><w:top w:w="0" w:type="dxa" /><w:left w:w="55" w:type="dxa" /><w:bottom w:w="0" w:type="dxa" /><w:right w:w="55" w:type="dxa" /></w:tblCellMar>"#
100 );
101 }
102
103 #[test]
104 fn test_table_cell_margin_setter() {
105 let b = TableCellMargins::new().margin(10, 20, 30, 40).build();
106 assert_eq!(
107 str::from_utf8(&b).unwrap(),
108 r#"<w:tblCellMar><w:top w:w="10" w:type="dxa" /><w:left w:w="40" w:type="dxa" /><w:bottom w:w="30" w:type="dxa" /><w:right w:w="20" w:type="dxa" /></w:tblCellMar>"#
109 );
110 }
111}