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
//! # Ternary Operator Documentation (C99 §6.5.15, K&R §2.11)
//!
//! This file provides comprehensive documentation for ternary operator transformations
//! from C to Rust, showing how C's ternary operator (`? :`) transforms to Rust's if expressions.
//!
//! ## C Ternary Operator Overview (C99 §6.5.15, K&R §2.11)
//!
//! C ternary operator:
//! - Syntax: `condition ? expr_true : expr_false`
//! - Evaluates condition first
//! - Returns expr_true if condition is non-zero
//! - Returns expr_false if condition is zero
//! - Short-circuits: only one branch is evaluated
//! - Returns value (not statement)
//!
//! ## Rust If Expression Overview
//!
//! Rust if expression:
//! - Syntax: `if condition { expr_true } else { expr_false }`
//! - Evaluates condition first (must be bool)
//! - Returns expr_true if condition is true
//! - Returns expr_false if condition is false
//! - Short-circuits: only one branch is evaluated
//! - Returns value (expression, not statement)
//! - Type-safe: both branches must return same type
//!
//! ## Critical Differences
//!
//! ### 1. Type Safety
//! - **C**: Performs implicit type conversions between branches
//!   ```c
//!   int x = (cond) ? 5 : 3.14;  // Valid: 3.14 truncated to 3
//!   ```
//! - **Rust**: REQUIRES both branches to have same type (compile error)
//!   ```rust
//!   let x = if cond { 5 } else { 3.14 };  // COMPILE ERROR! i32 vs f64
//!   let x = if cond { 5.0 } else { 3.14 };  // OK: both f64
//!   ```
//!
//! ### 2. Condition Type
//! - **C**: Allows any integer (0 is false, non-zero is true)
//!   ```c
//!   int x = 5;
//!   int result = (x) ? 10 : 20;  // Valid: 5 is true
//!   ```
//! - **Rust**: REQUIRES bool type (compile error otherwise)
//!   ```rust
//!   let x = 5;
//!   let result = if x { 10 } else { 20 };  // COMPILE ERROR!
//!   let result = if x != 0 { 10 } else { 20 };  // Must be explicit
//!   ```
//!
//! ### 3. Readability
//! - **C**: Nested ternaries become unreadable
//!   ```c
//!   grade = (x >= 90) ? 'A' : (x >= 80) ? 'B' : (x >= 70) ? 'C' : 'F';
//!   ```
//! - **Rust**: If-else-if chains are more readable
//!   ```rust
//!   let grade = if x >= 90 { 'A' }
//!               else if x >= 80 { 'B' }
//!               else if x >= 70 { 'C' }
//!               else { 'F' };
//!   ```
//!
//! ### 4. Option Pattern
//! - **C**: Often used with NULL checks
//!   ```c
//!   value = ptr ? ptr->data : default;
//!   ```
//! - **Rust**: Option methods are more idiomatic
//!   ```rust
//!   let value = opt.map(|p| p.data).unwrap_or(default);
//!   // OR: opt.as_ref().map_or(default, |p| p.data)
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Simple Ternary → If Expression
//! ```c
//! max = (a > b) ? a : b;
//! ```
//! ```rust
//! let max = if a > b { a } else { b };
//! ```
//!
//! ### Rule 2: Ternary in Assignment → If Expression
//! ```c
//! int sign = (x >= 0) ? 1 : -1;
//! ```
//! ```rust
//! let sign = if x >= 0 { 1 } else { -1 };
//! ```
//!
//! ### Rule 3: Nested Ternary → If-Else-If Chain
//! ```c
//! result = (x >= 90) ? 'A' : (x >= 80) ? 'B' : 'C';
//! ```
//! ```rust
//! let result = if x >= 90 { 'A' }
//!              else if x >= 80 { 'B' }
//!              else { 'C' };
//! ```
//!
//! ### Rule 4: Ternary in Return → If Expression
//! ```c
//! return (a > b) ? a : b;
//! ```
//! ```rust
//! if a > b { a } else { b }  // Implicit return
//! ```
//!
//! ### Rule 5: NULL Check → Option Methods
//! ```c
//! value = ptr ? ptr->data : default;
//! ```
//! ```rust
//! let value = opt.map(|p| p.data).unwrap_or(default);
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of ternary operator patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.5.15 (conditional operator)
//! - K&R: §2.11
//!
//! ## References
//!
//! - K&R "The C Programming Language" §2.11 (Conditional Expressions)
//! - ISO/IEC 9899:1999 (C99) §6.5.15 (Conditional operator)
//! - Rust Book: If Expressions

#[cfg(test)]
mod tests {
    /// Test 1: Simple ternary for max/min
    /// Most common pattern
    #[test]
    fn test_simple_ternary_max() {
        let c_code = r#"
int max = (a > b) ? a : b;
"#;

        let rust_expected = r#"
let max = if a > b { a } else { b };
"#;

        // Test validates:
        // 1. Ternary → if expression
        // 2. Same short-circuit behavior
        // 3. Type-safe in Rust (both branches same type)
        assert!(c_code.contains("(a > b) ? a : b"));
        assert!(rust_expected.contains("if a > b { a } else { b }"));
    }

    /// Test 2: Simple ternary for min
    /// Opposite comparison
    #[test]
    fn test_simple_ternary_min() {
        let c_code = r#"
int min = (a < b) ? a : b;
"#;

        let rust_expected = r#"
let min = if a < b { a } else { b };
"#;

        // Test validates:
        // 1. Min pattern same as max
        // 2. Common in algorithms
        // 3. If expression reads naturally
        assert!(c_code.contains("(a < b) ? a : b"));
        assert!(rust_expected.contains("if a < b { a } else { b }"));
    }

    /// Test 3: Ternary for sign determination
    /// Three possible values with two branches
    #[test]
    fn test_ternary_sign() {
        let c_code = r#"
int sign = (x >= 0) ? 1 : -1;
"#;

        let rust_expected = r#"
let sign = if x >= 0 { 1 } else { -1 };
"#;

        // Test validates:
        // 1. Ternary with different values
        // 2. Sign function pattern
        // 3. Clear if expression
        assert!(c_code.contains("(x >= 0) ? 1 : -1"));
        assert!(rust_expected.contains("if x >= 0 { 1 } else { -1 }"));
    }

    /// Test 4: Ternary in return statement
    /// Expression as return value
    #[test]
    fn test_ternary_in_return() {
        let c_code = r#"
int get_abs(int x) {
    return (x >= 0) ? x : -x;
}
"#;

        let rust_expected = r#"
fn get_abs(x: i32) -> i32 {
    if x >= 0 { x } else { -x }
}
"#;

        // Test validates:
        // 1. Ternary in return
        // 2. If expression as implicit return
        // 3. Absolute value pattern
        assert!(c_code.contains("return (x >= 0) ? x : -x"));
        assert!(rust_expected.contains("if x >= 0 { x } else { -x }"));
    }

    /// Test 5: Nested ternary (nested in false branch)
    /// Three-way decision
    #[test]
    fn test_nested_ternary_grades() {
        let c_code = r#"
char grade = (x >= 90) ? 'A' : (x >= 80) ? 'B' : 'C';
"#;

        let rust_expected = r#"
let grade = if x >= 90 { 'A' }
            else if x >= 80 { 'B' }
            else { 'C' };
"#;

        // Test validates:
        // 1. Nested ternary transformation
        // 2. If-else-if chain more readable
        // 3. Grade calculation pattern
        assert!(c_code.contains("(x >= 90) ? 'A' : (x >= 80) ? 'B' : 'C'"));
        assert!(rust_expected.contains("if x >= 90 { 'A' }"));
        assert!(rust_expected.contains("else if x >= 80 { 'B' }"));
    }

    /// Test 6: Deeply nested ternary (four branches)
    /// Complex grading system
    #[test]
    fn test_deeply_nested_ternary() {
        let c_code = r#"
char grade = (x >= 90) ? 'A' : (x >= 80) ? 'B' : (x >= 70) ? 'C' : 'F';
"#;

        let rust_expected = r#"
let grade = if x >= 90 { 'A' }
            else if x >= 80 { 'B' }
            else if x >= 70 { 'C' }
            else { 'F' };
"#;

        // Test validates:
        // 1. Deeply nested ternary
        // 2. If-else-if chain dramatically more readable
        // 3. Common grading pattern
        assert!(c_code.contains("(x >= 90) ? 'A'"));
        assert!(rust_expected.contains("if x >= 90 { 'A' }"));
        assert!(rust_expected.contains("else if x >= 70 { 'C' }"));
        assert!(rust_expected.contains("else { 'F' }"));
    }

    /// Test 7: Ternary with function calls
    /// Lazy evaluation of expensive operations
    #[test]
    fn test_ternary_with_function_calls() {
        let c_code = r#"
int result = is_cached() ? get_cached_value() : compute_expensive();
"#;

        let rust_expected = r#"
let result = if is_cached() { get_cached_value() } else { compute_expensive() };
"#;

        // Test validates:
        // 1. Ternary with function calls
        // 2. Short-circuit: only one function called
        // 3. Performance optimization pattern
        assert!(c_code.contains("is_cached() ? get_cached_value() : compute_expensive()"));
        assert!(rust_expected
            .contains("if is_cached() { get_cached_value() } else { compute_expensive() }"));
    }

    /// Test 8: Ternary for NULL check (C pattern)
    /// Pointer safety pattern
    #[test]
    fn test_ternary_null_check() {
        let c_code = r#"
int value = (ptr != NULL) ? ptr->data : default_value;
"#;

        let rust_expected = r#"
let value = if let Some(ptr) = opt {
    ptr.data
} else {
    default_value
};
"#;

        // Test validates:
        // 1. NULL check pattern
        // 2. Option pattern in Rust
        // 3. Safe pointer access
        assert!(c_code.contains("(ptr != NULL) ? ptr->data : default_value"));
        assert!(rust_expected.contains("if let Some(ptr) = opt"));
    }

    /// Test 9: Ternary for zero check
    /// Prevent division by zero
    #[test]
    fn test_ternary_zero_check() {
        let c_code = r#"
int result = (b != 0) ? a / b : 0;
"#;

        let rust_expected = r#"
let result = if b != 0 { a / b } else { 0 };
"#;

        // Test validates:
        // 1. Zero check before division
        // 2. Safety pattern
        // 3. Common guard pattern
        assert!(c_code.contains("(b != 0) ? a / b : 0"));
        assert!(rust_expected.contains("if b != 0 { a / b } else { 0 }"));
    }

    /// Test 10: Ternary in array initialization
    /// Element-wise conditional
    #[test]
    fn test_ternary_in_array() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    arr[i] = (i % 2 == 0) ? 0 : 1;
}
"#;

        let rust_expected = r#"
for i in 0..n {
    arr[i] = if i % 2 == 0 { 0 } else { 1 };
}
"#;

        // Test validates:
        // 1. Ternary in loop
        // 2. Pattern filling array
        // 3. Conditional assignment
        assert!(c_code.contains("(i % 2 == 0) ? 0 : 1"));
        assert!(rust_expected.contains("if i % 2 == 0 { 0 } else { 1 }"));
    }

    /// Test 11: Ternary for clamp/bounds check
    /// Ensure value in range
    #[test]
    fn test_ternary_clamp() {
        let c_code = r#"
int clamped = (x < MIN) ? MIN : (x > MAX) ? MAX : x;
"#;

        let rust_expected = r#"
let clamped = if x < MIN { MIN }
              else if x > MAX { MAX }
              else { x };
"#;

        // Test validates:
        // 1. Nested ternary for clamping
        // 2. Three-way check
        // 3. Common validation pattern
        assert!(c_code.contains("(x < MIN) ? MIN : (x > MAX) ? MAX : x"));
        assert!(rust_expected.contains("if x < MIN { MIN }"));
        assert!(rust_expected.contains("else if x > MAX { MAX }"));
    }

    /// Test 12: Ternary with boolean result
    /// Normalizing comparison to bool
    #[test]
    fn test_ternary_boolean_result() {
        let c_code = r#"
int is_valid = (x >= 0 && x < 100) ? 1 : 0;
"#;

        let rust_expected = r#"
let is_valid = x >= 0 && x < 100;
"#;

        // Test validates:
        // 1. Unnecessary ternary for bool
        // 2. Rust simplifies to direct bool
        // 3. Type safety improvement
        assert!(c_code.contains("(x >= 0 && x < 100) ? 1 : 0"));
        assert!(rust_expected.contains("x >= 0 && x < 100"));
    }

    /// Test 13: Ternary in printf/print (side effects)
    /// Conditional message
    #[test]
    fn test_ternary_in_print() {
        let c_code = r#"
printf("%s\n", (x > 0) ? "positive" : "non-positive");
"#;

        let rust_expected = r#"
println!("{}", if x > 0 { "positive" } else { "non-positive" });
"#;

        // Test validates:
        // 1. Ternary as function argument
        // 2. String selection pattern
        // 3. Common logging pattern
        assert!(c_code.contains("(x > 0) ? \"positive\" : \"non-positive\""));
        assert!(rust_expected.contains("if x > 0 { \"positive\" } else { \"non-positive\" }"));
    }

    /// Test 14: Ternary with pointer (truthy check)
    /// C allows pointer as condition
    #[test]
    fn test_ternary_pointer_truthy() {
        let c_code = r#"
int result = (ptr) ? process(ptr) : handle_null();
"#;

        let rust_expected = r#"
let result = if let Some(ptr) = opt {
    process(ptr)
} else {
    handle_null()
};
"#;

        // Test validates:
        // 1. C pointer truthy check (ptr vs NULL)
        // 2. Rust Option pattern matching
        // 3. Type safety with Option
        assert!(c_code.contains("(ptr) ? process(ptr) : handle_null()"));
        assert!(rust_expected.contains("if let Some(ptr) = opt"));
    }

    /// Test 15: Ternary for sign extension pattern
    /// Bit manipulation common pattern
    #[test]
    fn test_ternary_sign_extension() {
        let c_code = r#"
int extended = (x & 0x80) ? (x | 0xFF00) : x;
"#;

        let rust_expected = r#"
let extended = if (x & 0x80) != 0 { x | 0xFF00 } else { x };
"#;

        // Test validates:
        // 1. Bitwise operations in condition
        // 2. Must be explicit != 0 in Rust
        // 3. Sign extension pattern
        assert!(c_code.contains("(x & 0x80) ? (x | 0xFF00) : x"));
        assert!(rust_expected.contains("if (x & 0x80) != 0"));
    }

    /// Test 16: Ternary for default value pattern
    /// Provide fallback
    #[test]
    fn test_ternary_default_value() {
        let c_code = r#"
int value = (count > 0) ? total / count : 0;
"#;

        let rust_expected = r#"
let value = if count > 0 { total / count } else { 0 };
"#;

        // Test validates:
        // 1. Default value pattern
        // 2. Average calculation with guard
        // 3. Common safety pattern
        assert!(c_code.contains("(count > 0) ? total / count : 0"));
        assert!(rust_expected.contains("if count > 0 { total / count } else { 0 }"));
    }

    /// Test 17: Ternary operator transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_ternary_operator_transformation_summary() {
        let c_code = r#"
// Rule 1: Simple ternary (max/min)
max = (a > b) ? a : b;

// Rule 2: Sign determination
sign = (x >= 0) ? 1 : -1;

// Rule 3: Nested ternary (grades)
grade = (x >= 90) ? 'A' : (x >= 80) ? 'B' : 'C';

// Rule 4: In return statement
return (x >= 0) ? x : -x;

// Rule 5: NULL check
value = (ptr != NULL) ? ptr->data : default;

// Rule 6: Zero check
result = (b != 0) ? a / b : 0;

// Rule 7: Clamp/bounds
clamped = (x < MIN) ? MIN : (x > MAX) ? MAX : x;

// Rule 8: Boolean result (unnecessary ternary)
is_valid = (x >= 0 && x < 100) ? 1 : 0;

// Rule 9: With function calls (short-circuit)
result = is_cached() ? get_cached() : compute();
"#;

        let rust_expected = r#"
// Rule 1: Simple if expression
let max = if a > b { a } else { b };

// Rule 2: Sign determination
let sign = if x >= 0 { 1 } else { -1 };

// Rule 3: If-else-if chain (more readable)
let grade = if x >= 90 { 'A' }
            else if x >= 80 { 'B' }
            else { 'C' };

// Rule 4: If expression as implicit return
if x >= 0 { x } else { -x }

// Rule 5: Option pattern matching
let value = if let Some(ptr) = opt { ptr.data } else { default };

// Rule 6: Zero check guard
let result = if b != 0 { a / b } else { 0 };

// Rule 7: If-else-if clamp
let clamped = if x < MIN { MIN }
              else if x > MAX { MAX }
              else { x };

// Rule 8: Direct boolean (no if needed)
let is_valid = x >= 0 && x < 100;

// Rule 9: Short-circuit preserved
let result = if is_cached() { get_cached() } else { compute() };
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("(a > b) ? a : b"));
        assert!(rust_expected.contains("if a > b { a } else { b }"));
        assert!(c_code.contains("(x >= 90) ? 'A'"));
        assert!(rust_expected.contains("if x >= 90 { 'A' }"));
        assert!(c_code.contains("(ptr != NULL)"));
        assert!(rust_expected.contains("if let Some(ptr) = opt"));
    }
}