use crate::boundary::{self, Boundary};
use crate::pattern::Pattern;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Eq, PartialEq, Hash, Clone, Copy, Debug)]
#[non_exhaustive]
pub enum Case<'b> {
Custom {
boundaries: &'b [Boundary],
pattern: Pattern,
delimiter: &'static str,
},
Snake,
Constant,
UpperSnake,
Ada,
Kebab,
Cobol,
UpperKebab,
Train,
Flat,
UpperFlat,
Pascal,
UpperCamel,
Camel,
Lower,
Upper,
Title,
Sentence,
}
impl Case<'_> {
pub fn boundaries(&self) -> &[Boundary] {
use Case::*;
match self {
Snake | Constant | UpperSnake | Ada => &[Boundary::Underscore],
Kebab | Cobol | UpperKebab | Train => &[Boundary::Hyphen],
Upper | Lower | Title | Sentence => &[Boundary::Space],
Camel | UpperCamel | Pascal => &[
Boundary::LowerUpper,
Boundary::Acronym,
Boundary::LowerDigit,
Boundary::UpperDigit,
Boundary::DigitLower,
Boundary::DigitUpper,
],
UpperFlat | Flat => &[],
Custom { boundaries, .. } => boundaries,
}
}
pub const fn delimiter(&self) -> &'static str {
use Case::*;
match self {
Snake | Constant | UpperSnake | Ada => "_",
Kebab | Cobol | UpperKebab | Train => "-",
Upper | Lower | Title | Sentence => " ",
Flat | UpperFlat | Pascal | UpperCamel | Camel => "",
Custom {
delimiter: delim, ..
} => delim,
}
}
pub const fn pattern(&self) -> Pattern {
use Case::*;
match self {
Constant | UpperSnake | Cobol | UpperKebab | UpperFlat | Upper => Pattern::Uppercase,
Snake | Kebab | Flat | Lower => Pattern::Lowercase,
Ada | Train | Pascal | UpperCamel | Title => Pattern::Capital,
Camel => Pattern::Camel,
Sentence => Pattern::Sentence,
Custom { pattern, .. } => *pattern,
}
}
pub fn split<T>(self, s: &T) -> Vec<&str>
where
T: AsRef<str>,
{
boundary::split(s, self.boundaries())
}
pub fn mutate(self, words: &[&str]) -> Vec<String> {
self.pattern().mutate(words)
}
pub fn join(self, words: &[String]) -> String {
words.join(self.delimiter())
}
pub fn all_cases() -> &'static [Case<'static>] {
use Case::*;
&[
Snake, Constant, Ada, Kebab, Cobol, Train, Flat, UpperFlat, Pascal, Camel, Upper,
Lower, Title, Sentence,
]
}
}