docx_rs/documents/elements/
paragraph_borders.rs1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::types::*;
6use crate::xml_builder::*;
7
8#[derive(Serialize, Debug, Clone, PartialEq)]
9#[serde(rename_all = "camelCase")]
10pub struct ParagraphBorder {
11 position: ParagraphBorderPosition,
12 pub val: BorderType,
13 pub size: usize,
14 pub space: usize,
15 pub color: String,
16 }
22
23impl ParagraphBorder {
24 pub fn new(position: ParagraphBorderPosition) -> Self {
25 ParagraphBorder {
26 position,
27 val: BorderType::Single,
28 size: 2,
29 space: 0,
30 color: "auto".to_owned(),
31 }
37 }
38 pub fn val(mut self, val: BorderType) -> Self {
39 self.val = val;
40 self
41 }
42
43 pub fn size(mut self, size: usize) -> Self {
44 self.size = size;
45 self
46 }
47
48 pub fn space(mut self, space: usize) -> Self {
49 self.space = space;
50 self
51 }
52
53 pub fn color(mut self, color: impl Into<String>) -> Self {
54 self.color = color.into();
55 self
56 }
57
58 }
83
84impl BuildXML for ParagraphBorder {
85 fn build_to<W: Write>(
86 &self,
87 stream: xml::writer::EventWriter<W>,
88 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
89 let val = self.val.to_string();
90 let space = self.space.to_string();
91 let size = self.size.to_string();
92 let func = match self.position {
93 ParagraphBorderPosition::Top => XMLBuilder::paragraph_border_top,
94 ParagraphBorderPosition::Left => XMLBuilder::paragraph_border_left,
95 ParagraphBorderPosition::Bottom => XMLBuilder::paragraph_border_bottom,
96 ParagraphBorderPosition::Right => XMLBuilder::paragraph_border_right,
97 ParagraphBorderPosition::Between => XMLBuilder::paragraph_border_between,
98 ParagraphBorderPosition::Bar => XMLBuilder::paragraph_border_bar,
99 };
100 XMLBuilder::from(stream)
101 .apply(|b| func(b, &val, &space, &size, &self.color))?
102 .into_inner()
103 }
104}
105
106#[derive(Serialize, Debug, Clone, PartialEq)]
107#[serde(rename_all = "camelCase")]
108pub struct ParagraphBorders {
109 left: Option<ParagraphBorder>,
110 right: Option<ParagraphBorder>,
111 top: Option<ParagraphBorder>,
112 bottom: Option<ParagraphBorder>,
113 between: Option<ParagraphBorder>,
114 bar: Option<ParagraphBorder>,
115}
116
117impl Default for ParagraphBorders {
118 fn default() -> Self {
119 ParagraphBorders {
120 left: Some(ParagraphBorder::new(ParagraphBorderPosition::Left)),
121 right: Some(ParagraphBorder::new(ParagraphBorderPosition::Right)),
122 top: Some(ParagraphBorder::new(ParagraphBorderPosition::Top)),
123 bottom: Some(ParagraphBorder::new(ParagraphBorderPosition::Bottom)),
124 between: None,
125 bar: None,
126 }
127 }
128}
129
130impl ParagraphBorders {
131 pub fn new() -> Self {
132 Self::default()
133 }
134
135 pub fn with_empty() -> Self {
136 ParagraphBorders {
137 left: None,
138 right: None,
139 top: None,
140 bottom: None,
141 between: None,
142 bar: None,
143 }
144 }
145
146 pub fn set(mut self, border: ParagraphBorder) -> Self {
147 match border.position {
148 ParagraphBorderPosition::Top => self.top = Some(border),
149 ParagraphBorderPosition::Left => self.left = Some(border),
150 ParagraphBorderPosition::Bottom => self.bottom = Some(border),
151 ParagraphBorderPosition::Right => self.right = Some(border),
152 ParagraphBorderPosition::Between => self.between = Some(border),
153 ParagraphBorderPosition::Bar => self.bar = Some(border),
154 };
155 self
156 }
157
158 pub fn clear(mut self, position: ParagraphBorderPosition) -> Self {
159 let nil = ParagraphBorder::new(position.clone()).val(BorderType::Nil);
160 match position {
161 ParagraphBorderPosition::Top => self.top = Some(nil),
162 ParagraphBorderPosition::Left => self.left = Some(nil),
163 ParagraphBorderPosition::Bottom => self.bottom = Some(nil),
164 ParagraphBorderPosition::Right => self.right = Some(nil),
165 ParagraphBorderPosition::Between => self.between = Some(nil),
166 ParagraphBorderPosition::Bar => self.bar = Some(nil),
167 };
168 self
169 }
170
171 pub fn clear_all(mut self) -> Self {
172 self.left = Some(ParagraphBorder::new(ParagraphBorderPosition::Left).val(BorderType::Nil));
173 self.right =
174 Some(ParagraphBorder::new(ParagraphBorderPosition::Right).val(BorderType::Nil));
175 self.top = Some(ParagraphBorder::new(ParagraphBorderPosition::Top).val(BorderType::Nil));
176 self.bottom =
177 Some(ParagraphBorder::new(ParagraphBorderPosition::Bottom).val(BorderType::Nil));
178 self.between =
179 Some(ParagraphBorder::new(ParagraphBorderPosition::Between).val(BorderType::Nil));
180 self.bar = Some(ParagraphBorder::new(ParagraphBorderPosition::Bar).val(BorderType::Nil));
181 self
182 }
183}
184
185impl BuildXML for ParagraphBorders {
186 fn build_to<W: Write>(
187 &self,
188 stream: xml::writer::EventWriter<W>,
189 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
190 XMLBuilder::from(stream)
191 .open_paragraph_borders()?
192 .add_optional_child(&self.left)?
193 .add_optional_child(&self.right)?
194 .add_optional_child(&self.top)?
195 .add_optional_child(&self.bottom)?
196 .add_optional_child(&self.between)?
197 .add_optional_child(&self.bar)?
198 .close()?
199 .into_inner()
200 }
201}