pub mod fs;
pub mod cargo;
pub fn to_pascal_case(input: &str) -> String {
input
.split('_')
.map(|word| {
if word.is_empty() {
String::new()
} else {
let mut chars = word.chars();
match chars.next() {
None => String::new(),
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
}
}
})
.collect()
}
pub fn to_snake_case(s: &str) -> String {
if s.is_empty() {
return String::new();
}
let mut result = String::new();
let mut last_was_underscore = false;
let chars: Vec<char> = s.chars().collect();
if chars[0].is_alphanumeric() {
result.push(chars[0].to_ascii_lowercase());
} else {
last_was_underscore = true;
}
for i in 1..chars.len() {
let c = chars[i];
if c.is_alphanumeric() {
if c.is_uppercase() &&
i > 0 &&
(chars[i-1].is_lowercase() || chars[i-1].is_numeric()) &&
!last_was_underscore {
result.push('_');
}
result.push(c.to_ascii_lowercase());
last_was_underscore = false;
} else if !last_was_underscore {
result.push('_');
last_was_underscore = true;
}
}
if result.ends_with('_') {
result.pop();
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_to_pascal_case() {
assert_eq!(to_pascal_case("hello_world"), "HelloWorld");
assert_eq!(to_pascal_case("hello"), "Hello");
assert_eq!(to_pascal_case("hello_beautiful_world"), "HelloBeautifulWorld");
assert_eq!(to_pascal_case("Hello_World"), "HelloWorld");
assert_eq!(to_pascal_case(""), "");
assert_eq!(to_pascal_case("_hello"), "Hello");
assert_eq!(to_pascal_case("hello_"), "Hello");
assert_eq!(to_pascal_case("hello__world"), "HelloWorld");
assert_eq!(to_pascal_case("hello_World"), "HelloWorld");
}
#[test]
fn test_to_snake_case() {
assert_eq!(to_snake_case("HelloWorld"), "hello_world");
assert_eq!(to_snake_case("helloWorld"), "hello_world");
assert_eq!(to_snake_case("Hello"), "hello");
assert_eq!(to_snake_case("hello_world"), "hello_world");
assert_eq!(to_snake_case(""), "");
assert_eq!(to_snake_case("Hello World"), "hello_world");
assert_eq!(to_snake_case("Hello, World!"), "hello_world");
assert_eq!(to_snake_case("Hello--World"), "hello_world");
assert_eq!(to_snake_case("-Hello"), "hello");
assert_eq!(to_snake_case("Hello-"), "hello");
assert_eq!(to_snake_case("Hello123World"), "hello123_world");
assert_eq!(to_snake_case("HELLO_WORLD"), "hello_world");
}
}