use super::colors::Color;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Border {
pub border_style: Option<String>,
pub color: Color,
}
impl Default for Border {
fn default() -> Self {
Border {
border_style: Some(Border::BORDER_NONE.to_string()),
color: Color::new(Color::BLACK),
}
}
}
impl Border {
pub const BORDER_NONE: &'static str = "none";
pub const BORDER_DASHDOT: &'static str = "dashDot";
pub const BORDER_DASHDOTDOT: &'static str = "dashDotDot";
pub const BORDER_DASHED: &'static str = "dashed";
pub const BORDER_DOTTED: &'static str = "dotted";
pub const BORDER_DOUBLE: &'static str = "double";
pub const BORDER_HAIR: &'static str = "hair";
pub const BORDER_MEDIUM: &'static str = "medium";
pub const BORDER_MEDIUMDASHDOT: &'static str = "mediumDashDot";
pub const BORDER_MEDIUMDASHDOTDOT: &'static str = "mediumDashDotDot";
pub const BORDER_MEDIUMDASHED: &'static str = "mediumDashed";
pub const BORDER_SLANTDASHDOT: &'static str = "slantDashDot";
pub const BORDER_THICK: &'static str = "thick";
pub const BORDER_THIN: &'static str = "thin";
pub fn new() -> Self {
Border::default()
}
pub fn styled(style: &str, color: Color) -> Self {
Border {
border_style: Some(style.to_string()),
color,
}
}
pub fn absent() -> Self {
Border {
border_style: None,
color: Color::new(Color::BLACK),
}
}
pub fn with_style(mut self, style: Option<&str>) -> Self {
self.border_style = style.map(|s| s.to_string());
self
}
pub fn with_color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn has_style(&self) -> bool {
!matches!(
self.border_style.as_deref(),
None | Some(Border::BORDER_NONE)
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Borders {
pub left: Border,
pub right: Border,
pub top: Border,
pub bottom: Border,
pub diagonal: Border,
pub diagonal_direction: i64,
pub all_borders: Border,
pub outline: Border,
pub inside: Border,
pub vertical: Border,
pub horizontal: Border,
}
impl Default for Borders {
fn default() -> Self {
Borders {
left: Border::default(),
right: Border::default(),
top: Border::default(),
bottom: Border::default(),
diagonal: Border::default(),
diagonal_direction: Borders::DIAGONAL_NONE,
all_borders: Border::default(),
outline: Border::default(),
inside: Border::default(),
vertical: Border::default(),
horizontal: Border::default(),
}
}
}
impl Borders {
pub const DIAGONAL_NONE: i64 = 0;
pub const DIAGONAL_UP: i64 = 1;
pub const DIAGONAL_DOWN: i64 = 2;
pub const DIAGONAL_BOTH: i64 = 3;
pub fn new() -> Self {
Borders::default()
}
pub fn all_sides(style: &str, color: Color) -> Self {
let side = Border::styled(style, color);
Borders {
left: side.clone(),
right: side.clone(),
top: side.clone(),
bottom: side.clone(),
..Borders::default()
}
}
pub fn with_left(mut self, border: Border) -> Self {
self.left = border;
self
}
pub fn with_right(mut self, border: Border) -> Self {
self.right = border;
self
}
pub fn with_top(mut self, border: Border) -> Self {
self.top = border;
self
}
pub fn with_bottom(mut self, border: Border) -> Self {
self.bottom = border;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_border_is_none_black() {
let border = Border::new();
assert_eq!(border.border_style.as_deref(), Some("none"));
assert_eq!(border.color.index, Color::BLACK);
assert!(!border.has_style());
}
#[test]
fn absent_border_has_no_style() {
let border = Border::absent();
assert_eq!(border.border_style, None);
assert!(!border.has_style());
}
#[test]
fn all_sides_helper() {
let borders = Borders::all_sides("thin", Color::new("FFFF0000"));
assert_eq!(borders.left.border_style.as_deref(), Some("thin"));
assert_eq!(borders.bottom.color.index, "FFFF0000");
assert!(borders.left.has_style());
assert_eq!(borders.diagonal_direction, Borders::DIAGONAL_NONE);
}
#[test]
fn diagonal_direction_constants() {
assert_eq!(Borders::DIAGONAL_BOTH, 3);
assert_ne!(Borders::DIAGONAL_UP, Borders::DIAGONAL_DOWN);
}
}