use colored::{control::SHOULD_COLORIZE, ColoredString, Colorize};
pub trait CodeStr {
fn code_str(&self) -> ColoredString;
}
impl CodeStr for str {
fn code_str(&self) -> ColoredString {
if SHOULD_COLORIZE.should_colorize() {
self.magenta()
} else {
ColoredString::from(&format!("`{}`", self) as &Self)
}
}
}
pub fn number(n: usize, noun: &str) -> String {
if n == 1 {
format!("{} {}", n, noun)
} else {
format!("{} {}s", n, noun)
}
}
pub fn series(items: &[String]) -> String {
match items.len() {
0 => String::new(),
1 => items[0].clone(),
2 => format!("{} and {}", items[0], items[1]),
_ => format!(
"{}, and {}",
items[..items.len() - 1].join(", "),
items[items.len() - 1],
),
}
}
#[cfg(test)]
mod tests {
use crate::format::{number, series, CodeStr};
#[test]
fn code_str_display() {
assert_eq!(format!("{}", "foo".code_str()), "`foo`");
}
#[test]
fn number_zero() {
assert_eq!(number(0, "cow"), "0 cows");
}
#[test]
fn number_one() {
assert_eq!(number(1, "cow"), "1 cow");
}
#[test]
fn number_two() {
assert_eq!(number(2, "cow"), "2 cows");
}
#[test]
fn series_empty() {
assert_eq!(series(&[]), "");
}
#[test]
fn series_one() {
assert_eq!(series(&["foo".to_owned()]), "foo");
}
#[test]
fn series_two() {
assert_eq!(series(&["foo".to_owned(), "bar".to_owned()]), "foo and bar");
}
#[test]
fn series_three() {
assert_eq!(
series(&["foo".to_owned(), "bar".to_owned(), "baz".to_owned()]),
"foo, bar, and baz",
);
}
}