1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
9pub struct CellStyle {
10 pub font_color: Option<String>,
12 pub bg_color: Option<String>,
14 pub bold: Option<bool>,
16 pub italic: Option<bool>,
18 pub underline: Option<bool>,
20 pub font_family: Option<String>,
22 pub font_size: Option<f64>,
28 pub num_format: Option<String>,
34}
35
36impl CellStyle {
37 pub fn new() -> Self {
39 Self::default()
40 }
41
42 pub fn is_empty(&self) -> bool {
45 self.font_color.is_none()
46 && self.bg_color.is_none()
47 && self.bold.is_none()
48 && self.italic.is_none()
49 && self.underline.is_none()
50 && self.font_family.is_none()
51 && self.font_size.is_none()
52 && self.num_format.is_none()
53 }
54
55 pub fn merge(&mut self, other: &CellStyle) {
60 if other.font_color.is_some() {
61 self.font_color = other.font_color.clone();
62 }
63 if other.bg_color.is_some() {
64 self.bg_color = other.bg_color.clone();
65 }
66 if other.bold.is_some() {
67 self.bold = other.bold;
68 }
69 if other.italic.is_some() {
70 self.italic = other.italic;
71 }
72 if other.underline.is_some() {
73 self.underline = other.underline;
74 }
75 if other.font_family.is_some() {
76 self.font_family = other.font_family.clone();
77 }
78 if other.font_size.is_some() {
79 self.font_size = other.font_size;
80 }
81 if other.num_format.is_some() {
82 self.num_format = other.num_format.clone();
83 }
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn test_cell_style_is_empty() {
93 let style = CellStyle::new();
94 assert!(style.is_empty());
95
96 let style_bold = CellStyle {
97 bold: Some(true),
98 ..Default::default()
99 };
100 assert!(!style_bold.is_empty());
101 }
102
103 #[test]
104 fn test_cell_style_merge() {
105 let mut style1 = CellStyle {
106 font_color: Some("#FF0000".to_string()),
107 bold: Some(true),
108 ..Default::default()
109 };
110 let style2 = CellStyle {
111 bg_color: Some("#00FF00".to_string()),
112 font_color: Some("#0000FF".to_string()),
113 font_size: Some(14.0),
114 ..Default::default()
115 };
116
117 style1.merge(&style2);
118 assert_eq!(style1.font_color, Some("#0000FF".to_string()));
119 assert_eq!(style1.bg_color, Some("#00FF00".to_string()));
120 assert_eq!(style1.bold, Some(true));
121 assert_eq!(style1.font_size, Some(14.0));
122 }
123}