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
//! # Struct Definition Documentation (C99 §6.7.2.1, K&R §6.1)
//!
//! This file provides comprehensive documentation for struct definition transformations
//! from C to Rust, covering all struct patterns and memory layout considerations.
//!
//! ## C Struct Overview (C99 §6.7.2.1, K&R §6.1)
//!
//! C struct characteristics:
//! - Syntax: `struct tag { members };`
//! - Aggregate type grouping related data
//! - Members accessed with `.` (struct) or `->` (pointer to struct)
//! - Can be anonymous (no tag)
//! - Can be typedef'd for convenience
//! - Memory layout: sequential with padding for alignment
//! - No methods or functions inside struct
//! - Potential issues: uninitialized fields, padding bytes
//!
//! ## Rust Struct Overview
//!
//! Rust struct characteristics:
//! - Syntax: `struct Name { members }`
//! - No `struct` keyword needed for type usage
//! - Members accessed with `.` (ownership/borrow handled automatically)
//! - No pointer dereferencing needed (automatic)
//! - Can have methods via `impl` blocks
//! - All fields must be initialized or derive Default
//! - Memory layout: compiler-optimized by default (#[repr(C)] for C compatibility)
//! - Type-safe: no uninitialized fields
//!
//! ## Critical Differences
//!
//! ### 1. Type Usage
//! - **C**: Must use `struct` keyword or typedef
//!   ```c
//!   struct Point p;           // Need 'struct' keyword
//!   typedef struct Point Point;
//!   Point p;                  // After typedef
//!   ```
//! - **Rust**: Struct name is the type
//!   ```rust
//!   let p: Point;  // No 'struct' keyword needed
//!   ```
//!
//! ### 2. Initialization
//! - **C**: Uninitialized fields contain garbage
//!   ```c
//!   struct Point p;  // x and y are uninitialized!
//!   ```
//! - **Rust**: All fields must be initialized
//!   ```rust
//!   let p = Point { x: 0, y: 0 };  // Must initialize all fields
//!   ```
//!
//! ### 3. Member Access
//! - **C**: Different operators for value vs pointer
//!   ```c
//!   p.x        // For struct value
//!   ptr->x     // For pointer to struct
//!   ```
//! - **Rust**: Same operator, automatic dereferencing
//!   ```rust
//!   p.x        // For owned struct
//!   p.x        // For &Point or &mut Point (auto-deref)
//!   ```
//!
//! ### 4. Memory Layout
//! - **C**: Guaranteed sequential with padding
//! - **Rust**: Compiler-optimized by default, use `#[repr(C)]` for C layout
//!   ```rust
//!   #[repr(C)]  // Forces C-compatible layout
//!   struct Point { x: i32, y: i32 }
//!   ```
//!
//! ### 5. Methods
//! - **C**: No methods, use separate functions
//!   ```c
//!   void point_print(struct Point* p) { ... }
//!   ```
//! - **Rust**: Methods in impl blocks
//!   ```rust
//!   impl Point {
//!       fn print(&self) { ... }
//!   }
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Basic struct definition
//! ```c
//! struct Point {
//!     int x;
//!     int y;
//! };
//! ```
//! ```rust
//! struct Point {
//!     x: i32,
//!     y: i32,
//! }
//! ```
//!
//! ### Rule 2: struct with typedef → remove typedef
//! ```c
//! typedef struct {
//!     int x;
//!     int y;
//! } Point;
//! ```
//! ```rust
//! struct Point {
//!     x: i32,
//!     y: i32,
//! }
//! ```
//!
//! ### Rule 3: struct initialization → struct literal
//! ```c
//! struct Point p = { 10, 20 };
//! ```
//! ```rust
//! let p = Point { x: 10, y: 20 };
//! ```
//!
//! ### Rule 4: Pointer member access → same syntax
//! ```c
//! ptr->x
//! ```
//! ```rust
//! ptr.x  // Automatic dereferencing
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of struct definition patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.7.2.1 (Structure and union specifiers)
//! - K&R: §6.1 (Basics of structures)
//!
//! ## References
//!
//! - K&R "The C Programming Language" §6.1 (Basics of structures)
//! - ISO/IEC 9899:1999 (C99) §6.7.2.1 (Structure and union specifiers)

#[cfg(test)]
mod tests {
    /// Test 1: Simple struct definition
    /// Most basic pattern
    #[test]
    fn test_struct_definition_simple() {
        let c_code = r#"
struct Point {
    int x;
    int y;
};
"#;

        let rust_expected = r#"
struct Point {
    x: i32,
    y: i32,
}
"#;

        // Test validates:
        // 1. struct keyword preserved
        // 2. Field type syntax: int x → x: i32
        // 3. Comma instead of semicolon
        assert!(c_code.contains("struct Point"));
        assert!(rust_expected.contains("struct Point"));
        assert!(c_code.contains("int x;"));
        assert!(rust_expected.contains("x: i32,"));
    }

    /// Test 2: struct with multiple field types
    /// Various field types
    #[test]
    fn test_struct_definition_multiple_types() {
        let c_code = r#"
struct Person {
    char name[50];
    int age;
    float height;
    double weight;
};
"#;

        let rust_expected = r#"
struct Person {
    name: [u8; 50],
    age: i32,
    height: f32,
    weight: f64,
}
"#;

        // Test validates:
        // 1. Multiple field types
        // 2. Array field: char name[50] → name: [u8; 50]
        // 3. All primitive types mapped
        assert!(c_code.contains("struct Person"));
        assert!(rust_expected.contains("struct Person"));
        assert!(c_code.contains("char name[50]"));
        assert!(rust_expected.contains("name: [u8; 50]"));
    }

    /// Test 3: typedef struct pattern
    /// Anonymous struct with typedef
    #[test]
    fn test_struct_definition_typedef() {
        let c_code = r#"
typedef struct {
    int x;
    int y;
} Point;
"#;

        let rust_expected = r#"
struct Point {
    x: i32,
    y: i32,
}
"#;

        // Test validates:
        // 1. typedef removed (not needed in Rust)
        // 2. Anonymous struct → named struct
        // 3. Same field structure
        assert!(c_code.contains("typedef struct"));
        assert!(rust_expected.contains("struct Point"));
    }

    /// Test 4: struct with tag and typedef
    /// Both tag and typedef name
    #[test]
    fn test_struct_definition_typedef_with_tag() {
        let c_code = r#"
typedef struct Point {
    int x;
    int y;
} Point;
"#;

        let rust_expected = r#"
struct Point {
    x: i32,
    y: i32,
}
"#;

        // Test validates:
        // 1. typedef removed
        // 2. Tag name used
        // 3. Simplified in Rust
        assert!(c_code.contains("typedef struct Point"));
        assert!(rust_expected.contains("struct Point"));
    }

    /// Test 5: struct initialization
    /// Creating struct instance
    #[test]
    fn test_struct_initialization() {
        let c_code = r#"
struct Point {
    int x;
    int y;
};

struct Point p = { 10, 20 };
"#;

        let rust_expected = r#"
struct Point {
    x: i32,
    y: i32,
}

let p = Point { x: 10, y: 20 };
"#;

        // Test validates:
        // 1. Positional init → named field init
        // 2. { 10, 20 } → { x: 10, y: 20 }
        // 3. struct keyword not needed in Rust usage
        assert!(c_code.contains("struct Point p = { 10, 20 }"));
        assert!(rust_expected.contains("let p = Point { x: 10, y: 20 }"));
    }

    /// Test 6: struct member access
    /// Field access
    #[test]
    fn test_struct_member_access() {
        let c_code = r#"
struct Point p;
p.x = 10;
int y = p.y;
"#;

        let rust_expected = r#"
let mut p: Point;
p.x = 10;
let y = p.y;
"#;

        // Test validates:
        // 1. Member access syntax same
        // 2. p.x works in both
        // 3. mut needed for modification
        assert!(c_code.contains("p.x = 10"));
        assert!(rust_expected.contains("p.x = 10"));
    }

    /// Test 7: Pointer to struct with arrow operator
    /// ptr->member pattern
    #[test]
    fn test_struct_pointer_access() {
        let c_code = r#"
struct Point* ptr;
ptr->x = 10;
int y = ptr->y;
"#;

        let rust_expected = r#"
let ptr: &mut Point;
ptr.x = 10;
let y = ptr.y;
"#;

        // Test validates:
        // 1. ptr->x → ptr.x (automatic deref)
        // 2. No arrow operator in Rust
        // 3. Same semantics
        assert!(c_code.contains("ptr->x"));
        assert!(rust_expected.contains("ptr.x"));
    }

    /// Test 8: struct with pointer fields
    /// Pointer members
    #[test]
    fn test_struct_with_pointer_fields() {
        let c_code = r#"
struct Node {
    int data;
    struct Node* next;
};
"#;

        let rust_expected = r#"
struct Node {
    data: i32,
    next: Option<Box<Node>>,
}
"#;

        // Test validates:
        // 1. Self-referential struct
        // 2. struct Node* → Option<Box<Node>>
        // 3. NULL safety via Option
        assert!(c_code.contains("struct Node* next"));
        assert!(rust_expected.contains("next: Option<Box<Node>>"));
    }

    /// Test 9: Empty struct
    /// Zero-sized struct
    #[test]
    fn test_struct_empty() {
        let c_code = r#"
struct Empty {
};
"#;

        let rust_expected = r#"
struct Empty {}
"#;

        // Test validates:
        // 1. Empty struct allowed
        // 2. Simpler syntax in Rust
        // 3. Zero-sized type
        assert!(c_code.contains("struct Empty"));
        assert!(rust_expected.contains("struct Empty"));
    }

    /// Test 10: struct with arrays
    /// Array fields
    #[test]
    fn test_struct_with_arrays() {
        let c_code = r#"
struct Matrix {
    int data[10][10];
    int rows;
    int cols;
};
"#;

        let rust_expected = r#"
struct Matrix {
    data: [[i32; 10]; 10],
    rows: i32,
    cols: i32,
}
"#;

        // Test validates:
        // 1. Multidimensional arrays
        // 2. int data[10][10] → data: [[i32; 10]; 10]
        // 3. Array syntax differences
        assert!(c_code.contains("int data[10][10]"));
        assert!(rust_expected.contains("data: [[i32; 10]; 10]"));
    }

    /// Test 11: struct passed to function
    /// Function parameter
    #[test]
    fn test_struct_function_parameter() {
        let c_code = r#"
void process(struct Point p) {
    printf("%d, %d\n", p.x, p.y);
}
"#;

        let rust_expected = r#"
fn process(p: Point) {
    println!("{}, {}", p.x, p.y);
}
"#;

        // Test validates:
        // 1. struct keyword not needed in param
        // 2. Same member access
        // 3. Pass by value
        assert!(c_code.contains("struct Point p"));
        assert!(rust_expected.contains("p: Point"));
    }

    /// Test 12: struct passed by pointer
    /// Pointer parameter for efficiency
    #[test]
    fn test_struct_pointer_parameter() {
        let c_code = r#"
void process(struct Point* p) {
    printf("%d, %d\n", p->x, p->y);
}
"#;

        let rust_expected = r#"
fn process(p: &Point) {
    println!("{}, {}", p.x, p.y);
}
"#;

        // Test validates:
        // 1. struct Point* → &Point
        // 2. p->x → p.x (auto-deref)
        // 3. Borrow instead of pointer
        assert!(c_code.contains("struct Point* p"));
        assert!(rust_expected.contains("p: &Point"));
        assert!(c_code.contains("p->x"));
        assert!(rust_expected.contains("p.x"));
    }

    /// Test 13: struct with designated initializers (C99)
    /// Named field initialization
    #[test]
    fn test_struct_designated_initializers() {
        let c_code = r#"
struct Point p = { .x = 10, .y = 20 };
"#;

        let rust_expected = r#"
let p = Point { x: 10, y: 20 };
"#;

        // Test validates:
        // 1. C99 designated initializers
        // 2. .x = 10 → x: 10
        // 3. Same concept in Rust (but required)
        assert!(c_code.contains(".x = 10"));
        assert!(rust_expected.contains("x: 10"));
    }

    /// Test 14: struct partial initialization
    /// Default values for missing fields
    #[test]
    fn test_struct_partial_initialization() {
        let c_code = r#"
struct Point p = { .x = 10 };  // y initialized to 0
"#;

        let rust_expected = r#"
let p = Point { x: 10, ..Default::default() };  // y from Default
"#;

        // Test validates:
        // 1. Partial init in C (remaining = 0)
        // 2. ..Default::default() in Rust
        // 3. Explicit default values
        assert!(c_code.contains(".x = 10"));
        assert!(rust_expected.contains("..Default::default()"));
    }

    /// Test 15: struct with function pointer field
    /// Callback pattern
    #[test]
    fn test_struct_with_function_pointer() {
        let c_code = r#"
struct Handler {
    void (*callback)(int);
    int data;
};
"#;

        let rust_expected = r#"
struct Handler {
    callback: fn(i32),
    data: i32,
}
"#;

        // Test validates:
        // 1. Function pointer syntax
        // 2. void (*callback)(int) → callback: fn(i32)
        // 3. Simpler syntax in Rust
        assert!(c_code.contains("void (*callback)(int)"));
        assert!(rust_expected.contains("callback: fn(i32)"));
    }

    /// Test 16: struct with nested struct
    /// Composition
    #[test]
    fn test_struct_with_nested_struct() {
        let c_code = r#"
struct Rectangle {
    struct Point top_left;
    struct Point bottom_right;
};
"#;

        let rust_expected = r#"
struct Rectangle {
    top_left: Point,
    bottom_right: Point,
}
"#;

        // Test validates:
        // 1. Nested struct fields
        // 2. struct keyword removed from field type
        // 3. Composition pattern
        assert!(c_code.contains("struct Point top_left"));
        assert!(rust_expected.contains("top_left: Point"));
    }

    /// Test 17: struct transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_struct_transformation_summary() {
        let c_code = r#"
// Rule 1: Basic definition
struct Point { int x; int y; };

// Rule 2: typedef removed
typedef struct { int x; int y; } Point;

// Rule 3: Initialization
struct Point p = { 10, 20 };

// Rule 4: Member access
p.x = 10;

// Rule 5: Pointer access
ptr->x = 10;

// Rule 6: Self-referential
struct Node { int data; struct Node* next; };

// Rule 7: Function parameter
void f(struct Point p);

// Rule 8: Pointer parameter
void f(struct Point* p);

// Rule 9: Designated initializers
struct Point p = { .x = 10, .y = 20 };
"#;

        let rust_expected = r#"
// Rule 1: Same structure, different syntax
struct Point { x: i32, y: i32 }

// Rule 2: No typedef needed
struct Point { x: i32, y: i32 }

// Rule 3: Named fields required
let p = Point { x: 10, y: 20 };

// Rule 4: Same syntax
p.x = 10;

// Rule 5: Auto-deref (no arrow)
ptr.x = 10;

// Rule 6: Option<Box<T>> for nullable
struct Node { data: i32, next: Option<Box<Node>> }

// Rule 7: No 'struct' keyword in type
fn f(p: Point);

// Rule 8: Borrow instead of pointer
fn f(p: &Point);

// Rule 9: Same syntax (always required)
let p = Point { x: 10, y: 20 };
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("struct Point { int x; int y; }"));
        assert!(rust_expected.contains("struct Point { x: i32, y: i32 }"));
        assert!(c_code.contains("typedef struct"));
        assert!(c_code.contains("ptr->x"));
        assert!(rust_expected.contains("ptr.x"));
        assert!(c_code.contains("struct Node* next"));
        assert!(rust_expected.contains("Option<Box<Node>>"));
    }
}