aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs
blob: 3d8f1b5f2d146d992fe9365f9d836386d1867ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
pub fn to_camel_case(ident: &str) -> Option<String> {
    let mut output = String::new();

    if is_camel_case(ident) {
        return None;
    }

    let mut capital_added = false;
    for chr in ident.chars() {
        if chr.is_alphabetic() {
            if !capital_added {
                output.push(chr.to_ascii_uppercase());
                capital_added = true;
            } else {
                output.push(chr.to_ascii_lowercase());
            }
        } else if chr == '_' {
            // Skip this character and make the next one capital.
            capital_added = false;
        } else {
            // Put the characted as-is.
            output.push(chr);
        }
    }

    if output == ident {
        None
    } else {
        Some(output)
    }
}

pub fn to_lower_snake_case(ident: &str) -> Option<String> {
    // First, assume that it's UPPER_SNAKE_CASE.
    if let Some(normalized) = to_lower_snake_case_from_upper_snake_case(ident) {
        return Some(normalized);
    }

    // Otherwise, assume that it's CamelCase.
    let lower_snake_case = stdx::to_lower_snake_case(ident);

    if lower_snake_case == ident {
        None
    } else {
        Some(lower_snake_case)
    }
}

fn to_lower_snake_case_from_upper_snake_case(ident: &str) -> Option<String> {
    if is_upper_snake_case(ident) {
        let string = ident.chars().map(|c| c.to_ascii_lowercase()).collect();
        Some(string)
    } else {
        None
    }
}

fn is_upper_snake_case(ident: &str) -> bool {
    ident.chars().all(|c| c.is_ascii_uppercase() || c == '_')
}

fn is_camel_case(ident: &str) -> bool {
    // We assume that the string is either snake case or camel case.
    ident.chars().all(|c| c != '_')
}

#[cfg(test)]
mod tests {
    use super::*;
    use expect_test::{expect, Expect};

    fn check<F: Fn(&str) -> Option<String>>(fun: F, input: &str, expect: Expect) {
        // `None` is translated to empty string, meaning that there is nothing to fix.
        let output = fun(input).unwrap_or_default();

        expect.assert_eq(&output);
    }

    #[test]
    fn test_to_lower_snake_case() {
        check(to_lower_snake_case, "lower_snake_case", expect![[""]]);
        check(to_lower_snake_case, "UPPER_SNAKE_CASE", expect![["upper_snake_case"]]);
        check(to_lower_snake_case, "CamelCase", expect![["camel_case"]]);
    }

    #[test]
    fn test_to_camel_case() {
        check(to_camel_case, "CamelCase", expect![[""]]);
        check(to_camel_case, "lower_snake_case", expect![["LowerSnakeCase"]]);
        check(to_camel_case, "UPPER_SNAKE_CASE", expect![["UpperSnakeCase"]]);
    }
}