edit_xlsx/api/
format.rs

1//!
2//! This module contains the [`Format`] struct, which used to edit the style of the [`Cell`],
3//! also defines some methods for working with [`Format`].
4//!
5pub use align::{FormatAlignType, FormatAlign};
6pub use border::{FormatBorder, FormatBorderElement, FormatBorderType};
7pub use color::FormatColor;
8pub use fill::FormatFill;
9pub use font::FormatFont;
10use crate::Cell;
11
12mod align;
13mod color;
14mod fill;
15mod font;
16mod border;
17
18///
19/// [`Format`] struct, which used to edit the style of the [`Cell`].
20/// # Fields
21/// | field        | type        | meaning                                                      |
22/// | ------------ | ----------- | ------------------------------------------------------------ |
23/// | `font`     | [`FormatFont`] | The [`Cell`]'s font formats |
24/// | `border`    | [`FormatBorder`] | The [`Cell`]'s border formats |
25/// | `fill`      | [`FormatFill`] | The [`Cell`]'s fill(background) formats |
26/// | `align`   | [`FormatAlign`] | The [`Cell`]'s align formats |
27#[derive(Default, Clone, Debug, PartialEq)]
28pub struct Format {
29    pub font: FormatFont,
30    pub border: FormatBorder,
31    pub fill: FormatFill,
32    pub align: FormatAlign,
33}
34
35impl Format {
36    /// Checks whether the font is bold, learn more about it in [`FormatFont`].
37    ///
38    /// ## Returns
39    ///
40    /// Returns `true` if the font is bold, `false` otherwise.
41    pub fn is_bold(&self) -> bool {
42        self.font.bold
43    }
44    /// Checks whether the font is italic, learn more about it in [`FormatFont`].
45    ///
46    /// ## Returns
47    ///
48    /// Returns `true` if the font is italic, `false` otherwise.
49
50    pub fn is_italic(&self) -> bool {
51        self.font.italic
52    }
53    /// Checks whether the font is underline, learn more about it in [`FormatFont`].
54    ///
55    /// ## Returns
56    ///
57    /// Returns `true` if the font is underline, `false` otherwise.
58
59    pub fn is_underline(&self) -> bool {
60        self.font.underline
61    }
62    /// Retrieves the size of the font, learn more about it in [`FormatFont`].
63    ///
64    /// ## Returns
65    ///
66    /// Returns the pounds of the font size.
67    pub fn get_size(&self) -> f64 {
68        self.font.size
69    }
70    /// Retrieves the background(fill) of the cell.
71    ///
72    /// ## Returns
73    ///
74    /// Returns the [`FormatFill`] of the cell.
75    pub fn get_background(&self) -> &FormatFill {
76        &self.fill
77    }
78
79    /// Retrieves the borders of the cell, learn more about it in [`FormatBorder`].
80    ///
81    /// ## Returns
82    ///
83    /// Returns the [`FormatBorder`] of the cell.
84    pub fn get_borders(&self) -> &FormatBorder {
85        &self.border
86    }
87    /// Retrieves the color of the font, learn more about it in [`FormatFont`].
88    ///
89    /// ## Returns
90    ///
91    /// Returns the [`FormatColor`] of the font.
92    pub fn get_color(&self) -> &FormatColor {
93        &self.font.color
94    }
95    /// Retrieves the name of the font, learn more about it in [`FormatFont`].
96    ///
97    /// ## Returns
98    ///
99    /// Returns the name of the font.
100    pub fn get_font(&self) -> &str {
101        &self.font.name
102    }
103
104    /// Retrieves the color of the cell's background.
105    ///
106    /// ## Returns
107    ///
108    /// Returns the [`FormatColor`] of the cell's background.
109    pub fn get_background_color(&self) -> &FormatColor {
110        &self.fill.fg_color
111    }
112
113    /// Retrieves the left border of the cell, learn more about it in [`FormatBorder`].
114    ///
115    /// ## Returns
116    ///
117    /// Returns the [`FormatBorderElement`] of the cell's left border.
118    pub fn get_border_left(&self) -> &FormatBorderElement {
119        &self.border.left
120    }
121    /// Retrieves the right border of the cell, learn more about it in [`FormatBorder`].
122    ///
123    /// ## Returns
124    ///
125    /// Returns the [`FormatBorderElement`] of the cell's right border.
126    pub fn get_border_right(&self) -> &FormatBorderElement {
127        &self.border.right
128    }
129    /// Retrieves the top border of the cell, learn more about it in [`FormatBorder`].
130    ///
131    /// ## Returns
132    ///
133    /// Returns the [`FormatBorderElement`] of the cell's top border.
134    pub fn get_border_top(&self) -> &FormatBorderElement {
135        &self.border.top
136    }
137    /// Retrieves the bottom border of the cell, learn more about it in [`FormatBorder`].
138    ///
139    /// ## Returns
140    ///
141    /// Returns the [`FormatBorderElement`] of the cell's bottom border.
142    pub fn get_border_bottom(&self) -> &FormatBorderElement {
143        &self.border.bottom
144    }
145}
146
147impl Format {
148    /// Set the font to bold, learn more about it in [`FormatFont`].
149    ///
150    /// ## Returns
151    ///
152    /// Returns the modified format with font bold set to true.
153    pub fn set_bold(mut self) -> Self {
154        self.font.bold = true;
155        self
156    }
157    /// Set the font to italic, learn more about it in [`FormatFont`].
158    ///
159    /// ## Returns
160    ///
161    /// Returns the modified format with font italic set to true.
162    pub fn set_italic(mut self) -> Self {
163        self.font.italic = true;
164        self
165    }
166    /// Add an underline for the font, learn more about it in [`FormatFont`].
167    ///
168    /// ## Returns
169    ///
170    /// Returns the modified format with font underline set to true.
171    pub fn set_underline(mut self) -> Self {
172        self.font.underline = true;
173        self
174    }
175    /// Set the size of the font, learn more about it in [`FormatFont`].
176    ///
177    /// ## Arguments
178    ///
179    /// | arg | type | meaning                                       |
180    /// |----------|------|-----------------------------------------------|
181    /// | size     | u8   | The new size of the font. |
182    ///
183    /// ## Returns
184    ///
185    /// Returns the modified Format with the size set to the provided value.
186    pub fn set_size(mut self, size: u8) -> Self {
187        self.font.size = size as f64;
188        self
189    }
190
191    /// Set the size of the font, learn more about it in [`FormatFont`].
192    ///
193    /// ## Arguments
194    ///
195    /// | arg | type | meaning                                       |
196    /// |----------|------|-----------------------------------------------|
197    /// | size     | f64   | The new size of the font. |
198    ///
199    /// ## Returns
200    ///
201    /// Returns the modified Format with the size set to the provided value.
202    pub fn set_size_f64(mut self, size: f64) -> Self {
203        self.font.size = size;
204        self
205    }
206
207    /// Set the color of the font, learn more about it in [`FormatFont`].
208    ///
209    /// ## Arguments
210    ///
211    /// | arg | type | meaning                                       |
212    /// |----------|------|-----------------------------------------------|
213    /// | format_color     | [`FormatColor`]   | The new color of the font. |
214    ///
215    /// ## Returns
216    ///
217    /// Returns the modified Format with the color set to the provided value.
218    pub fn set_color(mut self, format_color: FormatColor) -> Self {
219        self.font.color = format_color;
220        self
221    }
222
223    /// Set the font name, learn more about it in [`FormatFont`].
224    ///
225    /// ## Arguments
226    ///
227    /// | arg | type | meaning                                       |
228    /// |----------|------|-----------------------------------------------|
229    /// | size     | &str   | The new font name. |
230    ///
231    /// ## Returns
232    ///
233    /// Returns the modified Format with the name set to the provided value.
234    pub fn set_font(mut self, font_name: &str) -> Self {
235        self.font.name = font_name.to_string();
236        self
237    }
238
239    /// Set all borders of the format, learn more about it in [`FormatBorder`].
240    ///
241    /// ## Arguments
242    ///
243    /// | arg            | type                  | meaning                                       |
244    /// |---------------------|-----------------------|-----------------------------------------------|
245    /// | format_border_type | [`FormatBorderType`]      | The type of border to apply to borders of the format. |
246    ///
247    /// ## Returns
248    ///
249    /// Returns the modified Format with borders of the format set to the provided type.
250    pub fn set_border(mut self, format_border_type: FormatBorderType) -> Self {
251        let mut format_border = FormatBorderElement::default();
252        format_border.border_type = format_border_type;
253        self.border.left = format_border.clone();
254        self.border.right = format_border.clone();
255        self.border.top = format_border.clone();
256        self.border.bottom = format_border.clone();
257        self.border.diagonal = format_border;
258        self
259    }
260
261    /// Set the left border of the format, learn more about it in [`FormatBorder`].
262    ///
263    /// ## Arguments
264    ///
265    /// | arg            | type                  | meaning                                       |
266    /// |---------------------|-----------------------|-----------------------------------------------|
267    /// | format_border_type | [`FormatBorderType`]      | The type of border to apply to the left side of the format. |
268    ///
269    /// ## Returns
270    ///
271    /// Returns the modified Format with the left border of the format set to the provided type.
272    pub fn set_border_left(mut self, format_border_type: FormatBorderType) -> Self {
273        let mut format_border = FormatBorderElement::default();
274        format_border.border_type = format_border_type;
275        self.border.left = format_border;
276        self
277    }
278
279    /// Set the right border of the format, learn more about it in [`FormatBorder`].
280    ///
281    /// ## Arguments
282    ///
283    /// | arg            | type                  | meaning                                       |
284    /// |---------------------|-----------------------|-----------------------------------------------|
285    /// | format_border_type | [`FormatBorderType`]      | The type of border to apply to the right side of the format. |
286    ///
287    /// ## Returns
288    ///
289    /// Returns the modified Format with the right border of the format set to the provided type.
290    pub fn set_border_right(mut self, format_border_type: FormatBorderType) -> Self {
291        let mut format_border = FormatBorderElement::default();
292        format_border.border_type = format_border_type;
293        self.border.right = format_border;
294        self
295    }
296
297    /// Set the top border of the format, learn more about it in [`FormatBorder`].
298    ///
299    /// ## Arguments
300    ///
301    /// | arg            | type                  | meaning                                       |
302    /// |---------------------|-----------------------|-----------------------------------------------|
303    /// | format_border_type | [`FormatBorderType`]      | The type of border to apply to the top side of the format. |
304    ///
305    /// ## Returns
306    ///
307    /// Returns the modified Format with the top border of the format set to the provided type.
308    pub fn set_border_top(mut self, format_border_type: FormatBorderType) -> Self {
309        let mut format_border = FormatBorderElement::default();
310        format_border.border_type = format_border_type;
311        self.border.top = format_border;
312        self
313    }
314    /// Set the bottom border of the format, learn more about it in [`FormatBorder`].
315    ///
316    /// ## Arguments
317    ///
318    /// | arg            | type                  | meaning                                       |
319    /// |---------------------|-----------------------|-----------------------------------------------|
320    /// | format_border_type | [`FormatBorderType`]      | The type of border to apply to the bottom side of the format. |
321    ///
322    /// ## Returns
323    ///
324    /// Returns the modified Format with the bottom border of the format set to the provided type.
325    pub fn set_border_bottom(mut self, format_border_type: FormatBorderType) -> Self {
326        let mut format_border = FormatBorderElement::default();
327        format_border.border_type = format_border_type;
328        self.border.bottom = format_border;
329        self
330    }
331    
332    /// Set the background color of the format.
333    ///
334    /// ## Arguments
335    ///
336    /// | arg       | type         | meaning                                       |
337    /// |----------------|--------------|-----------------------------------------------|
338    /// | format_color   | [`FormatColor`]  | The color to set as the background of the format. |
339    ///
340    /// ## Returns
341    ///
342    /// Returns the modified Format with the background color of the format set to the provided color.
343    pub fn set_background_color(mut self, format_color: FormatColor) -> Self {
344        self.fill.pattern_type = "solid".to_string();
345        self.fill.fg_color = format_color;
346        self
347    }
348
349    /// Set the alignment of the format, learn more about it in [`FormatAlign`].
350    ///
351    /// ## Arguments
352    ///
353    /// | arg             | type                  | meaning                                       |
354    /// |----------------------|-----------------------|-----------------------------------------------|
355    /// | format_align_type    | [`FormatAlignType`]       | The type of alignment to apply to the format. |
356    ///
357    /// ## Returns
358    ///
359    /// Returns the modified Format with the alignment of the format set to the provided type.
360    pub fn set_align(mut self, format_align_type: FormatAlignType) -> Self {
361        match format_align_type {
362            FormatAlignType::Left | FormatAlignType::Center | FormatAlignType::Right =>
363                self.align.horizontal = Some(format_align_type),
364            FormatAlignType::Top | FormatAlignType::VerticalCenter | FormatAlignType::Bottom =>
365                self.align.vertical = Some(format_align_type),
366        }
367        self
368    }
369
370    /// Set the indent of the text alignment, learn more about it in [`FormatAlign`].
371    ///
372    /// ## Arguments
373    ///
374    /// | arg | type | meaning                                       |
375    /// |----------|------|-----------------------------------------------|
376    /// | indent   | u8   | The new reading order of the text alignment. |
377    ///
378    /// ## Returns
379    ///
380    /// Returns the modified Format with the reading order of the text alignment set to the provided value.
381    pub fn set_reading_order(mut self, reading_order: u8) -> Self {
382        self.align.reading_order = reading_order;
383        self
384    }
385
386    /// Set the indent of the text alignment, learn more about it in [`FormatAlign`].
387    ///
388    /// ## Arguments
389    ///
390    /// | arg | type | meaning                                       |
391    /// |----------|------|-----------------------------------------------|
392    /// | indent   | u8   | The new indent of the text alignment. |
393    ///
394    /// ## Returns
395    ///
396    /// Returns the modified Format with the indent of the text alignment set to the provided value.
397    pub fn set_indent(mut self, indent: u8) -> Self {
398        self.align.indent = indent;
399        self
400    }
401}