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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//! # Function Declarations and Definitions Documentation (C99 §6.9.1, K&R §4.1)
//!
//! This file provides comprehensive documentation for function declaration and definition
//! transformations from C to Rust, covering all function patterns and parameter types.
//!
//! ## C Functions Overview (C99 §6.9.1, K&R §4.1)
//!
//! C function characteristics:
//! - Declarations: prototype specifying signature (`;` terminated)
//! - Definitions: implementation with body (`{}`)
//! - Return type: any type or `void`
//! - Parameters: typed list or `void` for no parameters
//! - Old-style K&R: parameters declared separately (deprecated)
//! - Default return type: `int` if omitted (C89, removed in C99)
//!
//! ## Rust Functions Overview
//!
//! Rust function characteristics:
//! - No separate declarations: definitions are declarations
//! - Return type: `-> Type` or omit for unit type `()`
//! - Parameters: always explicitly typed
//! - No void: empty param list `()` for no parameters
//! - Explicit returns: `return` keyword or implicit (expression)
//! - Type-safe: no implicit conversions
//!
//! ## Critical Differences
//!
//! ### 1. Declaration vs Definition
//! - **C**: Separate declarations (`.h`) and definitions (`.c`)
//!   ```c
//!   // header.h
//!   int add(int a, int b);  // Declaration
//!
//!   // source.c
//!   int add(int a, int b) { return a + b; }  // Definition
//!   ```
//! - **Rust**: Definition is declaration (modules instead of headers)
//!   ```rust
//!   fn add(a: i32, b: i32) -> i32 { a + b }  // Definition = declaration
//!   ```
//!
//! ### 2. Void vs Unit
//! - **C**: `void` for no return value
//!   ```c
//!   void print_message() { printf("Hello\n"); }
//!   ```
//! - **Rust**: Omit return type (defaults to unit `()`)
//!   ```rust
//!   fn print_message() { println!("Hello"); }  // Returns ()
//!   ```
//!
//! ### 3. Parameter Lists
//! - **C**: `void` required for no parameters
//!   ```c
//!   int get_value(void);  // No parameters
//!   ```
//! - **Rust**: Empty parentheses
//!   ```rust
//!   fn get_value() -> i32 { 42 }  // No parameters
//!   ```
//!
//! ### 4. Return Syntax
//! - **C**: Always use `return` keyword
//!   ```c
//!   int square(int x) { return x * x; }
//!   ```
//! - **Rust**: Implicit return (expression without `;`)
//!   ```rust
//!   fn square(x: i32) -> i32 { x * x }  // Implicit
//!   fn square(x: i32) -> i32 { return x * x; }  // Explicit (less idiomatic)
//!   ```
//!
//! ### 5. Forward Declarations
//! - **C**: Required if function used before defined
//!   ```c
//!   int helper();  // Forward declaration
//!   int main() { return helper(); }
//!   int helper() { return 0; }
//!   ```
//! - **Rust**: Order independent within module
//!   ```rust
//!   # fn helper() { }
//!   // Can call before definition (no forward declaration needed)
//!   helper();
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Simple Function with Return Value
//! ```c
//! int add(int a, int b) {
//!     return a + b;
//! }
//! ```
//! ```rust
//! fn add(a: i32, b: i32) -> i32 {
//!     a + b  // Implicit return
//! }
//! ```
//!
//! ### Rule 2: Void Function (No Return)
//! ```c
//! void print_value(int x) {
//!     printf("%d\n", x);
//! }
//! ```
//! ```rust
//! fn print_value(x: i32) {
//!     println!("{}", x);
//! }  // Omit -> Type for void
//! ```
//!
//! ### Rule 3: No Parameters
//! ```c
//! int get_constant(void) {
//!     return 42;
//! }
//! ```
//! ```rust
//! fn get_constant() -> i32 {
//!     42
//! }
//! ```
//!
//! ### Rule 4: Multiple Parameters
//! ```c
//! int max(int a, int b, int c) {
//!     int m = a;
//!     if (b > m) m = b;
//!     if (c > m) m = c;
//!     return m;
//! }
//! ```
//! ```rust
//! fn max(a: i32, b: i32, c: i32) -> i32 {
//!     let mut m = a;
//!     if b > m { m = b; }
//!     if c > m { m = c; }
//!     m
//! }
//! ```
//!
//! ### Rule 5: Early Return
//! ```c
//! int divide(int a, int b) {
//!     if (b == 0) return -1;
//!     return a / b;
//! }
//! ```
//! ```rust
//! fn divide(a: i32, b: i32) -> i32 {
//!     if b == 0 { return -1; }  // Explicit for early return
//!     a / b  // Implicit for success case
//! }
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of function declaration/definition patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.9.1 (function definitions)
//! - K&R: §4.1
//!
//! ## References
//!
//! - K&R "The C Programming Language" §4.1 (Basics of Functions)
//! - ISO/IEC 9899:1999 (C99) §6.9.1 (Function definitions)
//! - Rust Book: Functions

#[cfg(test)]
mod tests {
    /// Test 1: Simple function returning int
    /// Most basic pattern
    #[test]
    fn test_simple_return_int() {
        let c_code = r#"
int add(int a, int b) {
    return a + b;
}
"#;

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

        // Test validates:
        // 1. Return type syntax (int → i32)
        // 2. Parameter syntax (type after name)
        // 3. Implicit return in Rust
        assert!(c_code.contains("int add(int a, int b)"));
        assert!(rust_expected.contains("fn add(a: i32, b: i32) -> i32"));
        assert!(rust_expected.contains("a + b"));
    }

    /// Test 2: Void function (no return value)
    /// Common for side effects
    #[test]
    fn test_void_function() {
        let c_code = r#"
void print_message(int x) {
    printf("%d\n", x);
}
"#;

        let rust_expected = r#"
fn print_message(x: i32) {
    println!("{}", x);
}
"#;

        // Test validates:
        // 1. void → omit return type
        // 2. Function with side effects
        // 3. No return statement needed
        assert!(c_code.contains("void print_message(int x)"));
        assert!(rust_expected.contains("fn print_message(x: i32)"));
        assert!(!rust_expected.contains("->"));
    }

    /// Test 3: Function with no parameters
    /// Void parameter list in C
    #[test]
    fn test_no_parameters() {
        let c_code = r#"
int get_constant(void) {
    return 42;
}
"#;

        let rust_expected = r#"
fn get_constant() -> i32 {
    42
}
"#;

        // Test validates:
        // 1. void param list → empty ()
        // 2. Constant function pattern
        // 3. Implicit return
        assert!(c_code.contains("get_constant(void)"));
        assert!(rust_expected.contains("get_constant() -> i32"));
    }

    /// Test 4: Function with multiple parameters
    /// Three or more parameters
    #[test]
    fn test_multiple_parameters() {
        let c_code = r#"
int max3(int a, int b, int c) {
    int m = a;
    if (b > m) m = b;
    if (c > m) m = c;
    return m;
}
"#;

        let rust_expected = r#"
fn max3(a: i32, b: i32, c: i32) -> i32 {
    let mut m = a;
    if b > m { m = b; }
    if c > m { m = c; }
    m
}
"#;

        // Test validates:
        // 1. Multiple parameters same syntax
        // 2. Local variable needs mut
        // 3. Implicit return
        assert!(c_code.contains("int max3(int a, int b, int c)"));
        assert!(rust_expected.contains("fn max3(a: i32, b: i32, c: i32)"));
    }

    /// Test 5: Function with early return (guard clause)
    /// Return from middle of function
    #[test]
    fn test_early_return() {
        let c_code = r#"
int divide(int a, int b) {
    if (b == 0) return -1;
    return a / b;
}
"#;

        let rust_expected = r#"
fn divide(a: i32, b: i32) -> i32 {
    if b == 0 { return -1; }
    a / b
}
"#;

        // Test validates:
        // 1. Early return uses explicit return
        // 2. Guard clause pattern
        // 3. Final value implicit return
        assert!(c_code.contains("if (b == 0) return -1"));
        assert!(rust_expected.contains("if b == 0 { return -1; }"));
        assert!(rust_expected.contains("a / b"));
    }

    /// Test 6: Function returning boolean (int in C)
    /// C uses int for boolean
    #[test]
    fn test_boolean_return() {
        let c_code = r#"
int is_positive(int x) {
    if (x > 0) return 1;
    return 0;
}
"#;

        let rust_expected = r#"
fn is_positive(x: i32) -> bool {
    x > 0
}
"#;

        // Test validates:
        // 1. int 0/1 → bool true/false
        // 2. Simplified to direct boolean
        // 3. More idiomatic Rust
        assert!(c_code.contains("int is_positive"));
        assert!(rust_expected.contains("fn is_positive(x: i32) -> bool"));
    }

    /// Test 7: Function with floating point
    /// Different type syntax
    #[test]
    fn test_float_function() {
        let c_code = r#"
float average(float a, float b) {
    return (a + b) / 2.0f;
}
"#;

        let rust_expected = r#"
fn average(a: f32, b: f32) -> f32 {
    (a + b) / 2.0
}
"#;

        // Test validates:
        // 1. float → f32
        // 2. Floating point literals
        // 3. Same arithmetic
        assert!(c_code.contains("float average(float a, float b)"));
        assert!(rust_expected.contains("fn average(a: f32, b: f32) -> f32"));
    }

    /// Test 8: Function with double precision
    /// 64-bit floating point
    #[test]
    fn test_double_function() {
        let c_code = r#"
double square(double x) {
    return x * x;
}
"#;

        let rust_expected = r#"
fn square(x: f64) -> f64 {
    x * x
}
"#;

        // Test validates:
        // 1. double → f64
        // 2. Simple computation
        // 3. Implicit return
        assert!(c_code.contains("double square(double x)"));
        assert!(rust_expected.contains("fn square(x: f64) -> f64"));
    }

    /// Test 9: Function with mixed parameter types
    /// Different types in parameters
    #[test]
    fn test_mixed_parameter_types() {
        let c_code = r#"
float scale(int count, float factor) {
    return count * factor;
}
"#;

        let rust_expected = r#"
fn scale(count: i32, factor: f32) -> f32 {
    count as f32 * factor
}
"#;

        // Test validates:
        // 1. Mixed int and float parameters
        // 2. Explicit cast in Rust
        // 3. Type safety
        assert!(c_code.contains("float scale(int count, float factor)"));
        assert!(rust_expected.contains("fn scale(count: i32, factor: f32)"));
    }

    /// Test 10: Function calling another function
    /// Function composition
    #[test]
    fn test_function_calls_function() {
        let c_code = r#"
int square(int x) {
    return x * x;
}

int sum_of_squares(int a, int b) {
    return square(a) + square(b);
}
"#;

        let rust_expected = r#"
fn square(x: i32) -> i32 {
    x * x
}

fn sum_of_squares(a: i32, b: i32) -> i32 {
    square(a) + square(b)
}
"#;

        // Test validates:
        // 1. Function composition
        // 2. Order independent in Rust (no forward decl needed)
        // 3. Same calling syntax
        assert!(c_code.contains("square(a) + square(b)"));
        assert!(rust_expected.contains("square(a) + square(b)"));
    }

    /// Test 11: Function with complex body
    /// Multiple statements
    #[test]
    fn test_complex_function_body() {
        let c_code = r#"
int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}
"#;

        let rust_expected = r#"
fn factorial(n: i32) -> i32 {
    let mut result = 1;
    for i in 1..=n {
        result *= i;
    }
    result
}
"#;

        // Test validates:
        // 1. Complex multi-statement body
        // 2. Loop inside function
        // 3. Mutable local variable
        assert!(c_code.contains("int result = 1"));
        assert!(rust_expected.contains("let mut result = 1"));
        assert!(rust_expected.contains("result"));
    }

    /// Test 12: Recursive function
    /// Function calling itself
    #[test]
    fn test_recursive_function() {
        let c_code = r#"
int fibonacci(int n) {
    if (n <= 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}
"#;

        let rust_expected = r#"
fn fibonacci(n: i32) -> i32 {
    if n <= 1 { return n; }
    fibonacci(n - 1) + fibonacci(n - 2)
}
"#;

        // Test validates:
        // 1. Recursion works same way
        // 2. Base case with early return
        // 3. Recursive calls in expression
        assert!(c_code.contains("fibonacci(n - 1) + fibonacci(n - 2)"));
        assert!(rust_expected.contains("fibonacci(n - 1) + fibonacci(n - 2)"));
    }

    /// Test 13: Function with multiple returns
    /// Different return paths
    #[test]
    fn test_multiple_return_paths() {
        let c_code = r#"
int classify(int x) {
    if (x < 0) return -1;
    if (x > 0) return 1;
    return 0;
}
"#;

        let rust_expected = r#"
fn classify(x: i32) -> i32 {
    if x < 0 { return -1; }
    if x > 0 { return 1; }
    0
}
"#;

        // Test validates:
        // 1. Multiple return statements
        // 2. All explicit except final
        // 3. Same control flow logic
        assert!(c_code.contains("if (x < 0) return -1"));
        assert!(rust_expected.contains("if x < 0 { return -1; }"));
    }

    /// Test 14: Void function with early return
    /// Return without value
    #[test]
    fn test_void_early_return() {
        let c_code = r#"
void process(int x) {
    if (x < 0) return;
    printf("%d\n", x);
}
"#;

        let rust_expected = r#"
fn process(x: i32) {
    if x < 0 { return; }
    println!("{}", x);
}
"#;

        // Test validates:
        // 1. Void function early return
        // 2. return; (no value)
        // 3. Guard clause pattern
        assert!(c_code.contains("if (x < 0) return"));
        assert!(rust_expected.contains("if x < 0 { return; }"));
    }

    /// Test 15: Function with array parameter
    /// Pointer decay in C
    #[test]
    fn test_array_parameter() {
        let c_code = r#"
int sum_array(int arr[], int len) {
    int sum = 0;
    for (int i = 0; i < len; i++) {
        sum += arr[i];
    }
    return sum;
}
"#;

        let rust_expected = r#"
fn sum_array(arr: &[i32]) -> i32 {
    let mut sum = 0;
    for &item in arr {
        sum += item;
    }
    sum
}
"#;

        // Test validates:
        // 1. Array parameter → slice
        // 2. No separate length needed
        // 3. Iterator pattern more idiomatic
        assert!(c_code.contains("int arr[], int len"));
        assert!(rust_expected.contains("arr: &[i32]"));
    }

    /// Test 16: Static function (file scope)
    /// Internal linkage in C
    #[test]
    fn test_static_function() {
        let c_code = r#"
static int helper(int x) {
    return x * 2;
}
"#;

        let rust_expected = r#"
fn helper(x: i32) -> i32 {
    x * 2
}
"#;

        // Test validates:
        // 1. static → module-private by default
        // 2. No pub keyword needed
        // 3. Same implementation
        assert!(c_code.contains("static int helper"));
        assert!(rust_expected.contains("fn helper"));
    }

    /// Test 17: Function transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_function_transformation_summary() {
        let c_code = r#"
// Rule 1: Simple return
int add(int a, int b) { return a + b; }

// Rule 2: Void function
void print(int x) { printf("%d", x); }

// Rule 3: No parameters
int get_value(void) { return 42; }

// Rule 4: Early return
int divide(int a, int b) {
    if (b == 0) return -1;
    return a / b;
}

// Rule 5: Boolean (int in C)
int is_even(int x) { return x % 2 == 0; }

// Rule 6: Float/double
float avg(float a, float b) { return (a + b) / 2; }

// Rule 7: Recursive
int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}

// Rule 8: Array parameter
int sum(int arr[], int n) { ... }
"#;

        let rust_expected = r#"
// Rule 1: Implicit return
fn add(a: i32, b: i32) -> i32 { a + b }

// Rule 2: Omit return type
fn print(x: i32) { println!("{}", x); }

// Rule 3: Empty () for no params
fn get_value() -> i32 { 42 }

// Rule 4: Explicit early, implicit final
fn divide(a: i32, b: i32) -> i32 {
    if b == 0 { return -1; }
    a / b
}

// Rule 5: Use bool type
fn is_even(x: i32) -> bool { x % 2 == 0 }

// Rule 6: f32/f64 types
fn avg(a: f32, b: f32) -> f32 { (a + b) / 2.0 }

// Rule 7: Same recursion
fn fib(n: i32) -> i32 {
    if n <= 1 { return n; }
    fib(n-1) + fib(n-2)
}

// Rule 8: Slice instead of pointer+length
fn sum(arr: &[i32]) -> i32 { ... }
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("int add(int a, int b)"));
        assert!(rust_expected.contains("fn add(a: i32, b: i32) -> i32"));
        assert!(c_code.contains("void print(int x)"));
        assert!(rust_expected.contains("fn print(x: i32)"));
        assert!(c_code.contains("int get_value(void)"));
        assert!(rust_expected.contains("fn get_value() -> i32"));
        assert!(rust_expected.contains("arr: &[i32]"));
    }
}