use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct CellStyle {
pub font_color: Option<String>,
pub bg_color: Option<String>,
pub bold: Option<bool>,
pub italic: Option<bool>,
pub underline: Option<bool>,
pub font_family: Option<String>,
pub font_size: Option<f64>,
pub num_format: Option<String>,
}
impl CellStyle {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.font_color.is_none()
&& self.bg_color.is_none()
&& self.bold.is_none()
&& self.italic.is_none()
&& self.underline.is_none()
&& self.font_family.is_none()
&& self.font_size.is_none()
&& self.num_format.is_none()
}
pub fn merge(&mut self, other: &CellStyle) {
if other.font_color.is_some() {
self.font_color = other.font_color.clone();
}
if other.bg_color.is_some() {
self.bg_color = other.bg_color.clone();
}
if other.bold.is_some() {
self.bold = other.bold;
}
if other.italic.is_some() {
self.italic = other.italic;
}
if other.underline.is_some() {
self.underline = other.underline;
}
if other.font_family.is_some() {
self.font_family = other.font_family.clone();
}
if other.font_size.is_some() {
self.font_size = other.font_size;
}
if other.num_format.is_some() {
self.num_format = other.num_format.clone();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cell_style_is_empty() {
let style = CellStyle::new();
assert!(style.is_empty());
let style_bold = CellStyle {
bold: Some(true),
..Default::default()
};
assert!(!style_bold.is_empty());
}
#[test]
fn test_cell_style_merge() {
let mut style1 = CellStyle {
font_color: Some("#FF0000".to_string()),
bold: Some(true),
..Default::default()
};
let style2 = CellStyle {
bg_color: Some("#00FF00".to_string()),
font_color: Some("#0000FF".to_string()),
font_size: Some(14.0),
..Default::default()
};
style1.merge(&style2);
assert_eq!(style1.font_color, Some("#0000FF".to_string()));
assert_eq!(style1.bg_color, Some("#00FF00".to_string()));
assert_eq!(style1.bold, Some(true));
assert_eq!(style1.font_size, Some(14.0));
}
}