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