#[derive(Clone, Debug, Default, PartialEq)]
pub enum CellAlign {
Left,
#[default]
Center,
Right,
Justify,
}
impl CellAlign {
pub fn as_str(&self) -> &'static str {
match self {
CellAlign::Left => "l",
CellAlign::Center => "ctr",
CellAlign::Right => "r",
CellAlign::Justify => "just",
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum CellVAlign {
Top,
#[default]
Middle,
Bottom,
}
impl CellVAlign {
pub fn as_str(&self) -> &'static str {
match self {
CellVAlign::Top => "t",
CellVAlign::Middle => "ctr",
CellVAlign::Bottom => "b",
}
}
}
#[derive(Clone, Debug)]
pub struct TableCell {
pub text: String,
pub bold: bool,
pub italic: bool,
pub underline: bool,
pub text_color: Option<String>,
pub background_color: Option<String>,
pub font_size: Option<u32>,
pub font_family: Option<String>,
pub align: CellAlign,
pub valign: CellVAlign,
pub wrap_text: bool,
pub grid_span: Option<u32>,
pub row_span: Option<u32>,
pub h_merge: bool,
pub v_merge: bool,
}
impl TableCell {
pub fn new(text: &str) -> Self {
TableCell {
text: text.to_string(),
bold: false,
italic: false,
underline: false,
text_color: None,
background_color: None,
font_size: None,
font_family: None,
align: CellAlign::Center,
valign: CellVAlign::Middle,
wrap_text: true,
grid_span: None,
row_span: None,
h_merge: false,
v_merge: false,
}
}
pub fn bold(mut self) -> Self {
self.bold = true;
self
}
pub fn italic(mut self) -> Self {
self.italic = true;
self
}
pub fn underline(mut self) -> Self {
self.underline = true;
self
}
pub fn text_color(mut self, color: &str) -> Self {
self.text_color = Some(color.trim_start_matches('#').to_uppercase());
self
}
pub fn background_color(mut self, color: &str) -> Self {
self.background_color = Some(color.trim_start_matches('#').to_uppercase());
self
}
pub fn font_size(mut self, size: u32) -> Self {
self.font_size = Some(size);
self
}
pub fn font_family(mut self, family: &str) -> Self {
self.font_family = Some(family.to_string());
self
}
pub fn align(mut self, align: CellAlign) -> Self {
self.align = align;
self
}
pub fn align_left(mut self) -> Self {
self.align = CellAlign::Left;
self
}
pub fn align_right(mut self) -> Self {
self.align = CellAlign::Right;
self
}
pub fn align_center(mut self) -> Self {
self.align = CellAlign::Center;
self
}
pub fn valign(mut self, valign: CellVAlign) -> Self {
self.valign = valign;
self
}
pub fn valign_top(mut self) -> Self {
self.valign = CellVAlign::Top;
self
}
pub fn valign_bottom(mut self) -> Self {
self.valign = CellVAlign::Bottom;
self
}
pub fn wrap(mut self, wrap: bool) -> Self {
self.wrap_text = wrap;
self
}
pub fn grid_span(mut self, span: u32) -> Self {
self.grid_span = Some(span);
self
}
pub fn row_span(mut self, span: u32) -> Self {
self.row_span = Some(span);
self
}
pub fn h_merge(mut self) -> Self {
self.h_merge = true;
self
}
pub fn v_merge(mut self) -> Self {
self.v_merge = true;
self
}
pub fn with_col_span(self, span: u32) -> Self {
self.grid_span(span)
}
pub fn with_row_span(self, span: u32) -> Self {
self.row_span(span)
}
pub fn with_h_merge(self) -> Self {
self.h_merge()
}
pub fn with_v_merge(self) -> Self {
self.v_merge()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cell_new() {
let cell = TableCell::new("Hello");
assert_eq!(cell.text, "Hello");
assert!(!cell.bold);
assert!(!cell.italic);
assert_eq!(cell.align, CellAlign::Center);
assert_eq!(cell.valign, CellVAlign::Middle);
}
#[test]
fn test_cell_formatting() {
let cell = TableCell::new("Test")
.bold()
.italic()
.underline()
.text_color("FF0000")
.background_color("0000FF")
.font_size(24)
.font_family("Arial");
assert!(cell.bold);
assert!(cell.italic);
assert!(cell.underline);
assert_eq!(cell.text_color, Some("FF0000".to_string()));
assert_eq!(cell.background_color, Some("0000FF".to_string()));
assert_eq!(cell.font_size, Some(24));
assert_eq!(cell.font_family, Some("Arial".to_string()));
}
#[test]
fn test_cell_alignment() {
let cell = TableCell::new("Test").align_left().valign_top();
assert_eq!(cell.align, CellAlign::Left);
assert_eq!(cell.valign, CellVAlign::Top);
}
#[test]
fn test_cell_align_as_str() {
assert_eq!(CellAlign::Left.as_str(), "l");
assert_eq!(CellAlign::Center.as_str(), "ctr");
assert_eq!(CellAlign::Right.as_str(), "r");
assert_eq!(CellAlign::Justify.as_str(), "just");
}
#[test]
fn test_cell_valign_as_str() {
assert_eq!(CellVAlign::Top.as_str(), "t");
assert_eq!(CellVAlign::Middle.as_str(), "ctr");
assert_eq!(CellVAlign::Bottom.as_str(), "b");
}
}