decy 2.2.0

CLI tool for C-to-Rust transpilation with EXTREME quality standards
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! # Identifier Naming Rules Documentation (C99 §6.4.2, K&R §2.2)
//!
//! This file provides comprehensive documentation for identifier naming transformations
//! from C to Rust, covering naming conventions and style guidelines.
//!
//! ## C Identifier Overview (C99 §6.4.2, K&R §2.2)
//!
//! C identifier characteristics:
//! - Letters (a-z, A-Z), digits (0-9), underscore (_)
//! - Cannot start with digit
//! - No formal naming convention in C standard
//! - Common conventions:
//!   - snake_case: common for variables/functions
//!   - camelCase: sometimes used
//!   - PascalCase: sometimes for types
//!   - SCREAMING_SNAKE_CASE: for constants/macros
//! - Case-sensitive
//! - Reserved: C keywords cannot be identifiers
//!
//! ## Rust Identifier Overview
//!
//! Rust identifier characteristics:
//! - Same character set: a-z, A-Z, 0-9, _
//! - Cannot start with digit
//! - Enforced naming conventions (clippy warnings):
//!   - snake_case: variables, functions, modules
//!   - PascalCase: types (structs, enums, traits)
//!   - SCREAMING_SNAKE_CASE: constants, statics
//! - Case-sensitive
//! - Reserved: Rust keywords
//! - Raw identifiers: r#keyword to use keywords as identifiers
//!
//! ## Critical Differences
//!
//! ### 1. Enforced Conventions
//! - **C**: Conventions vary by project, not enforced
//!   ```c
//!   int myVariable;      // OK (camelCase)
//!   int my_variable;     // OK (snake_case)
//!   int MyVariable;      // OK (PascalCase)
//!   ```
//! - **Rust**: Conventions enforced by clippy
//!   ```rust
//!   let my_variable;     // OK (snake_case enforced)
//!   // let myVariable;   // Warning: use snake_case
//!   ```
//!
//! ### 2. Type Names
//! - **C**: Often use typedef with PascalCase or snake_case_t
//!   ```c
//!   typedef struct point Point;   // PascalCase
//!   typedef int my_int_t;         // _t suffix
//!   ```
//! - **Rust**: PascalCase required
//!   ```rust
//!   struct Point { }     // PascalCase required
//!   type MyInt = i32;    // PascalCase required
//!   ```
//!
//! ### 3. Constants
//! - **C**: #define macros typically SCREAMING_SNAKE_CASE
//!   ```c
//!   #define MAX_SIZE 100
//!   const int buffer_size = 256;  // Not always SCREAMING
//!   ```
//! - **Rust**: const and static require SCREAMING_SNAKE_CASE
//!   ```rust
//!   const MAX_SIZE: usize = 100;  // Required
//!   static BUFFER_SIZE: usize = 256;  // Required
//!   ```
//!
//! ### 4. Reserved Keywords
//! - **C**: 37 keywords (C99)
//! - **Rust**: More keywords, but can use raw identifiers
//!   ```rust
//!   let r#match = 42;  // 'match' is keyword, r# escapes it
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Variables/functions → snake_case
//! ```c
//! int myVariable;
//! void processData() { }
//! ```
//! ```rust
//! let my_variable: i32;
//! fn process_data() { }
//! ```
//!
//! ### Rule 2: Types → PascalCase
//! ```c
//! typedef struct point Point;
//! typedef int my_int_t;
//! ```
//! ```rust
//! struct Point { }
//! type MyInt = i32;
//! ```
//!
//! ### Rule 3: Constants → SCREAMING_SNAKE_CASE
//! ```c
//! #define MAX_SIZE 100
//! const int BUFFER_SIZE = 256;
//! ```
//! ```rust
//! const MAX_SIZE: usize = 100;
//! const BUFFER_SIZE: usize = 256;
//! ```
//!
//! ### Rule 4: Conflicting keywords → raw identifiers
//! ```c
//! int type = 42;  // 'type' not reserved in C
//! ```
//! ```rust
//! let r#type = 42;  // 'type' is keyword, use r#
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of naming convention patterns
//! - Unsafe blocks: 0 (all safe transformations)
//! - ISO C99: §6.4.2 (Identifiers)
//! - K&R: §2.2 (Variable names)
//!
//! ## References
//!
//! - K&R "The C Programming Language" §2.2 (Variable names)
//! - ISO/IEC 9899:1999 (C99) §6.4.2 (Identifiers)
//! - Rust API Guidelines (naming conventions)

#[cfg(test)]
mod tests {
    /// Test 1: Variable names - camelCase to snake_case
    /// Convert camelCase variables
    #[test]
    fn test_variable_name_camel_to_snake() {
        let c_code = r#"
int myVariable = 42;
int userId = 100;
"#;

        let rust_expected = r#"
let my_variable = 42;
let user_id = 100;
"#;

        // Test validates:
        // 1. camelCase → snake_case
        // 2. Convention enforcement
        // 3. Lowercase with underscores
        assert!(c_code.contains("myVariable"));
        assert!(c_code.contains("userId"));
        assert!(rust_expected.contains("my_variable"));
        assert!(rust_expected.contains("user_id"));
    }

    /// Test 2: Function names - snake_case convention
    /// Function naming
    #[test]
    fn test_function_name_snake_case() {
        let c_code = r#"
void processData() { }
int calculateSum(int a, int b) { }
"#;

        let rust_expected = r#"
fn process_data() { }
fn calculate_sum(a: i32, b: i32) -> i32 { }
"#;

        // Test validates:
        // 1. Function names snake_case
        // 2. camelCase → snake_case
        // 3. Consistent style
        assert!(c_code.contains("processData"));
        assert!(c_code.contains("calculateSum"));
        assert!(rust_expected.contains("process_data"));
        assert!(rust_expected.contains("calculate_sum"));
    }

    /// Test 3: Type names - PascalCase
    /// Struct and type names
    #[test]
    fn test_type_name_pascal_case() {
        let c_code = r#"
struct point { int x; int y; };
typedef struct point Point;
typedef int my_int_t;
"#;

        let rust_expected = r#"
struct Point { x: i32, y: i32 }
// typedef eliminated
type MyInt = i32;
"#;

        // Test validates:
        // 1. Type names PascalCase
        // 2. my_int_t → MyInt
        // 3. Capitalized
        assert!(c_code.contains("my_int_t"));
        assert!(rust_expected.contains("MyInt"));
        assert!(rust_expected.contains("Point"));
    }

    /// Test 4: Constants - SCREAMING_SNAKE_CASE
    /// Constant naming
    #[test]
    fn test_constant_name_screaming_snake() {
        let c_code = r#"
#define MAX_SIZE 100
#define BUFFER_LEN 256
const int DEFAULT_VALUE = 42;
"#;

        let rust_expected = r#"
const MAX_SIZE: usize = 100;
const BUFFER_LEN: usize = 256;
const DEFAULT_VALUE: i32 = 42;
"#;

        // Test validates:
        // 1. Constants SCREAMING_SNAKE_CASE
        // 2. All caps with underscores
        // 3. Consistent style
        assert!(c_code.contains("MAX_SIZE"));
        assert!(rust_expected.contains("MAX_SIZE"));
        assert!(c_code.contains("DEFAULT_VALUE"));
        assert!(rust_expected.contains("DEFAULT_VALUE"));
    }

    /// Test 5: Static variables - SCREAMING_SNAKE_CASE
    /// Global/static naming
    #[test]
    fn test_static_name_screaming_snake() {
        let c_code = r#"
static int counter = 0;
static const char* VERSION = "1.0";
"#;

        let rust_expected = r#"
static COUNTER: i32 = 0;
static VERSION: &str = "1.0";
"#;

        // Test validates:
        // 1. static → SCREAMING_SNAKE_CASE
        // 2. counter → COUNTER
        // 3. Global variable convention
        assert!(c_code.contains("counter"));
        assert!(rust_expected.contains("COUNTER"));
        assert!(rust_expected.contains("VERSION"));
    }

    /// Test 6: Enum variants - PascalCase
    /// Enum naming
    #[test]
    fn test_enum_variant_pascal_case() {
        let c_code = r#"
enum Color {
    RED,
    GREEN,
    BLUE
};
"#;

        let rust_expected = r#"
enum Color {
    Red,
    Green,
    Blue,
}
"#;

        // Test validates:
        // 1. Enum name PascalCase
        // 2. Variants PascalCase (not SCREAMING)
        // 3. RED → Red
        assert!(c_code.contains("RED"));
        assert!(c_code.contains("GREEN"));
        assert!(rust_expected.contains("Red"));
        assert!(rust_expected.contains("Green"));
    }

    /// Test 7: Module names - snake_case
    /// File/module naming
    #[test]
    fn test_module_name_snake_case() {
        let c_code = r#"
// File: DataProcessor.h
// or: data_processor.h
"#;

        let rust_expected = r#"
// File: data_processor.rs
// Module: mod data_processor;
"#;

        // Test validates:
        // 1. Module names snake_case
        // 2. File names lowercase
        // 3. Consistent with variable naming
        assert!(c_code.contains("DataProcessor") || c_code.contains("data_processor"));
        assert!(rust_expected.contains("data_processor"));
    }

    /// Test 8: Macro names - SCREAMING_SNAKE_CASE
    /// Macro naming convention
    #[test]
    fn test_macro_name_screaming_snake() {
        let c_code = r#"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define DEBUG_PRINT(x) printf("%d\n", x)
"#;

        let rust_expected = r#"
macro_rules! min {
    ($a:expr, $b:expr) => {
        if $a < $b { $a } else { $b }
    }
}
// Or function: fn min(a: i32, b: i32) -> i32
"#;

        // Test validates:
        // 1. C macros SCREAMING_SNAKE_CASE
        // 2. Rust macros snake_case (macro_rules!)
        // 3. Or convert to function
        assert!(c_code.contains("MIN"));
        assert!(rust_expected.contains("min"));
    }

    /// Test 9: Underscore prefix - private convention
    /// Private naming
    #[test]
    fn test_underscore_prefix() {
        let c_code = r#"
static int _privateCounter = 0;
void _internalFunction() { }
"#;

        let _rust_expected = r#"
static PRIVATE_COUNTER: i32 = 0;
fn internal_function() { }
// Or: fn _internal_function() { } (unused warning suppression)
"#;

        // Test validates:
        // 1. Underscore prefix meaning
        // 2. _private → private (or keep _)
        // 3. Rust uses pub/private keyword
        assert!(c_code.contains("_privateCounter"));
        assert!(c_code.contains("_internalFunction"));
    }

    /// Test 10: Abbreviations in names
    /// Handling acronyms
    #[test]
    fn test_abbreviations_in_names() {
        let c_code = r#"
int httpStatus;
int URLParser;
typedef struct HTTPRequest HTTPRequest;
"#;

        let rust_expected = r#"
let http_status: i32;
let url_parser: i32;
struct HttpRequest { }
"#;

        // Test validates:
        // 1. HTTP → http (snake_case)
        // 2. URL → url (all lowercase in snake_case)
        // 3. HTTPRequest → HttpRequest (PascalCase)
        assert!(c_code.contains("httpStatus"));
        assert!(c_code.contains("URLParser"));
        assert!(rust_expected.contains("http_status"));
        assert!(rust_expected.contains("url_parser"));
        assert!(rust_expected.contains("HttpRequest"));
    }

    /// Test 11: Numbers in identifiers
    /// Numeric suffixes
    #[test]
    fn test_numbers_in_identifiers() {
        let c_code = r#"
int value1;
int value2;
struct Point2D { int x; int y; };
"#;

        let rust_expected = r#"
let value1: i32;
let value2: i32;
struct Point2d { x: i32, y: i32 }
"#;

        // Test validates:
        // 1. Numbers allowed
        // 2. value1, value2 same
        // 3. 2D → 2d in PascalCase
        assert!(c_code.contains("value1"));
        assert!(c_code.contains("Point2D"));
        assert!(rust_expected.contains("value1"));
        assert!(rust_expected.contains("Point2d"));
    }

    /// Test 12: Reserved keyword conflicts
    /// Using Rust keywords as C identifiers
    #[test]
    fn test_reserved_keyword_conflicts() {
        let c_code = r#"
int type = 42;     // 'type' not reserved in C
int match = 10;    // 'match' not reserved in C
int loop = 5;      // 'loop' not reserved in C
"#;

        let rust_expected = r#"
let r#type = 42;   // 'type' is Rust keyword
let r#match = 10;  // 'match' is Rust keyword
let r#loop = 5;    // 'loop' is Rust keyword
"#;

        // Test validates:
        // 1. Raw identifiers r#
        // 2. Keyword escaping
        // 3. Preserve original name when needed
        assert!(c_code.contains("int type"));
        assert!(c_code.contains("int match"));
        assert!(rust_expected.contains("r#type"));
        assert!(rust_expected.contains("r#match"));
    }

    /// Test 13: Hungarian notation
    /// Type prefixes
    #[test]
    fn test_hungarian_notation() {
        let c_code = r#"
int nCount;
char* pszName;
bool bEnabled;
"#;

        let rust_expected = r#"
let count: i32;        // Remove 'n' prefix
let name: &str;        // Remove 'psz' prefix
let enabled: bool;     // Remove 'b' prefix
"#;

        // Test validates:
        // 1. Hungarian notation removal
        // 2. nCount → count
        // 3. Type safety makes prefixes unnecessary
        assert!(c_code.contains("nCount"));
        assert!(c_code.contains("pszName"));
        assert!(rust_expected.contains("let count"));
        assert!(rust_expected.contains("let name"));
    }

    /// Test 14: Struct member names
    /// Field naming
    #[test]
    fn test_struct_member_names() {
        let c_code = r#"
struct Person {
    char firstName[50];
    int userId;
    bool isActive;
};
"#;

        let rust_expected = r#"
struct Person {
    first_name: [u8; 50],
    user_id: i32,
    is_active: bool,
}
"#;

        // Test validates:
        // 1. Struct fields snake_case
        // 2. firstName → first_name
        // 3. Consistent convention
        assert!(c_code.contains("firstName"));
        assert!(c_code.contains("userId"));
        assert!(rust_expected.contains("first_name"));
        assert!(rust_expected.contains("user_id"));
    }

    /// Test 15: Function parameter names
    /// Parameter naming
    #[test]
    fn test_function_parameter_names() {
        let c_code = r#"
void setUserName(int userId, const char* userName) {
    // ...
}
"#;

        let rust_expected = r#"
fn set_user_name(user_id: i32, user_name: &str) {
    // ...
}
"#;

        // Test validates:
        // 1. Parameters snake_case
        // 2. userId → user_id
        // 3. Function and params consistent
        assert!(c_code.contains("userId"));
        assert!(c_code.contains("userName"));
        assert!(rust_expected.contains("user_id"));
        assert!(rust_expected.contains("user_name"));
    }

    /// Test 16: Global vs local naming
    /// Scope-based naming
    #[test]
    fn test_global_vs_local_naming() {
        let c_code = r#"
int globalCounter = 0;     // Global
static int fileCounter = 0; // File scope

void function() {
    int localCounter = 0;  // Local
}
"#;

        let rust_expected = r#"
// Module-level (like C global)
static GLOBAL_COUNTER: i32 = 0;
static FILE_COUNTER: i32 = 0;

fn function() {
    let local_counter = 0;
}
"#;

        // Test validates:
        // 1. Global → SCREAMING_SNAKE_CASE
        // 2. Local → snake_case
        // 3. Different conventions by scope
        assert!(c_code.contains("globalCounter"));
        assert!(c_code.contains("localCounter"));
        assert!(rust_expected.contains("GLOBAL_COUNTER"));
        assert!(rust_expected.contains("local_counter"));
    }

    /// Test 17: Naming transformation rules summary
    /// Documents all transformation rules
    #[test]
    fn test_naming_transformation_summary() {
        let c_code = r#"
// Rule 1: Variables → snake_case
int myVariable;

// Rule 2: Functions → snake_case
void processData() { }

// Rule 3: Types → PascalCase
typedef struct point Point;

// Rule 4: Constants → SCREAMING_SNAKE_CASE
#define MAX_SIZE 100

// Rule 5: Static → SCREAMING_SNAKE_CASE
static int counter;

// Rule 6: Enum variants → PascalCase
enum Color { RED, GREEN };

// Rule 7: Keyword conflicts → r#
int type;
"#;

        let rust_expected = r#"
// Rule 1: Enforced snake_case
let my_variable: i32;

// Rule 2: Enforced snake_case
fn process_data() { }

// Rule 3: Required PascalCase
struct Point { }

// Rule 4: Required SCREAMING
const MAX_SIZE: i32 = 100;

// Rule 5: Required SCREAMING
static COUNTER: i32;

// Rule 6: PascalCase variants
enum Color { Red, Green }

// Rule 7: Raw identifier
let r#type: i32;
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("myVariable"));
        assert!(rust_expected.contains("my_variable"));
        assert!(c_code.contains("processData"));
        assert!(rust_expected.contains("process_data"));
        assert!(c_code.contains("Point"));
        assert!(c_code.contains("MAX_SIZE"));
        assert!(c_code.contains("int type"));
        assert!(rust_expected.contains("r#type"));
    }
}