silent_db/
utils.rs

1/// 字符串转换为驼峰命名
2#[allow(dead_code)]
3pub fn to_camel_case(s: &str) -> String {
4    let mut result = String::new();
5    let mut next_upper = true;
6    for c in s.chars() {
7        if c == '_' {
8            next_upper = true;
9        } else if c.is_ascii_uppercase() {
10            result.push(c);
11            next_upper = false;
12        } else if next_upper {
13            result.push(c.to_ascii_uppercase());
14            next_upper = false;
15        } else {
16            result.push(c.to_ascii_lowercase());
17        }
18    }
19    result
20}
21
22/// 字符串转换为蛇形命名
23#[allow(dead_code)]
24pub fn to_snake_case(s: &str) -> String {
25    let mut result = String::new();
26    for c in s.chars() {
27        if c.is_ascii_uppercase() {
28            if !result.is_empty() {
29                result.push('_');
30            }
31            result.push(c.to_ascii_lowercase());
32        } else {
33            result.push(c);
34        }
35    }
36    result
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_to_camel_case() {
45        assert_eq!(to_camel_case("hello_world"), "HelloWorld");
46        assert_eq!(to_camel_case("rust_programming"), "RustProgramming");
47        assert_eq!(to_camel_case("github_copilot"), "GithubCopilot");
48    }
49
50    #[test]
51    fn test_to_snake_case() {
52        assert_eq!(to_snake_case("HelloWorld"), "hello_world");
53        assert_eq!(to_snake_case("RustProgramming"), "rust_programming");
54        assert_eq!(to_snake_case("GithubCopilot"), "github_copilot");
55    }
56}