use lazy_static::lazy_static;
use regex::Regex;
use crate::NamingCase;
pub fn which_case(identifier: &str) -> NamingCase {
if is_single_word(identifier) {
return NamingCase::SingleWord(identifier.to_string());
} else if is_screaming_snake(identifier) {
return NamingCase::ScreamingSnake(identifier.to_string());
} else if is_snake(identifier) {
return NamingCase::Snake(identifier.to_string());
} else if is_kebab(identifier) {
return NamingCase::Kebab(identifier.to_string());
} else if is_camel(identifier) {
return NamingCase::Camel(identifier.to_string());
} else if is_pascal(identifier) {
return NamingCase::Pascal(identifier.to_string());
} else {
NamingCase::Invalid(identifier.to_string())
}
}
pub fn is_single_word(word: &str) -> bool {
lazy_static! {
static ref SINGLE_WORD_REGEX:Regex=Regex::new(r"^(?:[a-z]+|[A-Z]+|[A-Z][a-z]+)\d*$").unwrap();
}
SINGLE_WORD_REGEX.is_match(word)
}
pub fn is_screaming_snake(identifier: &str) -> bool {
lazy_static! {
static ref SCREAMING_SNAKE_REGEX: Regex = Regex::new(r"^[A-Z]+\d*(_[A-Z]+\d*)*$").unwrap();
}
SCREAMING_SNAKE_REGEX.is_match(identifier)
}
pub fn is_snake(identifier: &str) -> bool {
lazy_static! {
static ref SNAKE_REGEX: Regex = Regex::new(r"^[a-z]+\d*(_[a-z]+\d*)*$").unwrap();
}
SNAKE_REGEX.is_match(identifier)
}
pub fn is_kebab(identifier: &str) -> bool {
lazy_static! {
static ref KEBAB_REGEX: Regex = Regex::new(r"^[a-z]+\d*(-[a-z]+\d*)*$").unwrap();
}
KEBAB_REGEX.is_match(identifier)
}
pub fn is_camel(identifier: &str) -> bool {
lazy_static! {
static ref CAMEL_REGEX: Regex = Regex::new(r"^[a-z]+\d*([A-Z][a-z]*\d*)*$").unwrap();
}
CAMEL_REGEX.is_match(identifier)
}
pub fn is_pascal(identifier: &str) -> bool {
lazy_static! {
static ref PASCAL_REGEX: Regex = Regex::new(r"^([A-Z][a-z]*\d*)+$").unwrap();
}
PASCAL_REGEX.is_match(identifier)
}