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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
//! # switch to match Transformation Documentation (K&R §3.4, ISO C99 §6.8.4.2)
//!
//! This file provides comprehensive documentation for the transformation
//! from C's switch statement to Rust's match expression.
//!
//! ## Why This Is Important
//!
//! This transformation improves code safety and expressiveness:
//! - Eliminates fall-through bugs (automatic in C, must be explicit in Rust)
//! - Exhaustiveness checking (compiler ensures all cases handled)
//! - Expression-based (returns values, more functional)
//! - Pattern matching power (more than just equality checks)
//! - No break statements needed (cleaner syntax)
//!
//! ## C switch Statement (K&R §3.4, ISO C99 §6.8.4.2)
//!
//! C's switch statement:
//! - Multi-way branch based on integer value
//! - Requires `break` to prevent fall-through
//! - `default` case is optional (can lead to unhandled cases)
//! - Fall-through is implicit (common source of bugs)
//! - Statement-based (cannot return values directly)
//!
//! ```c
//! switch (x) {
//!     case 1:
//!         y = 10;
//!         break;  // MUST include to prevent fall-through
//!     case 2:
//!         y = 20;
//!         break;
//!     default:
//!         y = 0;
//! }
//! // Forgot break: case 1 falls through to case 2 (bug)
//! ```
//!
//! ## Rust match Expression (Rust Book Ch. 6.2)
//!
//! Rust's match:
//! - Pattern matching expression
//! - No fall-through (each arm is isolated)
//! - Exhaustiveness checked by compiler
//! - Expression-based (returns value)
//! - Powerful patterns (ranges, guards, destructuring)
//!
//! ```rust
//! let y = match x {
//!     1 => 10,        // No break needed
//!     2 => 20,        // No fall-through
//!     _ => 0,         // Catch-all (required if not exhaustive)
//! };
//! ```
//!
//! ## Critical Differences
//!
//! ### 1. Fall-Through
//! - **C**: Implicit fall-through (dangerous)
//!   ```c
//!   switch (x) {
//!       case 1:
//!           printf("one\n");  // Falls through if no break!
//!       case 2:
//!           printf("one or two\n");
//!           break;
//!   }
//!   ```
//! - **Rust**: No fall-through (isolated arms)
//!   ```rust
//!   match x {
//!       1 => println!("one"),        // No fall-through
//!       2 => println!("two"),         // Separate arm
//!       _ => {},
//!   }
//!   ```
//!
//! ### 2. break Statement
//! - **C**: Required to prevent fall-through
//!   ```c
//!   case 1:
//!       y = 10;
//!       break;  // MUST include
//!   ```
//! - **Rust**: Not needed (no fall-through)
//!   ```rust
//!   1 => { y = 10 },  // No break needed
//!   ```
//!
//! ### 3. Exhaustiveness
//! - **C**: default case optional (can miss cases)
//!   ```c
//!   switch (x) {
//!       case 1: break;
//!       // No default - other values unhandled
//!   }
//!   ```
//! - **Rust**: Compiler enforces exhaustiveness
//!   ```rust
//!   match x {
//!       1 => {},
//!       _ => {},  // Required if not exhaustive
//!   }
//!   ```
//!
//! ### 4. Expression vs Statement
//! - **C**: Statement-based
//!   ```c
//!   int y;
//!   switch (x) {
//!       case 1: y = 10; break;
//!   }
//!   ```
//! - **Rust**: Expression-based
//!   ```rust
//!   let y = match x {
//!       1 => 10,
//!       _ => 0,
//!   };
//!   ```
//!
//! ### 5. Pattern Power
//! - **C**: Only constant integers
//!   ```c
//!   switch (x) {
//!       case 1: break;
//!       case 2: break;
//!   }
//!   ```
//! - **Rust**: Rich patterns (ranges, guards, etc.)
//!   ```rust
//!   match x {
//!       0 => {},
//!       1..=10 => {},      // Range
//!       n if n > 100 => {},  // Guard
//!       _ => {},
//!   }
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Pattern 1: Basic switch → match
//! ```c
//! switch (x) {
//!     case 1: y = 10; break;
//!     case 2: y = 20; break;
//!     default: y = 0;
//! }
//! ```
//! ```rust
//! let y = match x {
//!     1 => 10,
//!     2 => 20,
//!     _ => 0,
//! };
//! ```
//!
//! ### Pattern 2: switch with return → match expression
//! ```c
//! switch (x) {
//!     case 1: return 10;
//!     case 2: return 20;
//!     default: return 0;
//! }
//! ```
//! ```rust
//! match x {
//!     1 => 10,
//!     2 => 20,
//!     _ => 0,
//! }
//! ```
//!
//! ### Pattern 3: Multiple cases → multiple patterns
//! ```c
//! switch (x) {
//!     case 1:
//!     case 2:
//!     case 3:
//!         y = 1;
//!         break;
//!     default:
//!         y = 0;
//! }
//! ```
//! ```rust
//! let y = match x {
//!     1 | 2 | 3 => 1,
//!     _ => 0,
//! };
//! ```
//!
//! ## Unsafe Block Count: 0
//!
//! All transformations from switch to match are **100% safe**:
//! - match is a safe language construct
//! - No unsafe code needed
//! - Exhaustiveness checking prevents bugs
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of switch patterns
//! - Unsafe blocks: 0 (all safe transformations)
//! - K&R: §3.4 (switch statement)
//! - ISO C99: §6.8.4.2 (switch statement)
//!
//! ## References
//!
//! - K&R "The C Programming Language" §3.4 (switch statement)
//! - ISO/IEC 9899:1999 (C99) §6.8.4.2 (The switch statement)
//! - The Rust Programming Language Book Ch. 6.2 (match)

#[cfg(test)]
mod tests {
    /// Test 1: Basic switch → match
    /// Simple case with default
    #[test]
    fn test_switch_to_match_basic() {
        let c_code = r#"
switch (x) {
    case 1:
        y = 10;
        break;
    case 2:
        y = 20;
        break;
    default:
        y = 0;
}
"#;

        let rust_expected = r#"
let y = match x {
    1 => 10,
    2 => 20,
    _ => 0,
};
"#;

        // Test validates:
        // 1. switch → match
        // 2. case → pattern =>
        // 3. default → _
        assert!(c_code.contains("switch (x)"));
        assert!(c_code.contains("case 1:"));
        assert!(c_code.contains("default:"));
        assert!(rust_expected.contains("match x"));
        assert!(rust_expected.contains("_ =>"));
    }

    /// Test 2: switch with return → match expression
    /// Direct return values
    #[test]
    fn test_switch_return_to_match() {
        let c_code = r#"
switch (x) {
    case 1:
        return 10;
    case 2:
        return 20;
    default:
        return 0;
}
"#;

        let rust_expected = r#"
return match x {
    1 => 10,
    2 => 20,
    _ => 0,
};
"#;

        // Test validates:
        // 1. switch with returns → match expression
        // 2. No break needed in Rust
        // 3. Cleaner syntax
        assert!(c_code.contains("return 10"));
        assert!(rust_expected.contains("match x"));
    }

    /// Test 3: Multiple cases (fall-through intent) → pattern alternatives
    /// Intentional fall-through for same action
    #[test]
    fn test_switch_multiple_cases() {
        let c_code = r#"
switch (x) {
    case 1:
    case 2:
    case 3:
        y = 1;
        break;
    default:
        y = 0;
}
"#;

        let rust_expected = r#"
let y = match x {
    1 | 2 | 3 => 1,
    _ => 0,
};
"#;

        // Test validates:
        // 1. Multiple cases → | pattern
        // 2. Intentional fall-through → explicit alternative
        // 3. Clearer intent
        assert!(c_code.contains("case 1:"));
        assert!(c_code.contains("case 2:"));
        assert!(c_code.contains("case 3:"));
        assert!(rust_expected.contains("1 | 2 | 3"));
    }

    /// Test 4: switch without default → match with exhaustive check
    /// Compiler enforces exhaustiveness
    #[test]
    fn test_switch_no_default() {
        let c_code = r#"
switch (x) {
    case 0:
        y = 0;
        break;
    case 1:
        y = 1;
        break;
}
"#;

        let rust_expected = r#"
let y = match x {
    0 => 0,
    1 => 1,
    _ => panic!("Unhandled case"),  // Compiler requires this
};
"#;

        // Test validates:
        // 1. Missing default → _ arm required
        // 2. Exhaustiveness checking
        // 3. Prevents unhandled cases
        assert!(c_code.contains("case 0:"));
        assert!(c_code.contains("case 1:"));
        assert!(!c_code.contains("default:"));
        assert!(rust_expected.contains("_ =>"));
    }

    /// Test 5: Empty cases → empty arms
    /// No action needed
    #[test]
    fn test_switch_empty_cases() {
        let _c_code = r#"
switch (x) {
    case 1:
        break;
    case 2:
        process();
        break;
    default:
        break;
}
"#;

        let rust_expected = r#"
match x {
    1 => {},
    2 => { process(); },
    _ => {},
}
"#;

        // Test validates:
        // 1. Empty case → empty arm {}
        // 2. break alone → {}
        // 3. Explicit no-op
        assert!(rust_expected.contains("1 => {}"));
        assert!(rust_expected.contains("_ => {}"));
    }

    /// Test 6: Character switch → character match
    /// Matching on char
    #[test]
    fn test_switch_char_to_match() {
        let c_code = r#"
switch (c) {
    case 'a':
        result = 1;
        break;
    case 'b':
        result = 2;
        break;
    default:
        result = 0;
}
"#;

        let rust_expected = r#"
let result = match c {
    b'a' => 1,
    b'b' => 2,
    _ => 0,
};
"#;

        // Test validates:
        // 1. Character switch → character match
        // 2. 'a' → b'a' (byte literal)
        // 3. Type consistency
        assert!(c_code.contains("case 'a':"));
        assert!(rust_expected.contains("b'a'"));
    }

    /// Test 7: Nested switch → nested match
    /// Switch inside switch
    #[test]
    fn test_switch_nested() {
        let c_code = r#"
switch (x) {
    case 1:
        switch (y) {
            case 10: result = 100; break;
            default: result = 10; break;
        }
        break;
    default:
        result = 0;
}
"#;

        let rust_expected = r#"
let result = match x {
    1 => match y {
        10 => 100,
        _ => 10,
    },
    _ => 0,
};
"#;

        // Test validates:
        // 1. Nested switch → nested match
        // 2. Expression composition
        // 3. Cleaner nesting
        assert!(c_code.contains("switch (x)"));
        assert!(c_code.contains("switch (y)"));
        assert!(rust_expected.contains("match x"));
        assert!(rust_expected.contains("match y"));
    }

    /// Test 8: switch with blocks → match with blocks
    /// Multiple statements per case
    #[test]
    fn test_switch_with_blocks() {
        let c_code = r#"
switch (x) {
    case 1: {
        int temp = x * 2;
        result = temp + 1;
        break;
    }
    default:
        result = 0;
}
"#;

        let rust_expected = r#"
let result = match x {
    1 => {
        let temp = x * 2;
        temp + 1
    },
    _ => 0,
};
"#;

        // Test validates:
        // 1. Block in case → block in arm
        // 2. Local variables
        // 3. Last expression is value
        assert!(c_code.contains("int temp"));
        assert!(rust_expected.contains("let temp"));
    }

    /// Test 9: Range of values → range pattern
    /// Consecutive values
    #[test]
    fn test_switch_range_to_match_range() {
        let c_code = r#"
switch (x) {
    case 0: case 1: case 2: case 3: case 4: case 5:
        category = 0;
        break;
    case 6: case 7: case 8: case 9: case 10:
        category = 1;
        break;
    default:
        category = 2;
}
"#;

        let rust_expected = r#"
let category = match x {
    0..=5 => 0,
    6..=10 => 1,
    _ => 2,
};
"#;

        // Test validates:
        // 1. Consecutive cases → range pattern
        // 2. ..= inclusive range
        // 3. More readable
        assert!(c_code.contains("case 0:"));
        assert!(c_code.contains("case 5:"));
        assert!(rust_expected.contains("0..=5"));
    }

    /// Test 10: Enum switch → enum match
    /// Matching on enum values
    #[test]
    fn test_switch_enum_to_match() {
        let c_code = r#"
enum Color { RED, GREEN, BLUE };
switch (color) {
    case RED:
        value = 1;
        break;
    case GREEN:
        value = 2;
        break;
    case BLUE:
        value = 3;
        break;
}
"#;

        let rust_expected = r#"
enum Color { Red, Green, Blue }
let value = match color {
    Color::Red => 1,
    Color::Green => 2,
    Color::Blue => 3,
};
"#;

        // Test validates:
        // 1. Enum switch → enum match
        // 2. No default needed (exhaustive)
        // 3. Enum::Variant syntax
        assert!(c_code.contains("case RED:"));
        assert!(rust_expected.contains("Color::Red"));
    }

    /// Test 11: switch in function → match expression return
    /// Function returning based on switch
    #[test]
    fn test_switch_function_return() {
        let c_code = r#"
int get_category(int x) {
    switch (x) {
        case 1: return 10;
        case 2: return 20;
        default: return 0;
    }
}
"#;

        let rust_expected = r#"
fn get_category(x: i32) -> i32 {
    match x {
        1 => 10,
        2 => 20,
        _ => 0,
    }
}
"#;

        // Test validates:
        // 1. Function with switch → match expression
        // 2. Implicit return
        // 3. No explicit return keyword
        assert!(c_code.contains("int get_category"));
        assert!(rust_expected.contains("fn get_category"));
    }

    /// Test 12: switch with continue → match in loop
    /// Loop control in switch
    #[test]
    fn test_switch_with_continue() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    switch (arr[i]) {
        case 0:
            continue;
        case 1:
            process(i);
            break;
        default:
            break;
    }
}
"#;

        let rust_expected = r#"
for i in 0..n {
    match arr[i] {
        0 => continue,
        1 => { process(i); },
        _ => {},
    }
}
"#;

        // Test validates:
        // 1. continue in switch → continue in match
        // 2. Loop context preserved
        // 3. Control flow clear
        assert!(c_code.contains("continue;"));
        assert!(rust_expected.contains("continue"));
    }

    /// Test 13: Complex expressions in case → patterns with if guards
    /// Pattern guards for complex conditions
    #[test]
    fn test_switch_to_match_with_guards() {
        let c_code = r#"
// C switch only supports constant integers
switch (x) {
    case 1:
        if (y > 10) {
            result = 100;
        } else {
            result = 10;
        }
        break;
    default:
        result = 0;
}
"#;

        let rust_expected = r#"
let result = match x {
    1 if y > 10 => 100,  // Pattern guard
    1 => 10,
    _ => 0,
};
"#;

        // Test validates:
        // 1. Complex condition → pattern guard
        // 2. if guard syntax
        // 3. More concise
        assert!(c_code.contains("if (y > 10)"));
        assert!(rust_expected.contains("if y > 10"));
    }

    /// Test 14: Accidental fall-through → compile error prevented
    /// Rust prevents fall-through bugs
    #[test]
    fn test_switch_fallthrough_bug_prevented() {
        let c_code = r#"
switch (x) {
    case 1:
        printf("one\n");
        // ERROR: forgot break, falls through to case 2
    case 2:
        printf("one or two\n");
        break;
}
"#;

        let rust_expected = r#"
match x {
    1 => {
        println!("one");
        // No fall-through in Rust - each arm is isolated
    },
    2 => {
        println!("two");
    },
    _ => {},
}
"#;

        // Test validates:
        // 1. Fall-through bug impossible in Rust
        // 2. Each arm isolated
        // 3. Safer by default
        assert!(c_code.contains("// ERROR"));
        assert!(rust_expected.contains("No fall-through"));
    }

    /// Test 15: switch on boolean (antipattern) → if-else
    /// Boolean switch better as if-else
    #[test]
    fn test_switch_bool_to_if_else() {
        let c_code = r#"
switch (flag) {
    case 0:
        result = false_value;
        break;
    case 1:
        result = true_value;
        break;
}
"#;

        let rust_expected = r#"
let result = if flag != 0 {
    true_value
} else {
    false_value
};
"#;

        // Test validates:
        // 1. Boolean switch → if-else
        // 2. More idiomatic
        // 3. Clearer intent
        assert!(c_code.contains("case 0:"));
        assert!(c_code.contains("case 1:"));
        assert!(rust_expected.contains("if flag"));
    }

    /// Test 16: switch with macros → match with constants
    /// Constant values in cases
    #[test]
    fn test_switch_macros_to_match() {
        let c_code = r#"
#define ERROR_OK 0
#define ERROR_FAIL 1
switch (error_code) {
    case ERROR_OK:
        return true;
    case ERROR_FAIL:
        return false;
    default:
        return false;
}
"#;

        let rust_expected = r#"
const ERROR_OK: i32 = 0;
const ERROR_FAIL: i32 = 1;
match error_code {
    ERROR_OK => true,
    ERROR_FAIL => false,
    _ => false,
}
"#;

        // Test validates:
        // 1. #define → const
        // 2. Macro in case → constant in pattern
        // 3. Type safety
        assert!(c_code.contains("ERROR_OK"));
        assert!(rust_expected.contains("ERROR_OK"));
    }

    /// Test 17: Transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_switch_transformation_summary() {
        let c_code = r#"
// Rule 1: Basic switch → match
switch (x) { case 1: break; default: break; }

// Rule 2: switch with return → match expression
switch (x) { case 1: return 10; }

// Rule 3: Multiple cases → pattern alternatives
switch (x) { case 1: case 2: break; }

// Rule 4: No default → _ required
switch (x) { case 1: break; }

// Rule 5: Empty case → empty arm
switch (x) { case 1: break; }

// Rule 6: Character → character match
switch (c) { case 'a': break; }

// Rule 7: Nested switch → nested match
switch (x) { case 1: switch (y) { } break; }

// Rule 8: Range → range pattern
switch (x) { case 0: case 1: case 2: break; }

// Rule 9: Enum → enum match
switch (color) { case RED: break; }

// Rule 10: Fall-through prevented
switch (x) { case 1: /* no break */ case 2: break; }
"#;

        let rust_expected = r#"
// Rule 1: Expression-based
match x { 1 => {}, _ => {} }

// Rule 2: Direct return
match x { 1 => 10, _ => 0 }

// Rule 3: | for alternatives
match x { 1 | 2 => {}, _ => {} }

// Rule 4: Exhaustiveness required
match x { 1 => {}, _ => {} }

// Rule 5: {} for no-op
match x { 1 => {}, _ => {} }

// Rule 6: byte literal
match c { b'a' => {}, _ => {} }

// Rule 7: Composition
match x { 1 => match y { _ => {} }, _ => {} }

// Rule 8: Inclusive range
match x { 0..=2 => {}, _ => {} }

// Rule 9: Variant syntax
match color { Color::Red => {}, _ => {} }

// Rule 10: No fall-through (safe)
match x { 1 => {}, 2 => {}, _ => {} }
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("switch (x)"));
        assert!(c_code.contains("case 1:"));
        assert!(c_code.contains("default:"));
        assert!(rust_expected.contains("match x"));
        assert!(rust_expected.contains("_ =>"));
        assert!(rust_expected.contains("0..=2"));
        assert!(rust_expected.contains("1 | 2"));
    }
}