1pub mod fs;
4pub mod cargo;
5
6
7pub fn to_pascal_case(input: &str) -> String {
10 input
11 .split('_')
12 .map(|word| {
13 if word.is_empty() {
14 String::new()
15 } else {
16 let mut chars = word.chars();
17 match chars.next() {
18 None => String::new(),
19 Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
20 }
21 }
22 })
23 .collect()
24}
25
26pub fn to_snake_case(s: &str) -> String {
28 if s.is_empty() {
29 return String::new();
30 }
31
32 let mut result = String::new();
33 let mut last_was_underscore = false;
34 let chars: Vec<char> = s.chars().collect();
35
36 if chars[0].is_alphanumeric() {
38 result.push(chars[0].to_ascii_lowercase());
39 } else {
40 last_was_underscore = true;
41 }
42
43 for i in 1..chars.len() {
45 let c = chars[i];
46
47 if c.is_alphanumeric() {
48 if c.is_uppercase() &&
51 i > 0 &&
52 (chars[i-1].is_lowercase() || chars[i-1].is_numeric()) &&
53 !last_was_underscore {
54 result.push('_');
55 }
56 result.push(c.to_ascii_lowercase());
57 last_was_underscore = false;
58 } else if !last_was_underscore {
59 result.push('_');
60 last_was_underscore = true;
61 }
62 }
63
64 if result.ends_with('_') {
66 result.pop();
67 }
68
69 result
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn test_to_pascal_case() {
78 assert_eq!(to_pascal_case("hello_world"), "HelloWorld");
80
81 assert_eq!(to_pascal_case("hello"), "Hello");
83
84 assert_eq!(to_pascal_case("hello_beautiful_world"), "HelloBeautifulWorld");
86
87 assert_eq!(to_pascal_case("Hello_World"), "HelloWorld");
89
90 assert_eq!(to_pascal_case(""), "");
92
93 assert_eq!(to_pascal_case("_hello"), "Hello");
95
96 assert_eq!(to_pascal_case("hello_"), "Hello");
98
99 assert_eq!(to_pascal_case("hello__world"), "HelloWorld");
101
102 assert_eq!(to_pascal_case("hello_World"), "HelloWorld");
104 }
105
106 #[test]
107 fn test_to_snake_case() {
108 assert_eq!(to_snake_case("HelloWorld"), "hello_world");
110
111 assert_eq!(to_snake_case("helloWorld"), "hello_world");
113
114 assert_eq!(to_snake_case("Hello"), "hello");
116
117 assert_eq!(to_snake_case("hello_world"), "hello_world");
119
120 assert_eq!(to_snake_case(""), "");
122
123 assert_eq!(to_snake_case("Hello World"), "hello_world");
125
126 assert_eq!(to_snake_case("Hello, World!"), "hello_world");
128
129 assert_eq!(to_snake_case("Hello--World"), "hello_world");
131
132 assert_eq!(to_snake_case("-Hello"), "hello");
134
135 assert_eq!(to_snake_case("Hello-"), "hello");
137
138 assert_eq!(to_snake_case("Hello123World"), "hello123_world");
140
141 assert_eq!(to_snake_case("HELLO_WORLD"), "hello_world");
143 }
144}