use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ColorHint {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl ColorHint {
#[must_use]
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct StyleHint {
pub fg: Option<ColorHint>,
pub bg: Option<ColorHint>,
pub bold: bool,
pub italic: bool,
pub underlined: bool,
}
impl StyleHint {
#[must_use]
pub const fn with_fg(fg: ColorHint) -> Self {
Self {
fg: Some(fg),
bg: None,
bold: false,
italic: false,
underlined: false,
}
}
#[must_use]
pub const fn bg(mut self, bg: ColorHint) -> Self {
self.bg = Some(bg);
self
}
#[must_use]
pub const fn bold(mut self) -> Self {
self.bold = true;
self
}
#[must_use]
pub const fn italic(mut self) -> Self {
self.italic = true;
self
}
#[must_use]
pub const fn underlined(mut self) -> Self {
self.underlined = true;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_hint_rgb_constructor() {
let c = ColorHint::rgb(12, 34, 56);
assert_eq!(c.r, 12);
assert_eq!(c.g, 34);
assert_eq!(c.b, 56);
}
#[test]
fn style_hint_default_is_no_style() {
let s = StyleHint::default();
assert!(s.fg.is_none());
assert!(s.bg.is_none());
assert!(!s.bold);
assert!(!s.italic);
assert!(!s.underlined);
}
#[test]
fn style_hint_builder_chain() {
let s = StyleHint::with_fg(ColorHint::rgb(1, 2, 3))
.bg(ColorHint::rgb(10, 20, 30))
.bold()
.italic()
.underlined();
assert_eq!(s.fg, Some(ColorHint::rgb(1, 2, 3)));
assert_eq!(s.bg, Some(ColorHint::rgb(10, 20, 30)));
assert!(s.bold);
assert!(s.italic);
assert!(s.underlined);
}
}