use colored::{Color, ColoredString, Colorize};
pub trait Coloration<ColorType>
where
ColorType: Into<Color>,
{
fn as_colored_title(&self, color: ColorType) -> ColoredString;
}
impl<FS, ColorType> Coloration<ColorType> for FS
where
FS: AsRef<str> + ?Sized,
ColorType: Into<Color>,
{
fn as_colored_title(&self, color: ColorType) -> ColoredString {
self.as_ref().color(color.into()).bold()
}
}
#[cfg(test)]
mod tests {
use colored::Colorize;
use super::*;
#[test]
fn coloration_trait_with_str() {
let a = "Hello".as_colored_title(Color::Red);
assert_eq!(a, "Hello".red().bold());
}
#[test]
fn coloration_trait_with_string() {
let a = String::from("Hello").as_colored_title(Color::Red);
assert_eq!(a, "Hello".red().bold());
}
#[test]
fn coloration_trait_with_colored_string() {
let a = ColoredString::from("Hello").as_colored_title(Color::Red);
assert_eq!(a, "Hello".red().bold());
}
}