oxur-ast 0.1.0

Rust AST ↔ S-expression conversion for Oxur
Documentation
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
use crate::ast::*;
use crate::error::{ParseError, Position, Result};

/// An error comment to be inserted in output
#[derive(Debug, Clone)]
pub struct ErrorComment {
    pub error_message: String,
    pub rust_code: String,
}

/// Convert syn::File to our Crate
pub fn from_syn_file(file: &syn::File) -> Result<Crate> {
    let mut converter = SynConverter::new();
    converter.convert_file(file)
}

/// Convert syn::File to AST, collecting errors instead of failing
pub fn from_syn_file_partial(file: &syn::File) -> (Crate, Vec<ErrorComment>) {
    let mut converter = SynConverter::new();
    let mut successful_items = Vec::new();
    let mut error_comments = Vec::new();

    for item in &file.items {
        match converter.convert_item(item) {
            Ok(ast_item) => {
                successful_items.push(ast_item);
            }
            Err(e) => {
                // Generate pretty Rust code for the failed item
                let rust_code = prettyprint_item(item);

                error_comments.push(ErrorComment { error_message: e.to_string(), rust_code });
            }
        }
    }

    // Create Crate with successful items
    let inner_span = Span::new(0, 0);
    let spans = ModSpans::new(inner_span);
    let crate_ast = Crate::new(successful_items, spans, converter.next_id());

    (crate_ast, error_comments)
}

/// Pretty-print a syn::Item back to Rust code
fn prettyprint_item(item: &syn::Item) -> String {
    // prettyplease requires a File, so wrap the item
    let file = syn::File { shebang: None, attrs: vec![], items: vec![item.clone()] };

    prettyplease::unparse(&file)
}

/// Generate S-expression comment block for an error
pub fn generate_error_comment(error: &ErrorComment) -> String {
    let mut lines = vec![
        ";; Oxur AST does not support the following Rust code".to_string(),
        format!(";; Error: {}", error.error_message),
        ";;".to_string(),
    ];

    // Comment out each line of Rust code
    for line in error.rust_code.lines() {
        lines.push(format!(";; {}", line));
    }

    lines.join("\n")
}

struct SynConverter {
    next_node_id: usize,
}

impl SynConverter {
    fn new() -> Self {
        Self { next_node_id: 0 }
    }

    fn next_id(&mut self) -> NodeId {
        let id = self.next_node_id;
        self.next_node_id += 1;
        NodeId(id as u32)
    }

    fn convert_file(&mut self, file: &syn::File) -> Result<Crate> {
        let items =
            file.items.iter().map(|item| self.convert_item(item)).collect::<Result<Vec<_>>>()?;

        // Create spans from syn::File
        // Note: syn doesn't give us exact byte offsets easily, so we approximate
        let inner_span = Span::new(0, 0);
        let spans = ModSpans::new(inner_span);

        Ok(Crate::new(items, spans, self.next_id()))
    }

    fn convert_item(&mut self, item: &syn::Item) -> Result<Item> {
        match item {
            syn::Item::Fn(item_fn) => self.convert_item_fn(item_fn),
            syn::Item::Struct(item_struct) => self.convert_item_struct(item_struct),
            syn::Item::Enum(item_enum) => self.convert_item_enum(item_enum),
            syn::Item::Trait(item_trait) => self.convert_item_trait(item_trait),
            syn::Item::Impl(item_impl) => self.convert_item_impl(item_impl),
            syn::Item::Use(item_use) => self.convert_item_use(item_use),
            syn::Item::Static(item_static) => self.convert_item_static(item_static),
            syn::Item::Const(item_const) => self.convert_item_const(item_const),
            syn::Item::Type(item_type) => self.convert_item_type(item_type),
            syn::Item::Mod(item_mod) => self.convert_item_mod(item_mod),
            syn::Item::ExternCrate(_) => Err(ParseError::Expected {
                expected: "supported item type (currently: `fn`, `struct`, `enum`, `trait`, `impl`, `use`, `static`, `const`, `type`, `mod`)".to_string(),
                found: "`extern crate` item".to_string(),
                pos: Position::new(0, 1, 1),
            }),
            syn::Item::ForeignMod(_) => Err(ParseError::Expected {
                expected: "supported item type (currently: `fn`, `struct`, `enum`, `trait`, `impl`, `use`, `static`, `const`, `type`, `mod`)".to_string(),
                found: "`extern` block item".to_string(),
                pos: Position::new(0, 1, 1),
            }),
            syn::Item::Macro(_) => Err(ParseError::Expected {
                expected: "supported item type (currently: `fn`, `struct`, `enum`, `trait`, `impl`, `use`, `static`, `const`, `type`, `mod`)".to_string(),
                found: "macro definition".to_string(),
                pos: Position::new(0, 1, 1),
            }),
            syn::Item::TraitAlias(_) => Err(ParseError::Expected {
                expected: "supported item type (currently: `fn`, `struct`, `enum`, `trait`, `impl`, `use`, `static`, `const`, `type`, `mod`)".to_string(),
                found: "`trait` alias".to_string(),
                pos: Position::new(0, 1, 1),
            }),
            syn::Item::Union(_) => Err(ParseError::Expected {
                expected: "supported item type (currently: `fn`, `struct`, `enum`, `trait`, `impl`, `use`, `static`, `const`, `type`, `mod`)".to_string(),
                found: "`union` item".to_string(),
                pos: Position::new(0, 1, 1),
            }),
            _ => Err(ParseError::Expected {
                expected: "supported item type (currently: `fn`, `struct`, `enum`, `trait`, `impl`, `use`, `static`, `const`, `type`, `mod`)".to_string(),
                found: "unknown item".to_string(),
                pos: Position::new(0, 1, 1),
            }),
        }
    }

    fn convert_item_fn(&mut self, item_fn: &syn::ItemFn) -> Result<Item> {
        let ident = self.convert_ident(&item_fn.sig.ident);
        let vis = self.convert_visibility(&item_fn.vis);

        let fn_sig = self.convert_fn_sig(&item_fn.sig)?;
        let generics = self.convert_generics(&item_fn.sig.generics)?;
        let body = Some(self.convert_block(&item_fn.block)?);

        let fn_item = Fn { defaultness: Defaultness::Final, sig: fn_sig, generics, body };

        Ok(Item {
            attrs: vec![], // Phase 3: simplified
            id: self.next_id(),
            span: Span::DUMMY, // Will improve with proc-macro2::Span
            vis,
            ident,
            kind: ItemKind::Fn(Box::new(fn_item)),
            tokens: None,
        })
    }

    fn convert_ident(&mut self, ident: &syn::Ident) -> Ident {
        Ident::new(ident.to_string(), Span::DUMMY)
    }

    fn convert_visibility(&mut self, vis: &syn::Visibility) -> Visibility {
        match vis {
            syn::Visibility::Public(_) => Visibility::Public,
            syn::Visibility::Inherited => Visibility::Inherited,
            syn::Visibility::Restricted(_) => {
                // Simplified for Phase 3
                Visibility::Inherited
            }
        }
    }

    fn convert_fn_sig(&mut self, sig: &syn::Signature) -> Result<FnSig> {
        let header = self.convert_fn_header(sig);
        let decl = self.convert_fn_decl(sig)?;

        Ok(FnSig { header, decl, span: Span::DUMMY })
    }

    fn convert_fn_header(&mut self, sig: &syn::Signature) -> FnHeader {
        let safety = match sig.unsafety {
            Some(_) => Safety::Unsafe,
            None => Safety::Default,
        };

        let constness = match sig.constness {
            Some(_) => Constness::Const,
            None => Constness::NotConst,
        };

        let coroutine_kind = sig.asyncness.map(|_| CoroutineKind::Async);

        let ext = match &sig.abi {
            Some(abi) => {
                if let Some(name) = &abi.name {
                    Extern::Explicit(name.value())
                } else {
                    Extern::Explicit("C".to_string())
                }
            }
            None => Extern::None,
        };

        FnHeader { safety, coroutine_kind, constness, ext }
    }

    fn convert_fn_decl(&mut self, sig: &syn::Signature) -> Result<FnDecl> {
        let inputs = sig
            .inputs
            .iter()
            .filter_map(|arg| match arg {
                syn::FnArg::Typed(pat_type) => Some(self.convert_fn_arg(pat_type)),
                syn::FnArg::Receiver(_) => None, // Skip self for Phase 3
            })
            .collect::<Result<Vec<_>>>()?;

        let output = self.convert_return_type(&sig.output)?;

        Ok(FnDecl { inputs, output })
    }

    fn convert_fn_arg(&mut self, pat_type: &syn::PatType) -> Result<Param> {
        let pat = self.convert_pat(&pat_type.pat)?;
        let ty = self.convert_type(&pat_type.ty)?;

        Ok(Param {
            attrs: vec![],
            ty,
            pat,
            id: self.next_id(),
            span: Span::DUMMY,
            is_placeholder: false,
        })
    }

    fn convert_pat(&mut self, pat: &syn::Pat) -> Result<Pat> {
        match pat {
            syn::Pat::Ident(pat_ident) => {
                let ident = self.convert_ident(&pat_ident.ident);

                let binding_mode = if pat_ident.by_ref.is_some() {
                    BindingMode::ByRef(if pat_ident.mutability.is_some() {
                        Mutability::Mut
                    } else {
                        Mutability::Not
                    })
                } else {
                    BindingMode::ByValue(if pat_ident.mutability.is_some() {
                        Mutability::Mut
                    } else {
                        Mutability::Not
                    })
                };

                Ok(Pat {
                    id: self.next_id(),
                    kind: PatKind::Ident {
                        binding_mode,
                        ident,
                        sub: None, // Phase 3: simplified
                    },
                    span: Span::DUMMY,
                    tokens: None,
                })
            }
            _ => Err(ParseError::Expected {
                expected: "ident pattern".to_string(),
                found: "complex pattern".to_string(),
                pos: Position::new(0, 1, 1),
            }),
        }
    }

    fn convert_type(&mut self, ty: &syn::Type) -> Result<Ty> {
        match ty {
            syn::Type::Path(type_path) => {
                let path = self.convert_path(&type_path.path)?;
                Ok(Ty {
                    id: self.next_id(),
                    kind: TyKind::Path(None, path),
                    span: Span::DUMMY,
                    tokens: None,
                })
            }
            _ => Err(ParseError::Expected {
                expected: "path type".to_string(),
                found: "complex type".to_string(),
                pos: Position::new(0, 1, 1),
            }),
        }
    }

    fn convert_path(&mut self, path: &syn::Path) -> Result<Path> {
        let segments = path
            .segments
            .iter()
            .map(|seg| {
                let ident = self.convert_ident(&seg.ident);
                PathSegment::from_ident(ident)
            })
            .collect();

        Ok(Path { span: Span::DUMMY, segments, tokens: None })
    }

    fn convert_return_type(&mut self, ret: &syn::ReturnType) -> Result<FnRetTy> {
        match ret {
            syn::ReturnType::Default => Ok(FnRetTy::Default(Span::DUMMY)),
            syn::ReturnType::Type(_, ty) => Ok(FnRetTy::Ty(Box::new(self.convert_type(ty)?))),
        }
    }

    fn convert_generics(&mut self, _generics: &syn::Generics) -> Result<Generics> {
        // Simplified for Phase 3 - just create empty generics
        Ok(Generics {
            params: vec![],
            where_clause: WhereClause {
                has_where_token: false,
                predicates: vec![],
                span: Span::DUMMY,
            },
            span: Span::DUMMY,
        })
    }

    fn convert_block(&mut self, block: &syn::Block) -> Result<Block> {
        let stmts =
            block.stmts.iter().map(|stmt| self.convert_stmt(stmt)).collect::<Result<Vec<_>>>()?;

        Ok(Block::new(stmts, self.next_id(), Span::DUMMY))
    }

    fn convert_stmt(&mut self, stmt: &syn::Stmt) -> Result<Stmt> {
        match stmt {
            syn::Stmt::Expr(expr, semi) => {
                let expr = self.convert_expr(expr)?;
                let kind = if semi.is_some() { StmtKind::Semi(expr) } else { StmtKind::Expr(expr) };

                Ok(Stmt { id: self.next_id(), kind, span: Span::DUMMY })
            }
            syn::Stmt::Local(local) => {
                let local = self.convert_local(local)?;
                Ok(Stmt {
                    id: self.next_id(),
                    kind: StmtKind::Let(Box::new(local)),
                    span: Span::DUMMY,
                })
            }
            syn::Stmt::Item(_) => {
                // Skip items in blocks for Phase 3
                Ok(Stmt { id: self.next_id(), kind: StmtKind::Empty, span: Span::DUMMY })
            }
            syn::Stmt::Macro(mac) => {
                // Convert macro statement
                self.convert_macro_stmt(mac)
            }
        }
    }

    fn convert_local(&mut self, local: &syn::Local) -> Result<Local> {
        let pat = self.convert_pat(&local.pat)?;

        // syn::Local doesn't have direct ty field anymore
        let ty = None; // Phase 3: simplified

        let kind = if let Some(init) = &local.init {
            let expr = self.convert_expr(&init.expr)?;
            let local_init = LocalInit { expr, els: None }; // Phase 3: simplified
            LocalKind::Init(local_init)
        } else {
            LocalKind::Decl
        };

        Ok(Local { pat, kind, span: Span::DUMMY, ty, attrs: vec![], tokens: None })
    }

    fn convert_expr(&mut self, expr: &syn::Expr) -> Result<Expr> {
        let kind = match expr {
            syn::Expr::Macro(expr_macro) => {
                let mac_call = self.convert_macro(&expr_macro.mac)?;
                ExprKind::MacCall(mac_call)
            }
            syn::Expr::Lit(expr_lit) => {
                let lit = self.convert_lit(&expr_lit.lit)?;
                ExprKind::Lit(lit)
            }
            syn::Expr::Path(expr_path) => {
                let path = self.convert_path(&expr_path.path)?;
                ExprKind::Path(None, path)
            }
            _ => {
                return Err(ParseError::Expected {
                    expected: "supported expression".to_string(),
                    found: "complex expression".to_string(),
                    pos: Position::new(0, 1, 1),
                });
            }
        };

        Ok(Expr { id: self.next_id(), kind, span: Span::DUMMY, attrs: vec![], tokens: None })
    }

    fn convert_macro(&mut self, mac: &syn::Macro) -> Result<MacCall> {
        let path = self.convert_path(&mac.path)?;

        // Convert tokens to string representation
        let tokens_str = mac.tokens.to_string();

        let args = MacArgs::Delimited {
            dspan: DelSpan::new(Span::DUMMY, Span::DUMMY),
            delim: self.convert_delimiter(&mac.delimiter),
            tokens: TokenStream::Source(tokens_str),
        };

        Ok(MacCall { path, args, prior_type_ascription: None })
    }

    fn convert_delimiter(&mut self, delim: &syn::MacroDelimiter) -> Delimiter {
        match delim {
            syn::MacroDelimiter::Paren(_) => Delimiter::Paren,
            syn::MacroDelimiter::Brace(_) => Delimiter::Brace,
            syn::MacroDelimiter::Bracket(_) => Delimiter::Bracket,
        }
    }

    fn convert_macro_stmt(&mut self, mac: &syn::StmtMacro) -> Result<Stmt> {
        let mac_call = self.convert_macro(&mac.mac)?;

        let style =
            if mac.semi_token.is_some() { MacStmtStyle::Semicolon } else { MacStmtStyle::Braces };

        let mac_call_stmt = MacCallStmt { mac: mac_call, style, attrs: vec![], tokens: None };

        Ok(Stmt { id: self.next_id(), kind: StmtKind::MacCall(mac_call_stmt), span: Span::DUMMY })
    }

    fn convert_lit(&mut self, lit: &syn::Lit) -> Result<Lit> {
        let kind = match lit {
            syn::Lit::Str(lit_str) => LitKind::Str(lit_str.value()),
            syn::Lit::Int(lit_int) => {
                let value =
                    lit_int.base10_digits().parse::<i128>().map_err(|_| ParseError::Expected {
                        expected: "valid integer".to_string(),
                        found: lit_int.base10_digits().to_string(),
                        pos: Position::new(0, 1, 1),
                    })?;
                LitKind::Int(value)
            }
            _ => {
                return Err(ParseError::Expected {
                    expected: "string or int literal".to_string(),
                    found: "other literal".to_string(),
                    pos: Position::new(0, 1, 1),
                });
            }
        };

        Ok(Lit { kind, span: Span::DUMMY })
    }

    fn convert_item_struct(&mut self, item_struct: &syn::ItemStruct) -> Result<Item> {
        let ident = self.convert_ident(&item_struct.ident);
        let vis = self.convert_visibility(&item_struct.vis);

        // Convert fields based on struct type
        let variant_data = match &item_struct.fields {
            syn::Fields::Named(fields_named) => {
                let fields = fields_named
                    .named
                    .iter()
                    .map(|f| self.convert_field(f))
                    .collect::<Result<Vec<_>>>()?;
                VariantData::Struct { fields, recovered: false }
            }
            syn::Fields::Unnamed(fields_unnamed) => {
                let fields = fields_unnamed
                    .unnamed
                    .iter()
                    .map(|f| self.convert_field(f))
                    .collect::<Result<Vec<_>>>()?;
                VariantData::Tuple(fields)
            }
            syn::Fields::Unit => VariantData::Unit,
        };

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::Struct(variant_data),
            tokens: None,
        })
    }

    fn convert_field(&mut self, field: &syn::Field) -> Result<FieldDef> {
        let ident = field.ident.as_ref().map(|i| self.convert_ident(i));
        let ty = self.convert_type(&field.ty)?;
        let vis = self.convert_visibility(&field.vis);

        Ok(FieldDef { attrs: vec![], id: self.next_id(), span: Span::DUMMY, vis, ident, ty })
    }

    fn convert_item_enum(&mut self, item_enum: &syn::ItemEnum) -> Result<Item> {
        let ident = self.convert_ident(&item_enum.ident);
        let vis = self.convert_visibility(&item_enum.vis);

        // Convert all variants
        let variants = item_enum
            .variants
            .iter()
            .map(|v| self.convert_variant(v))
            .collect::<Result<Vec<_>>>()?;

        let enum_def = EnumDef { variants };

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::Enum(enum_def),
            tokens: None,
        })
    }

    fn convert_variant(&mut self, variant: &syn::Variant) -> Result<Variant> {
        let ident = self.convert_ident(&variant.ident);
        let vis = Visibility::Inherited; // Variants inherit visibility from enum

        // Convert variant fields (same as struct fields)
        let data = match &variant.fields {
            syn::Fields::Named(fields_named) => {
                let fields = fields_named
                    .named
                    .iter()
                    .map(|f| self.convert_field(f))
                    .collect::<Result<Vec<_>>>()?;
                VariantData::Struct { fields, recovered: false }
            }
            syn::Fields::Unnamed(fields_unnamed) => {
                let fields = fields_unnamed
                    .unnamed
                    .iter()
                    .map(|f| self.convert_field(f))
                    .collect::<Result<Vec<_>>>()?;
                VariantData::Tuple(fields)
            }
            syn::Fields::Unit => VariantData::Unit,
        };

        // Convert discriminant if present (e.g., Foo = 1)
        let disr_expr = if let Some((_, expr)) = &variant.discriminant {
            Some(self.convert_expr(expr)?)
        } else {
            None
        };

        Ok(Variant {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            data,
            disr_expr,
        })
    }

    fn convert_item_impl(&mut self, item_impl: &syn::ItemImpl) -> Result<Item> {
        // Impl blocks don't have an ident like other items, so we create a dummy one
        let ident = Ident::new("impl".to_string(), Span::DUMMY);
        let vis = Visibility::Inherited; // Impl blocks don't have visibility

        // Convert safety (unsafe impl)
        let safety = match item_impl.unsafety {
            Some(_) => Safety::Unsafe,
            None => Safety::Safe,
        };

        // Convert generics
        let generics = self.convert_generics(&item_impl.generics)?;

        // Convert optional trait reference (trait impl vs inherent impl)
        let of_trait = if let Some((_, path, _)) = &item_impl.trait_ {
            Some(TraitRef { path: self.convert_path(path)? })
        } else {
            None
        };

        // Convert self type (the type being implemented for)
        let self_ty = self.convert_type(&item_impl.self_ty)?;

        // Convert impl items (methods, associated types, etc.)
        let items = item_impl
            .items
            .iter()
            .map(|item| self.convert_impl_item(item))
            .collect::<Result<Vec<_>>>()?;

        let impl_def = ImplDef { safety, generics, of_trait, self_ty, items };

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::Impl(Box::new(impl_def)),
            tokens: None,
        })
    }

    fn convert_impl_item(&mut self, item: &syn::ImplItem) -> Result<AssocItem> {
        match item {
            syn::ImplItem::Fn(method) => {
                let ident = self.convert_ident(&method.sig.ident);
                let vis = self.convert_visibility(&method.vis);

                // Convert the function signature
                let sig = self.convert_fn_sig(&method.sig)?;
                let generics = self.convert_generics(&method.sig.generics)?;

                // Impl methods always have a body
                let body = Some(self.convert_block(&method.block)?);

                let fn_def = Fn { defaultness: Defaultness::Final, sig, generics, body };

                Ok(AssocItem {
                    attrs: vec![],
                    id: self.next_id(),
                    span: Span::DUMMY,
                    vis,
                    ident,
                    kind: AssocItemKind::Fn(Box::new(fn_def)),
                })
            }
            syn::ImplItem::Type(ty) => {
                let ident = self.convert_ident(&ty.ident);
                let vis = self.convert_visibility(&ty.vis);

                // Associated type in impl always has a concrete type
                let concrete_ty = self.convert_type(&ty.ty)?;

                Ok(AssocItem {
                    attrs: vec![],
                    id: self.next_id(),
                    span: Span::DUMMY,
                    vis,
                    ident,
                    kind: AssocItemKind::Type(Box::new(Some(concrete_ty))),
                })
            }
            _ => Err(ParseError::Expected {
                expected: "impl method or associated type".to_string(),
                found: "unsupported impl item (const, macro)".to_string(),
                pos: Position::new(0, 1, 1),
            }),
        }
    }

    fn convert_item_use(&mut self, item_use: &syn::ItemUse) -> Result<Item> {
        // Use declarations don't have a meaningful ident, so we create a dummy one
        let ident = Ident::new("use".to_string(), Span::DUMMY);
        let vis = self.convert_visibility(&item_use.vis);

        // Convert the use tree
        let use_tree = self.convert_use_tree(&item_use.tree)?;

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::Use(use_tree),
            tokens: None,
        })
    }

    fn convert_use_tree(&mut self, tree: &syn::UseTree) -> Result<UseTree> {
        match tree {
            syn::UseTree::Path(use_path) => {
                // Recursive case: `foo::bar::...`
                let segment = PathSegment {
                    ident: self.convert_ident(&use_path.ident),
                    id: NodeId::DUMMY,
                    args: None,
                };

                // Recursively convert the rest of the tree
                let rest = self.convert_use_tree(&use_path.tree)?;

                // Combine the current segment with the rest
                let mut segments = vec![segment];
                segments.extend(rest.prefix.segments);

                Ok(UseTree {
                    prefix: Path { span: Span::DUMMY, segments, tokens: None },
                    kind: rest.kind,
                })
            }
            syn::UseTree::Name(use_name) => {
                // Simple name: `use foo::bar;`
                let segment = PathSegment {
                    ident: self.convert_ident(&use_name.ident),
                    id: NodeId::DUMMY,
                    args: None,
                };

                Ok(UseTree {
                    prefix: Path { span: Span::DUMMY, segments: vec![segment], tokens: None },
                    kind: UseTreeKind::Simple(None), // No renaming
                })
            }
            syn::UseTree::Rename(use_rename) => {
                // Rename: `use foo::bar as baz;`
                let segment = PathSegment {
                    ident: self.convert_ident(&use_rename.ident),
                    id: NodeId::DUMMY,
                    args: None,
                };

                let rename = self.convert_ident(&use_rename.rename);

                Ok(UseTree {
                    prefix: Path { span: Span::DUMMY, segments: vec![segment], tokens: None },
                    kind: UseTreeKind::Simple(Some(rename)),
                })
            }
            syn::UseTree::Glob(_) => {
                // Glob: `use foo::*;`
                Ok(UseTree {
                    prefix: Path { span: Span::DUMMY, segments: vec![], tokens: None },
                    kind: UseTreeKind::Glob,
                })
            }
            syn::UseTree::Group(use_group) => {
                // Nested: `use foo::{bar, baz};`
                let items = use_group
                    .items
                    .iter()
                    .map(|item| self.convert_use_tree(item))
                    .collect::<Result<Vec<_>>>()?;

                Ok(UseTree {
                    prefix: Path { span: Span::DUMMY, segments: vec![], tokens: None },
                    kind: UseTreeKind::Nested(items),
                })
            }
        }
    }

    fn convert_item_static(&mut self, item_static: &syn::ItemStatic) -> Result<Item> {
        let ident = self.convert_ident(&item_static.ident);
        let vis = self.convert_visibility(&item_static.vis);

        // Convert mutability
        let mutability = match item_static.mutability {
            syn::StaticMutability::Mut(_) => Mutability::Mut,
            syn::StaticMutability::None => Mutability::Not,
            _ => Mutability::Not, // Future-proof for new variants
        };

        // Convert type
        let ty = self.convert_type(&item_static.ty)?;

        // Convert initializer expression
        let expr = Some(self.convert_expr(&item_static.expr)?);

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::Static { mutability, ty, expr },
            tokens: None,
        })
    }

    fn convert_item_const(&mut self, item_const: &syn::ItemConst) -> Result<Item> {
        let ident = self.convert_ident(&item_const.ident);
        let vis = self.convert_visibility(&item_const.vis);

        // Convert type
        let ty = self.convert_type(&item_const.ty)?;

        // Convert initializer expression
        let expr = Some(self.convert_expr(&item_const.expr)?);

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::Const { ty, expr },
            tokens: None,
        })
    }

    fn convert_item_type(&mut self, item_type: &syn::ItemType) -> Result<Item> {
        let ident = self.convert_ident(&item_type.ident);
        let vis = self.convert_visibility(&item_type.vis);

        // Convert generics
        let generics = self.convert_generics(&item_type.generics)?;

        // Convert the aliased type
        let ty = Some(self.convert_type(&item_type.ty)?);

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::TyAlias { generics, ty },
            tokens: None,
        })
    }

    fn convert_item_mod(&mut self, item_mod: &syn::ItemMod) -> Result<Item> {
        let ident = self.convert_ident(&item_mod.ident);
        let vis = self.convert_visibility(&item_mod.vis);

        // Convert module items if present (inline module vs external module)
        let items = if let Some((_, module_items)) = &item_mod.content {
            // Inline module: `mod foo { ... }`
            let converted_items = module_items
                .iter()
                .map(|item| self.convert_item(item))
                .collect::<Result<Vec<_>>>()?;
            Some(converted_items)
        } else {
            // External module: `mod foo;`
            None
        };

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::Mod { items },
            tokens: None,
        })
    }

    fn convert_type_param_bound(&mut self, bound: &syn::TypeParamBound) -> Result<GenericBound> {
        match bound {
            syn::TypeParamBound::Trait(trait_bound) => {
                let path = self.convert_path(&trait_bound.path)?;
                Ok(GenericBound::Trait(TraitRef { path }))
            }
            syn::TypeParamBound::Lifetime(_) => Err(ParseError::Expected {
                expected: "trait bound".to_string(),
                found: "lifetime bound (not yet supported)".to_string(),
                pos: Position::new(0, 1, 1),
            }),
            _ => Err(ParseError::Expected {
                expected: "trait bound".to_string(),
                found: "unknown bound type".to_string(),
                pos: Position::new(0, 1, 1),
            }),
        }
    }

    fn convert_item_trait(&mut self, item_trait: &syn::ItemTrait) -> Result<Item> {
        let ident = self.convert_ident(&item_trait.ident);
        let vis = self.convert_visibility(&item_trait.vis);

        // Convert safety (unsafe trait or not)
        let safety = match item_trait.unsafety {
            Some(_) => Safety::Unsafe,
            None => Safety::Safe,
        };

        // Convert generics
        let generics = self.convert_generics(&item_trait.generics)?;

        // Convert supertraits (bounds like `: Clone + Send`)
        let bounds = item_trait
            .supertraits
            .iter()
            .map(|b| self.convert_type_param_bound(b))
            .collect::<Result<Vec<_>>>()?;

        // Convert trait items (methods, types, etc.)
        let items = item_trait
            .items
            .iter()
            .map(|item| self.convert_trait_item(item))
            .collect::<Result<Vec<_>>>()?;

        let trait_def = TraitDef { safety, generics, bounds, items };

        Ok(Item {
            attrs: vec![],
            id: self.next_id(),
            span: Span::DUMMY,
            vis,
            ident,
            kind: ItemKind::Trait(Box::new(trait_def)),
            tokens: None,
        })
    }

    fn convert_trait_item(&mut self, item: &syn::TraitItem) -> Result<AssocItem> {
        match item {
            syn::TraitItem::Fn(method) => {
                let ident = self.convert_ident(&method.sig.ident);
                let vis = Visibility::Inherited; // Trait items are public by default

                // Convert the function signature
                let sig = self.convert_fn_sig(&method.sig)?;
                let generics = self.convert_generics(&method.sig.generics)?;

                // Trait methods may or may not have a body
                let body = if let Some(block) = &method.default {
                    Some(self.convert_block(block)?)
                } else {
                    None
                };

                let fn_def = Fn { defaultness: Defaultness::Final, sig, generics, body };

                Ok(AssocItem {
                    attrs: vec![],
                    id: self.next_id(),
                    span: Span::DUMMY,
                    vis,
                    ident,
                    kind: AssocItemKind::Fn(Box::new(fn_def)),
                })
            }
            syn::TraitItem::Type(ty) => {
                let ident = self.convert_ident(&ty.ident);
                let vis = Visibility::Inherited;

                // Associated type may have a default
                let default_ty = if let Some((_eq, ty)) = &ty.default {
                    Some(self.convert_type(ty)?)
                } else {
                    None
                };

                Ok(AssocItem {
                    attrs: vec![],
                    id: self.next_id(),
                    span: Span::DUMMY,
                    vis,
                    ident,
                    kind: AssocItemKind::Type(Box::new(default_ty)),
                })
            }
            _ => Err(ParseError::Expected {
                expected: "trait method or associated type".to_string(),
                found: "unsupported trait item (const, macro)".to_string(),
                pos: Position::new(0, 1, 1),
            }),
        }
    }
}