Skip to main content

alef_codegen/
naming.rs

1use heck::{ToLowerCamelCase, ToPascalCase, ToShoutySnakeCase, ToSnakeCase};
2
3/// Convert a Rust snake_case name to the target language convention.
4pub fn to_python_name(name: &str) -> String {
5    name.to_snake_case()
6}
7
8/// Convert a Rust snake_case name to Node.js/TypeScript lowerCamelCase convention.
9pub fn to_node_name(name: &str) -> String {
10    name.to_lower_camel_case()
11}
12
13/// Convert a Rust snake_case name to Ruby snake_case convention.
14pub fn to_ruby_name(name: &str) -> String {
15    name.to_snake_case()
16}
17
18/// Convert a Rust snake_case name to PHP lowerCamelCase convention.
19pub fn to_php_name(name: &str) -> String {
20    name.to_lower_camel_case()
21}
22
23/// Convert a Rust snake_case name to Elixir snake_case convention.
24pub fn to_elixir_name(name: &str) -> String {
25    name.to_snake_case()
26}
27
28/// Well-known initialisms that must be fully uppercased per Go naming conventions.
29/// See: https://go.dev/wiki/CodeReviewComments#initialisms
30const INITIALISMS: &[&str] = &[
31    "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "FTP", "GID", "GraphQL", "GUI", "HTML", "HTTP", "HTTPS", "ID", "IMAP",
32    "IP", "JSON", "LHS", "MFA", "POP", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SQL", "SSH", "SSL", "TCP", "TLS",
33    "TTL", "UDP", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS",
34];
35
36/// Initialisms preserved in C# PascalCase. Microsoft's framework design guidelines
37/// recommend `Json`/`Http`/`Url` rather than `JSON`/`HTTP`/`URL` (3+ letter
38/// initialisms use PascalCase, 2-letter ones use all-caps). This list intentionally
39/// excludes generic acronyms so they round-trip cleanly through heck's PascalCase
40/// (matching alef's hardcoded helper names like `{Type}ToJson`/`{Type}FromJson`),
41/// while still preserving product names like `GraphQL` that heck would mangle.
42const CSHARP_INITIALISMS: &[&str] = &["GraphQL", "ID", "UUID"];
43
44/// Apply initialism uppercasing to a PascalCase name using the provided list.
45///
46/// Scans word boundaries in the PascalCase string and replaces any run of
47/// characters that matches a known initialism (case-insensitively) with the
48/// canonical form from the list. For example `ImageUrl` becomes `ImageURL`,
49/// `UserId` becomes `UserID`, and `GraphQlRouteConfig` becomes `GraphQLRouteConfig`.
50fn apply_initialisms(name: &str, list: &[&str]) -> String {
51    if name.is_empty() {
52        return name.to_string();
53    }
54
55    // Split the PascalCase string into words at uppercase letter boundaries.
56    // Each "word" is a contiguous sequence starting with an uppercase letter.
57    let mut words: Vec<&str> = Vec::new();
58    let mut word_start = 0;
59    let bytes = name.as_bytes();
60    for i in 1..bytes.len() {
61        if bytes[i].is_ascii_uppercase() {
62            words.push(&name[word_start..i]);
63            word_start = i;
64        }
65    }
66    words.push(&name[word_start..]);
67
68    // For each word, check if it matches a known initialism (case-insensitive).
69    let mut result = String::with_capacity(name.len());
70    let mut i = 0;
71    while i < words.len() {
72        // Try to match the longest possible span of consecutive words to a known initialism
73        // (longest-match first). This handles multi-segment initialisms like "GraphQL" which
74        // heck splits into "Graph" + "Ql".
75        let mut matched = false;
76        for span in (1..=(words.len() - i)).rev() {
77            let candidate: String = words[i..i + span].concat();
78            let candidate_upper = candidate.to_ascii_uppercase();
79            if let Some(&canonical) = list.iter().find(|&&s| s.to_ascii_uppercase() == candidate_upper) {
80                result.push_str(canonical);
81                i += span;
82                matched = true;
83                break;
84            }
85        }
86        if !matched {
87            result.push_str(words[i]);
88            i += 1;
89        }
90    }
91    result
92}
93
94/// Apply Go initialism uppercasing to a PascalCase name.
95///
96/// Scans word boundaries in the PascalCase string and replaces any run of
97/// characters that matches a known initialism (case-insensitively) with the
98/// all-caps form. For example `ImageUrl` becomes `ImageURL` and `UserId`
99/// becomes `UserID`.
100fn apply_go_acronyms(name: &str) -> String {
101    apply_initialisms(name, INITIALISMS)
102}
103
104/// Convert a Rust snake_case name to Go PascalCase convention with acronym uppercasing.
105pub fn to_go_name(name: &str) -> String {
106    apply_go_acronyms(&name.to_pascal_case())
107}
108
109/// Apply Go acronym uppercasing to a name that is already in PascalCase (e.g. an IR type name).
110///
111/// IR type names come directly from Rust PascalCase (e.g. `ImageUrl`, `JsonSchemaFormat`).
112/// This function uppercases known acronym segments so they conform to Go naming conventions
113/// (e.g. `ImageUrl` → `ImageURL`, `JsonSchemaFormat` → `JSONSchemaFormat`).
114pub fn go_type_name(name: &str) -> String {
115    apply_go_acronyms(name)
116}
117
118/// Convert a Rust snake_case parameter/variable name to Go lowerCamelCase with acronym uppercasing.
119///
120/// Go naming conventions require that acronyms in identifiers be fully uppercased.
121/// `to_lower_camel_case` alone converts `base_url` → `baseUrl`, but Go wants `baseURL`.
122/// This function converts via PascalCase (which applies acronym uppercasing) then lowercases
123/// the first "word" (the initial run of uppercase letters treated as a unit) while preserving
124/// the case of subsequent words/acronyms:
125/// - `base_url`  → `BaseURL`  → `baseURL`
126/// - `api_key`   → `APIKey`   → `apiKey`
127/// - `user_id`   → `UserID`   → `userID`
128/// - `json`      → `JSON`     → `json`
129pub fn go_param_name(name: &str) -> String {
130    let pascal = apply_go_acronyms(&name.to_pascal_case());
131    if pascal.is_empty() {
132        return pascal;
133    }
134    let bytes = pascal.as_bytes();
135    // Find the boundary of the first "word":
136    // - If the string begins with a multi-char uppercase run followed by a lowercase letter,
137    //   the run minus its last char is an acronym prefix (e.g. "APIKey": run="API", next='K')
138    //   → lowercase "AP" and keep "IKey" → "apIKey" ... but Go actually wants "apiKey".
139    //   The real rule: lowercase the whole leading uppercase run regardless, because the
140    //   acronym-prefix IS the first word.
141    // - If the string begins with a single uppercase char (e.g. "BaseURL"), lowercase just it.
142    //
143    // Concretely: find how many leading bytes are uppercase. If that whole run is followed by
144    // end-of-string, lowercase everything. If followed by more chars, lowercase the entire run.
145    // For "APIKey": upper_len=3, next='K'(uppercase) but that starts the second word.
146    // Actually: scan for the first lowercase char to find where the first word ends.
147    let first_lower = bytes.iter().position(|b| b.is_ascii_lowercase());
148    match first_lower {
149        None => {
150            // Entire string is uppercase (single acronym like "JSON", "URL") — all lowercase.
151            pascal.to_lowercase()
152        }
153        Some(0) => {
154            // Starts with lowercase (already correct)
155            pascal
156        }
157        Some(pos) => {
158            // pos is the index of the first lowercase char.
159            // The first "word" ends just before pos-1 (the char at pos-1 is the first char of
160            // the next PascalCase word that isnds with a lowercase continuation).
161            // For "BaseURL": pos=1 ('a'), so uppercase run = ['B'], lowercase just index 0.
162            // For "APIKey":  pos=4 ('e' in "Key"), uppercase run = "APIK", next lower = 'e',
163            //   so word boundary is at pos-1=3 ('K' is start of "Key").
164            //   → lowercase "API" (indices 0..2), keep "Key" → "apiKey" ✓
165            // For "UserID":  pos=1 ('s'), uppercase run starts at 'U', lowercase just 'U' → "userID"... wait
166            //   "UserID": 'U'(upper),'s'(lower) → pos=1, word="U", lower "U" → "u"+"serID" = "userID" ✓
167            let word_end = if pos > 1 { pos - 1 } else { 1 };
168            let lower_prefix = pascal[..word_end].to_lowercase();
169            format!("{}{}", lower_prefix, &pascal[word_end..])
170        }
171    }
172}
173
174/// Convert a Rust snake_case name to Java lowerCamelCase convention.
175pub fn to_java_name(name: &str) -> String {
176    name.to_lower_camel_case()
177}
178
179/// Convert a Rust snake_case name to C# PascalCase convention with initialism uppercasing.
180///
181/// Converts snake_case to PascalCase via `heck` and then restores C#-preserved initialisms.
182/// The C# list is intentionally narrow (Microsoft's framework design guidelines prefer
183/// `Json`/`Http`/`Url` over `JSON`/`HTTP`/`URL`), so only product names like `GraphQL`
184/// and short 2-letter abbreviations get all-caps. This keeps method names like
185/// `to_json` → `ToJson` in lockstep with alef's hardcoded `{Type}ToJson` /
186/// `{Type}FromJson` helper declarations.
187pub fn to_csharp_name(name: &str) -> String {
188    apply_initialisms(&name.to_pascal_case(), CSHARP_INITIALISMS)
189}
190
191/// Normalize 3+ letter acronyms at the start of a name to PascalCase.
192///
193/// C# convention: 3+ letter acronyms use PascalCase (Uri, Xml, Json) not all-caps (URI, XML, JSON).
194/// This function detects names like "URI", "XML", "JSON" and converts them to "Uri", "Xml", "Json".
195/// Leaves already-correct names like "Uri" unchanged, and preserves non-acronym names.
196///
197/// Examples:
198/// - `URI`  → `Uri`  (acronym → PascalCase)
199/// - `Uri`  → `Uri`  (already correct)
200/// - `XML`  → `Xml`
201/// - `Xml`  → `Xml`
202/// - `JSON` → `Json`
203/// - `Json` → `Json`
204/// - `HttpStatus` → `HttpStatus` (not an acronym)
205fn normalize_acronym_to_pascalcase(name: &str) -> String {
206    if name.is_empty() {
207        return name.to_string();
208    }
209
210    // Check if the name is all uppercase and 3+ letters (an acronym like "URI", "XML", "JSON")
211    if name.len() >= 3 && name.chars().all(|c| c.is_ascii_uppercase()) {
212        // Convert "URI" → "Uri", "XML" → "Xml", "JSON" → "Json"
213        let mut result = String::with_capacity(name.len());
214        result.push(name.chars().next().unwrap().to_ascii_uppercase());
215        result.extend(name.chars().skip(1).map(|c| c.to_ascii_lowercase()));
216        return result;
217    }
218
219    // Not an all-caps acronym — return as-is
220    name.to_string()
221}
222
223/// Apply C# initialism handling to a name that is already in PascalCase (e.g. an IR type name).
224///
225/// IR type names come directly from Rust PascalCase (e.g. `GraphQLRouteConfig`, `HttpStatus`).
226/// When such names have been processed by `heck::ToPascalCase` they may lose initialism
227/// capitalisation for the names we explicitly preserve (e.g. `GraphQLRouteConfig` →
228/// `GraphQlRouteConfig`). This function restores them.
229///
230/// Examples:
231/// - `GraphQlRouteConfig`   → `GraphQLRouteConfig`
232/// - `GraphQLRouteConfig`   → `GraphQLRouteConfig`  (idempotent)
233/// - `HttpStatus`           → `HttpStatus`          (left alone — `Http` not in `CSHARP_INITIALISMS`)
234pub fn csharp_type_name(name: &str) -> String {
235    // First normalize 3+ letter acronyms to PascalCase (URI → Uri, XML → Xml, JSON → Json)
236    let normalized = normalize_acronym_to_pascalcase(name);
237    // Then apply the preserved initialism rules (GraphQL, ID, UUID)
238    apply_initialisms(&normalized, CSHARP_INITIALISMS)
239}
240
241/// Convert a Rust name to a C-style prefixed snake_case identifier (e.g. `prefix_name`).
242pub fn to_c_name(prefix: &str, name: &str) -> String {
243    format!("{}_{}", prefix, name.to_snake_case())
244}
245
246/// Convert a Rust type name to class name convention for target language.
247pub fn to_class_name(name: &str) -> String {
248    name.to_pascal_case()
249}
250
251/// Convert to SCREAMING_SNAKE for constants.
252pub fn to_constant_name(name: &str) -> String {
253    name.to_shouty_snake_case()
254}
255
256/// Convert a PascalCase or mixed-case name to snake_case with correct acronym handling.
257///
258/// Use this instead of `heck::ToSnakeCase` when the input is a PascalCase Rust type or
259/// enum variant name — `heck` inserts an underscore before every uppercase letter, which
260/// incorrectly splits acronym-style names like `Rdfa` into `rd_fa`.
261///
262/// Rules:
263/// - A run of consecutive uppercase letters is treated as a single acronym word.
264/// - If the run is followed by a lowercase letter, the last uppercase char begins the
265///   next word (e.g. `XMLHttp` → `xml_http`).
266/// - A single uppercase letter followed by lowercase is a normal word start.
267///
268/// Examples:
269/// - `MyType`         → `my_type`
270/// - `Rdfa`           → `rdfa`
271/// - `HTMLParser`     → `html_parser`
272/// - `XMLHttpRequest` → `xml_http_request`
273/// - `IOError`        → `io_error`
274/// - `URLPath`        → `url_path`
275/// - `JSONLD`         → `jsonld`
276pub fn pascal_to_snake(name: &str) -> String {
277    if name.is_empty() {
278        return String::new();
279    }
280    let chars: Vec<char> = name.chars().collect();
281    let n = chars.len();
282    let mut out = String::with_capacity(n + 4);
283    let mut i = 0;
284    while i < n {
285        let ch = chars[i];
286        if ch.is_ascii_uppercase() {
287            let run_start = i;
288            while i < n && chars[i].is_ascii_uppercase() {
289                i += 1;
290            }
291            let run_end = i;
292            let run_len = run_end - run_start;
293            if run_len == 1 {
294                if !out.is_empty() {
295                    out.push('_');
296                }
297                out.extend(chars[run_start].to_lowercase());
298            } else {
299                let split = if i < n && chars[i].is_ascii_lowercase() {
300                    run_len - 1
301                } else {
302                    run_len
303                };
304                if !out.is_empty() {
305                    out.push('_');
306                }
307                for &c in chars.iter().skip(run_start).take(split) {
308                    out.extend(c.to_lowercase());
309                }
310                if split < run_len {
311                    out.push('_');
312                    out.extend(chars[run_start + split].to_lowercase());
313                }
314            }
315        } else {
316            out.push(ch);
317            i += 1;
318        }
319    }
320    out
321}
322
323/// Convert a PascalCase name to SCREAMING_SNAKE_CASE with correct acronym handling.
324///
325/// Examples:
326/// - `MyType`     → `MY_TYPE`
327/// - `Rdfa`       → `RDFA`
328/// - `HTMLParser` → `HTML_PARSER`
329pub fn pascal_to_screaming_snake(name: &str) -> String {
330    pascal_to_snake(name).to_ascii_uppercase()
331}
332
333#[cfg(test)]
334mod tests {
335    use super::*;
336
337    // --- to_go_name (snake_case → Go PascalCase with initialism uppercasing) ---
338
339    #[test]
340    fn test_to_go_name_html_initialism() {
341        assert_eq!(to_go_name("html"), "HTML");
342    }
343
344    #[test]
345    fn test_to_go_name_url_initialism() {
346        assert_eq!(to_go_name("url"), "URL");
347    }
348
349    #[test]
350    fn test_to_go_name_id_initialism() {
351        assert_eq!(to_go_name("id"), "ID");
352    }
353
354    #[test]
355    fn test_to_go_name_plain_word() {
356        assert_eq!(to_go_name("links"), "Links");
357    }
358
359    #[test]
360    fn test_to_go_name_user_id() {
361        assert_eq!(to_go_name("user_id"), "UserID");
362    }
363
364    #[test]
365    fn test_to_go_name_request_url() {
366        assert_eq!(to_go_name("request_url"), "RequestURL");
367    }
368
369    // --- Additional cases ---
370
371    #[test]
372    fn test_to_go_name_http_status() {
373        assert_eq!(to_go_name("http_status"), "HTTPStatus");
374    }
375
376    #[test]
377    fn test_to_go_name_json_body() {
378        assert_eq!(to_go_name("json_body"), "JSONBody");
379    }
380
381    // --- go_param_name (snake_case → Go lowerCamelCase with initialism uppercasing) ---
382
383    #[test]
384    fn test_go_param_name_base_url() {
385        assert_eq!(go_param_name("base_url"), "baseURL");
386    }
387
388    #[test]
389    fn test_go_param_name_user_id() {
390        assert_eq!(go_param_name("user_id"), "userID");
391    }
392
393    #[test]
394    fn test_go_param_name_api_key() {
395        assert_eq!(go_param_name("api_key"), "apiKey");
396    }
397
398    #[test]
399    fn test_go_param_name_plain() {
400        assert_eq!(go_param_name("json"), "json");
401    }
402
403    // --- pascal_to_snake ---
404
405    #[test]
406    fn pascal_to_snake_normal_case() {
407        assert_eq!(pascal_to_snake("MyType"), "my_type");
408    }
409
410    #[test]
411    fn pascal_to_snake_rdfa() {
412        assert_eq!(pascal_to_snake("Rdfa"), "rdfa");
413    }
414
415    #[test]
416    fn pascal_to_snake_html_parser() {
417        assert_eq!(pascal_to_snake("HTMLParser"), "html_parser");
418    }
419
420    #[test]
421    fn pascal_to_snake_xml_http_request() {
422        assert_eq!(pascal_to_snake("XMLHttpRequest"), "xml_http_request");
423    }
424
425    #[test]
426    fn pascal_to_snake_io_error() {
427        assert_eq!(pascal_to_snake("IOError"), "io_error");
428    }
429
430    #[test]
431    fn pascal_to_snake_url_path() {
432        assert_eq!(pascal_to_snake("URLPath"), "url_path");
433    }
434
435    #[test]
436    fn pascal_to_snake_jsonld_all_caps() {
437        assert_eq!(pascal_to_snake("JSONLD"), "jsonld");
438    }
439
440    #[test]
441    fn pascal_to_snake_camel_case() {
442        assert_eq!(pascal_to_snake("myField"), "my_field");
443    }
444
445    #[test]
446    fn pascal_to_snake_already_snake() {
447        assert_eq!(pascal_to_snake("already_snake"), "already_snake");
448    }
449
450    #[test]
451    fn pascal_to_snake_empty() {
452        assert_eq!(pascal_to_snake(""), "");
453    }
454
455    // --- pascal_to_screaming_snake ---
456
457    #[test]
458    fn pascal_to_screaming_snake_rdfa() {
459        assert_eq!(pascal_to_screaming_snake("Rdfa"), "RDFA");
460    }
461
462    #[test]
463    fn pascal_to_screaming_snake_html_parser() {
464        assert_eq!(pascal_to_screaming_snake("HTMLParser"), "HTML_PARSER");
465    }
466
467    #[test]
468    fn pascal_to_screaming_snake_my_type() {
469        assert_eq!(pascal_to_screaming_snake("MyType"), "MY_TYPE");
470    }
471
472    // --- to_csharp_name (snake_case → C# PascalCase with initialism uppercasing) ---
473
474    #[test]
475    fn test_to_csharp_name_graphql_route_config() {
476        assert_eq!(to_csharp_name("graphql_route_config"), "GraphQLRouteConfig");
477    }
478
479    #[test]
480    fn test_to_csharp_name_http_status_no_acronym() {
481        // C# follows Microsoft style — 3+ letter initialisms use PascalCase ("Http"),
482        // not all-caps ("HTTP"). Only product names like GraphQL get all-caps.
483        assert_eq!(to_csharp_name("http_status"), "HttpStatus");
484    }
485
486    #[test]
487    fn test_to_csharp_name_to_json_no_acronym() {
488        // Keeps `to_json` → `ToJson` so it matches alef's hardcoded helper names
489        // (`{Type}ToJson`, `{Type}FromJson`) on the FFI declaration side.
490        assert_eq!(to_csharp_name("to_json"), "ToJson");
491    }
492
493    #[test]
494    fn test_to_csharp_name_plain() {
495        assert_eq!(to_csharp_name("my_field"), "MyField");
496    }
497
498    // --- csharp_type_name (PascalCase → C# PascalCase with initialism uppercasing) ---
499
500    #[test]
501    fn test_csharp_type_name_heck_corrupted() {
502        // heck produces "GraphQlRouteConfig" from "GraphQLRouteConfig" — we must restore it
503        assert_eq!(csharp_type_name("GraphQlRouteConfig"), "GraphQLRouteConfig");
504    }
505
506    #[test]
507    fn test_csharp_type_name_already_correct() {
508        // Input that already has the correct form is preserved idempotently
509        assert_eq!(csharp_type_name("GraphQLRouteConfig"), "GraphQLRouteConfig");
510    }
511
512    #[test]
513    fn test_csharp_type_name_http_status_no_acronym() {
514        // `Http` is intentionally not in CSHARP_INITIALISMS — Microsoft style prefers `Http`.
515        assert_eq!(csharp_type_name("HttpStatus"), "HttpStatus");
516    }
517
518    #[test]
519    fn test_csharp_type_name_three_letter_acronyms() {
520        // 3+ letter acronyms should NOT be uppercased (Uri not URI, Xml not XML, Json not JSON)
521        assert_eq!(csharp_type_name("Uri"), "Uri");
522        assert_eq!(csharp_type_name("URI"), "Uri");
523        assert_eq!(csharp_type_name("Xml"), "Xml");
524        assert_eq!(csharp_type_name("XML"), "Xml");
525        assert_eq!(csharp_type_name("Json"), "Json");
526        assert_eq!(csharp_type_name("JSON"), "Json");
527    }
528}