use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Style {
#[serde(default)]
pub font: Font,
#[serde(default)]
pub fill: Option<String>,
#[serde(default)]
pub alignment: Alignment,
#[serde(default)]
pub borders: Borders,
#[serde(default)]
pub number_format: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Font {
pub bold: Option<bool>,
pub italic: Option<bool>,
pub underline: Option<bool>,
pub strikethrough: Option<bool>,
pub size: Option<f64>,
pub name: Option<String>,
pub color: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Horizontal {
General,
Left,
Right,
Center,
CenterContinuous,
Justify,
Fill,
Distributed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Vertical {
Bottom,
Top,
Center,
Justify,
Distributed,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Alignment {
pub horizontal: Option<Horizontal>,
pub vertical: Option<Vertical>,
pub wrap_text: Option<bool>,
pub indent: Option<u8>,
pub text_rotation: Option<i16>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BorderStyle {
None,
Thin,
Medium,
Thick,
Dashed,
Dotted,
Double,
Hair,
MediumDashed,
DashDot,
MediumDashDot,
DashDotDot,
MediumDashDotDot,
SlantDashDot,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Border {
pub style: BorderStyle,
pub color: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Borders {
pub top: Option<Border>,
pub bottom: Option<Border>,
pub left: Option<Border>,
pub right: Option<Border>,
}
macro_rules! merge {
($target:expr, $source:expr, $($field:ident),*) => {
$(
if $source.$field.is_some() {
$target.$field = $source.$field.clone();
}
)*
};
}
impl Style {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
*self == Self::default()
}
pub fn apply(&mut self, other: &Style) {
merge!(
self.font,
other.font,
bold,
italic,
underline,
strikethrough,
size,
name,
color
);
merge!(self, other, fill, number_format);
merge!(
self.alignment,
other.alignment,
horizontal,
vertical,
wrap_text,
indent,
text_rotation
);
merge!(self.borders, other.borders, top, bottom, left, right);
}
pub fn merged(&self, other: &Style) -> Style {
let mut style = self.clone();
style.apply(other);
style
}
pub fn bold(mut self) -> Self {
self.font.bold = Some(true);
self
}
pub fn italic(mut self) -> Self {
self.font.italic = Some(true);
self
}
pub fn underline(mut self) -> Self {
self.font.underline = Some(true);
self
}
pub fn strikethrough(mut self) -> Self {
self.font.strikethrough = Some(true);
self
}
pub fn font_size(mut self, size: f64) -> Self {
self.font.size = Some(size);
self
}
pub fn font_name(mut self, name: impl Into<String>) -> Self {
self.font.name = Some(name.into());
self
}
pub fn font_color(mut self, color: impl Into<String>) -> Self {
self.font.color = Some(color.into());
self
}
pub fn background(mut self, color: impl Into<String>) -> Self {
self.fill = Some(color.into());
self
}
pub fn horizontal(mut self, horizontal: Horizontal) -> Self {
self.alignment.horizontal = Some(horizontal);
self
}
pub fn vertical(mut self, vertical: Vertical) -> Self {
self.alignment.vertical = Some(vertical);
self
}
pub fn wrap_text(mut self) -> Self {
self.alignment.wrap_text = Some(true);
self
}
pub fn indent(mut self, indent: u8) -> Self {
self.alignment.indent = Some(indent);
self
}
pub fn text_rotation(mut self, degrees: i16) -> Self {
self.alignment.text_rotation = Some(degrees);
self
}
pub fn border(mut self, style: BorderStyle) -> Self {
let border = Some(Border { style, color: None });
self.borders = Borders {
top: border.clone(),
bottom: border.clone(),
left: border.clone(),
right: border,
};
self
}
pub fn border_with_color(mut self, style: BorderStyle, color: impl Into<String>) -> Self {
let border = Some(Border {
style,
color: Some(color.into()),
});
self.borders = Borders {
top: border.clone(),
bottom: border.clone(),
left: border.clone(),
right: border,
};
self
}
pub fn border_top(mut self, style: BorderStyle) -> Self {
self.borders.top = Some(Border { style, color: None });
self
}
pub fn border_bottom(mut self, style: BorderStyle) -> Self {
self.borders.bottom = Some(Border { style, color: None });
self
}
pub fn border_left(mut self, style: BorderStyle) -> Self {
self.borders.left = Some(Border { style, color: None });
self
}
pub fn border_right(mut self, style: BorderStyle) -> Self {
self.borders.right = Some(Border { style, color: None });
self
}
pub fn number_format(mut self, format: impl Into<String>) -> Self {
self.number_format = Some(format.into());
self
}
}
pub(crate) fn rgb(color: &str) -> Option<u32> {
let hex = color.trim().trim_start_matches('#');
let hex = if hex.len() == 8 { &hex[2..] } else { hex };
if hex.len() != 6 {
return None;
}
u32::from_str_radix(hex, 16).ok()
}
pub mod number_format {
pub use crate::number_formatter::format_value;
pub const FORMAT_GENERAL: &str = "General";
pub const FORMAT_TEXT: &str = "@";
pub const FORMAT_NUMBER: &str = "0";
pub const FORMAT_NUMBER_0: &str = "0.0";
pub const FORMAT_NUMBER_00: &str = "0.00";
pub const FORMAT_NUMBER_COMMA_SEPARATED1: &str = "#,##0.00";
pub const FORMAT_NUMBER_COMMA_SEPARATED2: &str = "#,##0.00_-";
pub const FORMAT_PERCENTAGE: &str = "0%";
pub const FORMAT_PERCENTAGE_0: &str = "0.0%";
pub const FORMAT_PERCENTAGE_00: &str = "0.00%";
pub const FORMAT_DATE_YYYYMMDD: &str = "yyyy-mm-dd";
pub const FORMAT_DATE_DDMMYYYY: &str = "dd/mm/yyyy";
pub const FORMAT_DATE_DMYSLASH: &str = "d/m/yy";
pub const FORMAT_DATE_DMYMINUS: &str = "d-m-yy";
pub const FORMAT_DATE_DMMINUS: &str = "d-m";
pub const FORMAT_DATE_MYMINUS: &str = "m-yy";
pub const FORMAT_DATE_XLSX14: &str = "mm-dd-yy";
pub const FORMAT_DATE_XLSX15: &str = "d-mmm-yy";
pub const FORMAT_DATE_XLSX16: &str = "d-mmm";
pub const FORMAT_DATE_XLSX17: &str = "mmm-yy";
pub const FORMAT_DATE_XLSX22: &str = "m/d/yy h:mm";
pub const FORMAT_DATE_DATETIME: &str = "d/m/yy h:mm";
pub const FORMAT_DATE_TIME1: &str = "h:mm AM/PM";
pub const FORMAT_DATE_TIME2: &str = "h:mm:ss AM/PM";
pub const FORMAT_DATE_TIME3: &str = "h:mm";
pub const FORMAT_DATE_TIME4: &str = "h:mm:ss";
pub const FORMAT_DATE_TIME5: &str = "mm:ss";
pub const FORMAT_DATE_TIME6: &str = "h:mm:ss";
pub const FORMAT_DATE_TIME8: &str = "h:mm:ss;@";
pub const FORMAT_DATE_YYYYMMDDSLASH: &str = "yyyy/mm/dd;@";
}