Skip to main content

rustra_naming/
lib.rs

1//! Shared identifier naming rules used by the Rust and proc-macro codegen paths.
2
3/// Converts snake-, kebab-, and dot-separated names to lower camel case.
4///
5/// The first alphanumeric character is always lower-cased. Delimiters are
6/// removed and uppercase the next alphanumeric character, matching the
7/// historical Rustra codegen behavior.
8pub fn snake_to_lower_camel(name: &str) -> String {
9    let mut output = String::new();
10    let mut uppercase_next = false;
11
12    for character in name.chars() {
13        if matches!(character, '_' | '-' | '.') {
14            uppercase_next = true;
15            continue;
16        }
17
18        if output.is_empty() {
19            output.push(character.to_ascii_lowercase());
20            uppercase_next = false;
21        } else if uppercase_next {
22            output.push(character.to_ascii_uppercase());
23            uppercase_next = false;
24        } else {
25            output.push(character);
26        }
27    }
28
29    output
30}
31
32#[cfg(test)]
33mod tests {
34    use super::snake_to_lower_camel;
35
36    #[test]
37    fn empty_and_delimiter_only_names_remain_empty() {
38        assert_eq!(snake_to_lower_camel(""), "");
39        assert_eq!(snake_to_lower_camel("__"), "");
40    }
41}