audb-codegen 0.1.11

Code generation for AuDB compile-time database applications
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Naming utilities for code generation
//!
//! This module provides utilities for converting schema names to collection names,
//! pluralizing entity names, and converting between naming conventions.
//!
//! ## Naming Conventions
//!
//! - **Schema names**: PascalCase (e.g., `User`, `BlogPost`)
//! - **Collection names**: Pluralized snake_case (e.g., `users`, `blog_posts`)
//! - **Field names**: snake_case (e.g., `user_id`, `created_at`)
//!
//! ## Examples
//!
//! ```
//! use audb_codegen::naming::{collection_name, pluralize, to_snake_case};
//!
//! assert_eq!(collection_name("User"), "users");
//! assert_eq!(collection_name("BlogPost"), "blog_posts");
//! assert_eq!(pluralize("person"), "people");
//! assert_eq!(to_snake_case("BlogPost"), "blog_post");
//! ```

use std::collections::HashMap;

/// Convert a PascalCase or camelCase string to snake_case
///
/// This function handles various edge cases including:
/// - Consecutive uppercase letters (e.g., `HTTPRequest` → `http_request`)
/// - Numbers (e.g., `User2FA` → `user2_fa`)
/// - Already snake_case strings (returned unchanged)
///
/// # Examples
///
/// ```
/// # use audb_codegen::naming::to_snake_case;
/// assert_eq!(to_snake_case("User"), "user");
/// assert_eq!(to_snake_case("BlogPost"), "blog_post");
/// assert_eq!(to_snake_case("HTTPRequest"), "http_request");
/// assert_eq!(to_snake_case("User2FA"), "user2_fa");
/// assert_eq!(to_snake_case("already_snake_case"), "already_snake_case");
/// assert_eq!(to_snake_case(""), "");
/// ```
pub fn to_snake_case(s: &str) -> String {
    if s.is_empty() {
        return String::new();
    }

    let mut result = String::with_capacity(s.len() + 4);
    let chars: Vec<char> = s.chars().collect();

    for (i, &ch) in chars.iter().enumerate() {
        if ch.is_uppercase() {
            // Don't add underscore at the start
            if i > 0 {
                let prev = chars[i - 1];
                let next = chars.get(i + 1);

                // Add underscore before uppercase letter if:
                // 1. Previous char is lowercase or digit
                // 2. Previous char is uppercase AND next char is lowercase (handles HTTPRequest)
                if prev.is_lowercase() || prev.is_ascii_digit() {
                    result.push('_');
                } else if prev.is_uppercase() && next.map_or(false, |c| c.is_lowercase()) {
                    result.push('_');
                }
            }
            result.push(ch.to_ascii_lowercase());
        } else {
            result.push(ch);
        }
    }

    result
}

/// Pluralize an English word using common rules
///
/// This function handles most common English pluralization rules including:
/// - Regular plurals (add 's')
/// - Words ending in s, x, z, ch, sh (add 'es')
/// - Words ending in consonant + y (change y to 'ies')
/// - Irregular plurals (person → people, child → children, etc.)
/// - Uncountable nouns (data, information, etc.)
///
/// # Examples
///
/// ```
/// # use audb_codegen::naming::pluralize;
/// // Regular plurals
/// assert_eq!(pluralize("user"), "users");
/// assert_eq!(pluralize("post"), "posts");
///
/// // Words ending in s, x, z, ch, sh
/// assert_eq!(pluralize("box"), "boxes");
/// assert_eq!(pluralize("class"), "classes");
/// assert_eq!(pluralize("church"), "churches");
///
/// // Words ending in consonant + y
/// assert_eq!(pluralize("story"), "stories");
/// assert_eq!(pluralize("category"), "categories");
///
/// // Irregular plurals
/// assert_eq!(pluralize("person"), "people");
/// assert_eq!(pluralize("child"), "children");
/// assert_eq!(pluralize("mouse"), "mice");
///
/// // Uncountable nouns
/// assert_eq!(pluralize("data"), "data");
/// assert_eq!(pluralize("information"), "information");
/// ```
pub fn pluralize(word: &str) -> String {
    if word.is_empty() {
        return String::new();
    }

    let lower = word.to_lowercase();

    // Irregular plurals
    let irregulars = get_irregular_plurals();
    if let Some(plural) = irregulars.get(lower.as_str()) {
        return apply_case_pattern(word, plural);
    }

    // Uncountable nouns (already plural or no plural form)
    if is_uncountable(&lower) {
        return word.to_string();
    }

    // Words ending in 's', 'ss', 'x', 'z', 'ch', 'sh' → add 'es'
    if lower.ends_with("s")
        || lower.ends_with("ss")
        || lower.ends_with("x")
        || lower.ends_with("z")
        || lower.ends_with("ch")
        || lower.ends_with("sh")
    {
        return format!("{}es", word);
    }

    // Words ending in consonant + 'y' → change 'y' to 'ies'
    if lower.ends_with('y') && lower.len() > 1 {
        let before_y = lower.chars().rev().nth(1).unwrap();
        if !is_vowel(before_y) {
            let stem = &word[..word.len() - 1];
            return format!("{}ies", stem);
        }
    }

    // Words ending in 'f' or 'fe' → change to 'ves'
    if lower.ends_with("fe") {
        let stem = &word[..word.len() - 2];
        return format!("{}ves", stem);
    }
    if lower.ends_with('f') {
        let stem = &word[..word.len() - 1];
        return format!("{}ves", stem);
    }

    // Words ending in consonant + 'o' → add 'es'
    if lower.ends_with('o') && lower.len() > 1 {
        let before_o = lower.chars().rev().nth(1).unwrap();
        if !is_vowel(before_o) {
            return format!("{}es", word);
        }
    }

    // Default: add 's'
    format!("{}s", word)
}

/// Generate a collection name from a schema name
///
/// Converts a PascalCase schema name to a pluralized snake_case collection name.
///
/// # Examples
///
/// ```
/// # use audb_codegen::naming::collection_name;
/// assert_eq!(collection_name("User"), "users");
/// assert_eq!(collection_name("BlogPost"), "blog_posts");
/// assert_eq!(collection_name("Person"), "people");
/// assert_eq!(collection_name("Category"), "categories");
/// ```
pub fn collection_name(schema_name: &str) -> String {
    let snake = to_snake_case(schema_name);
    pluralize(&snake)
}

/// Check if a character is a vowel
fn is_vowel(ch: char) -> bool {
    matches!(ch, 'a' | 'e' | 'i' | 'o' | 'u')
}

/// Check if a word is uncountable (no plural form or same as plural)
fn is_uncountable(word: &str) -> bool {
    matches!(
        word,
        "data"
            | "information"
            | "equipment"
            | "furniture"
            | "luggage"
            | "software"
            | "hardware"
            | "feedback"
            | "progress"
            | "research"
            | "evidence"
            | "news"
            | "series"
            | "species"
            | "sheep"
            | "deer"
            | "fish"
    )
}

/// Get map of irregular plural forms
fn get_irregular_plurals() -> HashMap<&'static str, &'static str> {
    let mut map = HashMap::new();

    // Common irregular plurals
    map.insert("person", "people");
    map.insert("man", "men");
    map.insert("woman", "women");
    map.insert("child", "children");
    map.insert("tooth", "teeth");
    map.insert("foot", "feet");
    map.insert("mouse", "mice");
    map.insert("goose", "geese");
    map.insert("ox", "oxen");
    map.insert("leaf", "leaves");
    map.insert("life", "lives");
    map.insert("wife", "wives");
    map.insert("knife", "knives");
    map.insert("half", "halves");
    map.insert("self", "selves");
    map.insert("elf", "elves");
    map.insert("loaf", "loaves");
    map.insert("potato", "potatoes");
    map.insert("tomato", "tomatoes");
    map.insert("cactus", "cacti");
    map.insert("focus", "foci");
    map.insert("fungus", "fungi");
    map.insert("nucleus", "nuclei");
    map.insert("radius", "radii");
    map.insert("alumnus", "alumni");
    map.insert("analysis", "analyses");
    map.insert("diagnosis", "diagnoses");
    map.insert("thesis", "theses");
    map.insert("crisis", "crises");
    map.insert("phenomenon", "phenomena");
    map.insert("criterion", "criteria");
    map.insert("datum", "data");

    map
}

/// Apply the original word's case pattern to the plural form
///
/// This preserves capitalization when pluralizing (e.g., "Person" → "People", not "people")
fn apply_case_pattern(original: &str, plural: &str) -> String {
    if original.is_empty() {
        return plural.to_string();
    }

    // If original is all uppercase, return uppercase plural
    if original
        .chars()
        .all(|c| !c.is_alphabetic() || c.is_uppercase())
    {
        return plural.to_uppercase();
    }

    // If original starts with uppercase, capitalize plural
    if original.chars().next().unwrap().is_uppercase() {
        let mut chars = plural.chars();
        match chars.next() {
            Some(first) => first.to_uppercase().chain(chars).collect(),
            None => String::new(),
        }
    } else {
        plural.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_to_snake_case_simple() {
        assert_eq!(to_snake_case("User"), "user");
        assert_eq!(to_snake_case("BlogPost"), "blog_post");
        assert_eq!(to_snake_case("HTTPRequest"), "http_request");
        assert_eq!(to_snake_case("XMLParser"), "xml_parser");
    }

    #[test]
    fn test_to_snake_case_with_numbers() {
        assert_eq!(to_snake_case("User2FA"), "user2_fa");
        assert_eq!(to_snake_case("OAuth2Client"), "o_auth2_client");
    }

    #[test]
    fn test_to_snake_case_already_snake() {
        assert_eq!(to_snake_case("already_snake_case"), "already_snake_case");
        assert_eq!(to_snake_case("user"), "user");
    }

    #[test]
    fn test_to_snake_case_edge_cases() {
        assert_eq!(to_snake_case(""), "");
        assert_eq!(to_snake_case("A"), "a");
        assert_eq!(to_snake_case("AB"), "ab");
        assert_eq!(to_snake_case("ABC"), "abc");
    }

    #[test]
    fn test_pluralize_regular() {
        assert_eq!(pluralize("user"), "users");
        assert_eq!(pluralize("post"), "posts");
        assert_eq!(pluralize("item"), "items");
        assert_eq!(pluralize("tag"), "tags");
    }

    #[test]
    fn test_pluralize_sibilants() {
        assert_eq!(pluralize("class"), "classes");
        assert_eq!(pluralize("box"), "boxes");
        assert_eq!(pluralize("buzz"), "buzzes");
        assert_eq!(pluralize("church"), "churches");
        assert_eq!(pluralize("dish"), "dishes");
    }

    #[test]
    fn test_pluralize_consonant_y() {
        assert_eq!(pluralize("story"), "stories");
        assert_eq!(pluralize("category"), "categories");
        assert_eq!(pluralize("company"), "companies");
    }

    #[test]
    fn test_pluralize_vowel_y() {
        assert_eq!(pluralize("boy"), "boys");
        assert_eq!(pluralize("day"), "days");
        assert_eq!(pluralize("key"), "keys");
    }

    #[test]
    fn test_pluralize_f_fe() {
        assert_eq!(pluralize("leaf"), "leaves");
        assert_eq!(pluralize("knife"), "knives");
        assert_eq!(pluralize("life"), "lives");
        assert_eq!(pluralize("wife"), "wives");
    }

    #[test]
    fn test_pluralize_consonant_o() {
        assert_eq!(pluralize("hero"), "heroes");
        assert_eq!(pluralize("potato"), "potatoes");
        assert_eq!(pluralize("tomato"), "tomatoes");
    }

    #[test]
    fn test_pluralize_vowel_o() {
        assert_eq!(pluralize("video"), "videos");
        assert_eq!(pluralize("stereo"), "stereos");
    }

    #[test]
    fn test_pluralize_irregular() {
        assert_eq!(pluralize("person"), "people");
        assert_eq!(pluralize("man"), "men");
        assert_eq!(pluralize("woman"), "women");
        assert_eq!(pluralize("child"), "children");
        assert_eq!(pluralize("tooth"), "teeth");
        assert_eq!(pluralize("foot"), "feet");
        assert_eq!(pluralize("mouse"), "mice");
        assert_eq!(pluralize("goose"), "geese");
    }

    #[test]
    fn test_pluralize_irregular_capitalized() {
        assert_eq!(pluralize("Person"), "People");
        assert_eq!(pluralize("Child"), "Children");
        assert_eq!(pluralize("PERSON"), "PEOPLE");
    }

    #[test]
    fn test_pluralize_uncountable() {
        assert_eq!(pluralize("data"), "data");
        assert_eq!(pluralize("information"), "information");
        assert_eq!(pluralize("equipment"), "equipment");
        assert_eq!(pluralize("sheep"), "sheep");
        assert_eq!(pluralize("deer"), "deer");
        assert_eq!(pluralize("fish"), "fish");
    }

    #[test]
    fn test_pluralize_edge_cases() {
        assert_eq!(pluralize(""), "");
        assert_eq!(pluralize("a"), "as");
        assert_eq!(pluralize("I"), "Is");
    }

    #[test]
    fn test_collection_name() {
        assert_eq!(collection_name("User"), "users");
        assert_eq!(collection_name("BlogPost"), "blog_posts");
        assert_eq!(collection_name("Person"), "people");
        assert_eq!(collection_name("Category"), "categories");
        assert_eq!(collection_name("Company"), "companies");
    }

    #[test]
    fn test_collection_name_complex() {
        assert_eq!(collection_name("HTTPRequest"), "http_requests");
        assert_eq!(collection_name("XMLDocument"), "xml_documents");
        assert_eq!(collection_name("OAuth2Client"), "o_auth2_clients");
    }

    #[test]
    fn test_is_vowel() {
        assert!(is_vowel('a'));
        assert!(is_vowel('e'));
        assert!(is_vowel('i'));
        assert!(is_vowel('o'));
        assert!(is_vowel('u'));
        assert!(!is_vowel('b'));
        assert!(!is_vowel('x'));
    }

    #[test]
    fn test_is_uncountable() {
        assert!(is_uncountable("data"));
        assert!(is_uncountable("information"));
        assert!(is_uncountable("sheep"));
        assert!(!is_uncountable("user"));
        assert!(!is_uncountable("post"));
    }

    #[test]
    fn test_apply_case_pattern() {
        assert_eq!(apply_case_pattern("Person", "people"), "People");
        assert_eq!(apply_case_pattern("person", "people"), "people");
        assert_eq!(apply_case_pattern("PERSON", "people"), "PEOPLE");
        assert_eq!(apply_case_pattern("Child", "children"), "Children");
    }

    #[test]
    fn test_latin_plurals() {
        assert_eq!(pluralize("cactus"), "cacti");
        assert_eq!(pluralize("focus"), "foci");
        assert_eq!(pluralize("radius"), "radii");
        assert_eq!(pluralize("alumnus"), "alumni");
    }

    #[test]
    fn test_greek_plurals() {
        assert_eq!(pluralize("analysis"), "analyses");
        assert_eq!(pluralize("thesis"), "theses");
        assert_eq!(pluralize("crisis"), "crises");
        assert_eq!(pluralize("phenomenon"), "phenomena");
        assert_eq!(pluralize("criterion"), "criteria");
    }
}