use crate::string::*;
use std::borrow::ToOwned;
use std::collections::HashMap;
use std::str::FromStr;
pub trait CaseTransform: ToOwned + AsRef<str> {
fn to_lower_first(&self) -> String {
lower_first(self.as_ref())
}
fn to_upper_first(&self) -> String {
upper_first(self.as_ref())
}
fn to_camel_case(&self) -> String {
camel_case(self.as_ref())
}
fn to_kebab_case(&self) -> String {
kebab_case(self.as_ref())
}
fn to_snake_case(&self) -> String {
snake_case(self.as_ref())
}
fn to_screaming_snake_case(&self) -> String {
screaming_snake_case(self.as_ref())
}
fn to_title_case(&self) -> String {
title_case(self.as_ref())
}
fn to_slug(&self) -> String {
slugify(self.as_ref())
}
fn to_capitalize(&self) -> String {
capitalize(self.as_ref())
}
}
pub trait WordTransform: ToOwned + AsRef<str> {
fn to_words(&self) -> Vec<String> {
words(self.as_ref())
}
fn wrap(&self, width: usize, break_str: &str, cut: bool) -> String {
wordwrap(self.as_ref(), width, break_str, cut)
}
fn wordwrap(&self, width: usize, break_str: &str, cut: bool) -> String {
wordwrap(self.as_ref(), width, break_str, cut)
}
}
pub trait UtilityTransform: ToOwned + AsRef<str> {
fn str_rev(&self) -> String {
str_rev(self.as_ref())
}
fn str_split(&self, delimiter: &str) -> Vec<String> {
str_split(self.as_ref(), delimiter)
}
fn to_truncate_middle(&self, max_len: usize) -> String {
truncate_middle(self.as_ref(), max_len)
}
fn to_template(&self, values: &HashMap<&str, &str>) -> String {
template(self.as_ref(), values)
}
fn to_safe_parse<T: FromStr>(&self) -> Option<T> {
safe_parse(self.as_ref())
}
fn pad(&self, length: usize, pad_str: &str, pad_type: Alignment) -> String {
str_pad(self.as_ref(), length, pad_str, pad_type)
}
}
impl CaseTransform for str {}
impl WordTransform for str {}
impl UtilityTransform for str {}
impl CaseTransform for String {}
impl WordTransform for String {}
impl UtilityTransform for String {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_to_lower_first_from_str() {
let input: &str = "Hello, World!";
let expected = "hello, World!";
assert_eq!(input.to_lower_first(), expected);
}
#[test]
fn test_to_lower_first_from_string() {
let input = String::from("Hello, World!");
let expected = "hello, World!";
assert_eq!(input.to_lower_first(), expected);
}
#[test]
fn test_to_slug_from_str() {
let input: &str = "Hello, World!";
let expected = "hello-world";
assert_eq!(input.to_slug(), expected);
}
#[test]
fn test_to_slug_from_string() {
let input = String::from("Hello, World!");
let expected = "hello-world";
assert_eq!(input.to_slug(), expected);
}
#[test]
fn test_to_pad_from_string() {
assert_eq!("42".pad(5, "0", Alignment::Left), "00042");
}
}