cargo-brief 0.0.2

Visibility-aware Rust API extractor — pseudo-Rust output for AI agent consumption
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
use rustdoc_types::{
    Constant, Enum, Function, GenericArg, GenericArgs, GenericBound, GenericParamDefKind, Item,
    ItemEnum, Static, Struct, StructKind, Trait, Type, TypeAlias, Union, VariantKind, Visibility,
};

use crate::cli::BriefArgs;
use crate::model::{CrateModel, is_visible_from};

/// Render the API of a target module as pseudo-Rust.
///
/// Returns an error if the target module path is specified but not found.
pub fn render_module_api(
    model: &CrateModel,
    target_module_path: Option<&str>,
    args: &BriefArgs,
    observer_module_path: Option<&str>,
    same_crate: bool,
) -> String {
    let mut output = String::new();

    let crate_name = model.crate_name();
    output.push_str(&format!("// crate {crate_name}\n"));

    let target_item = if let Some(path) = target_module_path {
        model.find_module(path)
    } else {
        model.root_module()
    };

    let Some(target_item) = target_item else {
        if let Some(path) = target_module_path {
            output.push_str(&format!("// ERROR: module '{path}' not found\n"));
            output.push_str("// Available modules:\n");
            let mut paths: Vec<&str> = model.module_index.keys().map(|s| s.as_str()).collect();
            paths.sort();
            for p in paths {
                output.push_str(&format!("//   {p}\n"));
            }
        } else {
            output.push_str("// ERROR: crate root module not found\n");
        }
        return output;
    };

    let observer = observer_module_path
        .map(|p| {
            if p.contains("::") || p == crate_name {
                p.to_string()
            } else {
                format!("{crate_name}::{p}")
            }
        })
        .unwrap_or_else(|| crate_name.to_string());

    let depth = if args.recursive { u32::MAX } else { args.depth };

    let mod_display_path = target_module_path.unwrap_or(crate_name);

    render_module_contents(
        model,
        target_item,
        args,
        &observer,
        same_crate,
        depth,
        0,
        mod_display_path,
        &mut output,
    );

    output
}

#[allow(clippy::too_many_arguments)]
fn render_module_contents(
    model: &CrateModel,
    module_item: &Item,
    args: &BriefArgs,
    observer: &str,
    same_crate: bool,
    max_depth: u32,
    current_depth: u32,
    display_path: &str,
    output: &mut String,
) {
    let indent = "    ".repeat(current_depth as usize);

    if current_depth > 0 {
        output.push_str(&format!("{indent}mod {} {{\n", last_segment(display_path)));
    }

    let children = model.module_children(module_item);

    for (child_id, child) in &children {
        // Check visibility (skip items that aren't visible from observer)
        if !matches!(child.visibility, Visibility::Default) {
            if !is_visible_from(model, child, child_id, observer, same_crate) {
                continue;
            }
        }

        // Use items may have name=None at the item level; name is inside inner.use
        let name = child.name.as_deref().or_else(|| {
            if let ItemEnum::Use(u) = &child.inner {
                Some(u.name.as_str())
            } else {
                None
            }
        });
        let Some(name) = name else { continue };

        let child_indent = if current_depth > 0 {
            format!("{indent}    ")
        } else {
            String::new()
        };

        match &child.inner {
            ItemEnum::Module(_) => {
                if current_depth < max_depth {
                    let child_path = format!("{display_path}::{name}");
                    render_module_contents(
                        model,
                        child,
                        args,
                        observer,
                        same_crate,
                        max_depth,
                        current_depth + 1,
                        &child_path,
                        output,
                    );
                } else {
                    output.push_str(&format!("{child_indent}mod {name} {{ /* ... */ }}\n"));
                }
            }
            ItemEnum::Struct(_) if !args.no_structs => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::Enum(_) if !args.no_enums => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::Trait(_) if !args.no_traits => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::Function(_) if !args.no_functions => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::TypeAlias(_) if !args.no_aliases => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::Constant { .. } if !args.no_constants => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::Static(_) if !args.no_constants => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::Union(_) if !args.no_unions => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::Macro(_) if !args.no_macros => {
                render_item(
                    model,
                    child,
                    child_id,
                    &child_indent,
                    observer,
                    same_crate,
                    output,
                );
            }
            ItemEnum::Use(use_item) => {
                // Render re-exports
                if let Some(target_id) = &use_item.id {
                    if let Some(target_item) = model.krate.index.get(target_id) {
                        render_use(child, use_item, target_item, &child_indent, output);
                    }
                }
            }
            _ => {}
        }
    }

    // Render impl blocks for types in this module
    render_impl_blocks(
        model,
        module_item,
        args,
        &indent,
        observer,
        same_crate,
        current_depth,
        output,
    );

    if current_depth > 0 {
        output.push_str(&format!("{indent}}}\n"));
    }
}

fn render_impl_blocks(
    model: &CrateModel,
    module_item: &Item,
    args: &BriefArgs,
    indent: &str,
    observer: &str,
    same_crate: bool,
    current_depth: u32,
    output: &mut String,
) {
    let child_indent = if current_depth > 0 {
        format!("{indent}    ")
    } else {
        String::new()
    };

    // Collect impl IDs from visible structs/enums/unions in this module
    let children = model.module_children(module_item);
    let mut impl_ids: Vec<Id> = Vec::new();

    for (child_id, child) in &children {
        // Only collect impls for types visible from the observer
        if !matches!(child.visibility, Visibility::Default) {
            if !is_visible_from(model, child, child_id, observer, same_crate) {
                continue;
            }
        }

        let impls = match &child.inner {
            ItemEnum::Struct(s) => &s.impls,
            ItemEnum::Enum(e) => &e.impls,
            ItemEnum::Union(u) => &u.impls,
            _ => continue,
        };
        impl_ids.extend(impls.iter().cloned());
    }

    for impl_id in &impl_ids {
        let Some(impl_item) = model.krate.index.get(impl_id) else {
            continue;
        };
        let ItemEnum::Impl(impl_block) = &impl_item.inner else {
            continue;
        };

        // Skip blanket impls and synthetic impls unless --all
        if !args.all && (impl_block.is_synthetic || impl_block.blanket_impl.is_some()) {
            continue;
        }

        let type_name = format_type(&impl_block.for_);
        if type_name.is_empty() {
            continue;
        }

        let generics = format_generics(&impl_block.generics);
        let impl_header = if let Some(trait_) = &impl_block.trait_ {
            let trait_path = format_path(trait_);
            format!("{child_indent}impl{generics} {trait_path} for {type_name}")
        } else {
            format!("{child_indent}impl{generics} {type_name}")
        };

        // Collect visible methods/items in this impl block
        let mut rendered_items = Vec::new();
        let inner_indent = format!("{child_indent}    ");

        for item_id in &impl_block.items {
            if let Some(item) = model.krate.index.get(item_id) {
                // For impl items, check visibility
                if !matches!(item.visibility, Visibility::Default | Visibility::Public) {
                    if !is_visible_from(model, item, item_id, observer, same_crate) {
                        continue;
                    }
                }

                if let Some(r) = render_impl_item(item, &inner_indent, args) {
                    rendered_items.push(r);
                }
            }
        }

        if !rendered_items.is_empty() {
            render_docs(impl_item, &child_indent, output);
            output.push_str(&format!("{impl_header} {{\n"));
            for item_str in &rendered_items {
                output.push_str(item_str);
            }
            output.push_str(&format!("{child_indent}}}\n"));
        }
    }
}

fn render_item(
    model: &CrateModel,
    item: &Item,
    _item_id: &Id,
    indent: &str,
    observer: &str,
    same_crate: bool,
    output: &mut String,
) {
    render_docs(item, indent, output);
    let vis = format_visibility(&item.visibility);

    match &item.inner {
        ItemEnum::Struct(s) => {
            render_struct(model, item, s, indent, &vis, observer, same_crate, output);
        }
        ItemEnum::Enum(e) => {
            render_enum(model, item, e, indent, &vis, output);
        }
        ItemEnum::Trait(t) => {
            render_trait(model, item, t, indent, &vis, output);
        }
        ItemEnum::Function(f) => {
            let name = item.name.as_deref().unwrap_or("?");
            let sig = format_function_sig(name, f, &vis);
            output.push_str(&format!("{indent}{sig};\n"));
        }
        ItemEnum::TypeAlias(ta) => {
            render_type_alias(item, ta, indent, &vis, output);
        }
        ItemEnum::Constant { type_, const_: c } => {
            render_constant(item, type_, c, indent, &vis, output);
        }
        ItemEnum::Static(s) => {
            render_static(item, s, indent, &vis, output);
        }
        ItemEnum::Union(u) => {
            render_union(model, item, u, indent, &vis, observer, same_crate, output);
        }
        ItemEnum::Macro(_) => {
            let name = item.name.as_deref().unwrap_or("?");
            output.push_str(&format!("{indent}macro_rules! {name} {{ /* ... */ }}\n"));
        }
        _ => {}
    }
}

fn render_struct(
    model: &CrateModel,
    item: &Item,
    s: &Struct,
    indent: &str,
    vis: &str,
    observer: &str,
    same_crate: bool,
    output: &mut String,
) {
    let name = item.name.as_deref().unwrap_or("?");
    let generics = format_generics(&s.generics);

    match &s.kind {
        StructKind::Unit => {
            output.push_str(&format!("{indent}{vis}struct {name}{generics};\n"));
        }
        StructKind::Tuple(fields) => {
            let field_strs: Vec<String> = fields
                .iter()
                .map(|f_id| {
                    f_id.as_ref()
                        .and_then(|id| model.krate.index.get(id))
                        .map(|f| {
                            if let ItemEnum::StructField(ty) = &f.inner {
                                let fvis = format_visibility(&f.visibility);
                                format!("{fvis}{}", format_type(ty))
                            } else {
                                "?".to_string()
                            }
                        })
                        .unwrap_or_else(|| "/* private */".to_string())
                })
                .collect();
            output.push_str(&format!(
                "{indent}{vis}struct {name}{generics}({});\n",
                field_strs.join(", ")
            ));
        }
        StructKind::Plain {
            fields,
            has_stripped_fields,
        } => {
            output.push_str(&format!("{indent}{vis}struct {name}{generics} {{\n"));
            for field_id in fields {
                if let Some(field_item) = model.krate.index.get(field_id) {
                    // Check field visibility
                    if !is_visible_from(model, field_item, field_id, observer, same_crate)
                        && !matches!(field_item.visibility, Visibility::Public)
                    {
                        continue;
                    }
                    if let ItemEnum::StructField(ty) = &field_item.inner {
                        let fname = field_item.name.as_deref().unwrap_or("?");
                        let fvis = format_visibility(&field_item.visibility);
                        render_docs(field_item, &format!("{indent}    "), output);
                        output.push_str(&format!(
                            "{indent}    {fvis}{fname}: {},\n",
                            format_type(ty)
                        ));
                    }
                }
            }
            if *has_stripped_fields {
                output.push_str(&format!("{indent}    // ... private fields\n"));
            }
            output.push_str(&format!("{indent}}}\n"));
        }
    }
}

fn render_enum(
    model: &CrateModel,
    item: &Item,
    e: &Enum,
    indent: &str,
    vis: &str,
    output: &mut String,
) {
    let name = item.name.as_deref().unwrap_or("?");
    let generics = format_generics(&e.generics);
    output.push_str(&format!("{indent}{vis}enum {name}{generics} {{\n"));

    for variant_id in &e.variants {
        if let Some(variant_item) = model.krate.index.get(variant_id) {
            render_docs(variant_item, &format!("{indent}    "), output);
            let vname = variant_item.name.as_deref().unwrap_or("?");
            if let ItemEnum::Variant(variant) = &variant_item.inner {
                match &variant.kind {
                    VariantKind::Plain => {
                        output.push_str(&format!("{indent}    {vname},\n"));
                    }
                    VariantKind::Tuple(fields) => {
                        let field_strs: Vec<String> = fields
                            .iter()
                            .map(|f_id| {
                                f_id.as_ref()
                                    .and_then(|id| model.krate.index.get(id))
                                    .map(|f| {
                                        if let ItemEnum::StructField(ty) = &f.inner {
                                            format_type(ty)
                                        } else {
                                            "?".to_string()
                                        }
                                    })
                                    .unwrap_or_else(|| "_".to_string())
                            })
                            .collect();
                        output.push_str(&format!(
                            "{indent}    {vname}({}),\n",
                            field_strs.join(", ")
                        ));
                    }
                    VariantKind::Struct {
                        fields,
                        has_stripped_fields,
                    } => {
                        output.push_str(&format!("{indent}    {vname} {{\n"));
                        for field_id in fields {
                            if let Some(field_item) = model.krate.index.get(field_id) {
                                if let ItemEnum::StructField(ty) = &field_item.inner {
                                    let fname = field_item.name.as_deref().unwrap_or("?");
                                    output.push_str(&format!(
                                        "{indent}        {fname}: {},\n",
                                        format_type(ty)
                                    ));
                                }
                            }
                        }
                        if *has_stripped_fields {
                            output.push_str(&format!("{indent}        // ... private fields\n"));
                        }
                        output.push_str(&format!("{indent}    }},\n"));
                    }
                }
            }
        }
    }

    output.push_str(&format!("{indent}}}\n"));
}

fn render_trait(
    model: &CrateModel,
    item: &Item,
    t: &Trait,
    indent: &str,
    vis: &str,
    output: &mut String,
) {
    let name = item.name.as_deref().unwrap_or("?");
    let generics = format_generics(&t.generics);

    let bounds = if t.bounds.is_empty() {
        String::new()
    } else {
        let bound_strs: Vec<String> = t.bounds.iter().map(format_generic_bound).collect();
        format!(": {}", bound_strs.join(" + "))
    };

    output.push_str(&format!("{indent}{vis}trait {name}{generics}{bounds} {{\n"));

    for item_id in &t.items {
        if let Some(trait_item) = model.krate.index.get(item_id) {
            let inner_indent = format!("{indent}    ");
            render_docs(trait_item, &inner_indent, output);
            match &trait_item.inner {
                ItemEnum::Function(f) => {
                    let mname = trait_item.name.as_deref().unwrap_or("?");
                    let sig = format_function_sig(mname, f, "");
                    output.push_str(&format!("{inner_indent}{sig};\n"));
                }
                ItemEnum::AssocType {
                    generics: _,
                    bounds,
                    type_,
                } => {
                    let tname = trait_item.name.as_deref().unwrap_or("?");
                    let bounds_str = if bounds.is_empty() {
                        String::new()
                    } else {
                        let b: Vec<String> = bounds.iter().map(format_generic_bound).collect();
                        format!(": {}", b.join(" + "))
                    };
                    if let Some(default) = type_ {
                        output.push_str(&format!(
                            "{inner_indent}type {tname}{bounds_str} = {};\n",
                            format_type(default)
                        ));
                    } else {
                        output.push_str(&format!("{inner_indent}type {tname}{bounds_str};\n"));
                    }
                }
                ItemEnum::AssocConst { type_, value } => {
                    let cname = trait_item.name.as_deref().unwrap_or("?");
                    let val = value
                        .as_deref()
                        .map_or(String::new(), |v| format!(" = {v}"));
                    output.push_str(&format!(
                        "{inner_indent}const {cname}: {}{val};\n",
                        format_type(type_)
                    ));
                }
                _ => {}
            }
        }
    }

    output.push_str(&format!("{indent}}}\n"));
}

fn render_type_alias(item: &Item, ta: &TypeAlias, indent: &str, vis: &str, output: &mut String) {
    let name = item.name.as_deref().unwrap_or("?");
    let generics = format_generics(&ta.generics);
    output.push_str(&format!(
        "{indent}{vis}type {name}{generics} = {};\n",
        format_type(&ta.type_)
    ));
}

fn render_constant(
    item: &Item,
    type_: &Type,
    c: &Constant,
    indent: &str,
    vis: &str,
    output: &mut String,
) {
    let name = item.name.as_deref().unwrap_or("?");
    let val = c.value.as_deref().unwrap_or("_");
    output.push_str(&format!(
        "{indent}{vis}const {name}: {} = {val};\n",
        format_type(type_)
    ));
}

fn render_static(item: &Item, s: &Static, indent: &str, vis: &str, output: &mut String) {
    let name = item.name.as_deref().unwrap_or("?");
    let mutability = if s.is_mutable { "mut " } else { "" };
    let val = if s.expr.is_empty() { "_" } else { &s.expr };
    output.push_str(&format!(
        "{indent}{vis}static {mutability}{name}: {} = {val};\n",
        format_type(&s.type_)
    ));
}

fn render_union(
    model: &CrateModel,
    item: &Item,
    u: &Union,
    indent: &str,
    vis: &str,
    observer: &str,
    same_crate: bool,
    output: &mut String,
) {
    let name = item.name.as_deref().unwrap_or("?");
    let generics = format_generics(&u.generics);
    output.push_str(&format!("{indent}{vis}union {name}{generics} {{\n"));

    for field_id in &u.fields {
        if let Some(field_item) = model.krate.index.get(field_id) {
            if !is_visible_from(model, field_item, field_id, observer, same_crate)
                && !matches!(field_item.visibility, Visibility::Public)
            {
                continue;
            }
            if let ItemEnum::StructField(ty) = &field_item.inner {
                let fname = field_item.name.as_deref().unwrap_or("?");
                let fvis = format_visibility(&field_item.visibility);
                output.push_str(&format!(
                    "{indent}    {fvis}{fname}: {},\n",
                    format_type(ty)
                ));
            }
        }
    }

    if u.has_stripped_fields {
        output.push_str(&format!("{indent}    // ... private fields\n"));
    }
    output.push_str(&format!("{indent}}}\n"));
}

fn render_use(
    item: &Item,
    use_item: &rustdoc_types::Use,
    _target_item: &Item,
    indent: &str,
    output: &mut String,
) {
    let vis = format_visibility(&item.visibility);
    let source = &use_item.source;
    let alias = &use_item.name;
    if source.ends_with(alias.as_str()) {
        output.push_str(&format!("{indent}{vis}use {source};\n"));
    } else {
        output.push_str(&format!("{indent}{vis}use {source} as {alias};\n"));
    }
}

fn render_impl_item(item: &Item, indent: &str, _args: &BriefArgs) -> Option<String> {
    let mut out = String::new();

    match &item.inner {
        ItemEnum::Function(f) => {
            let name = item.name.as_deref().unwrap_or("?");
            let vis = format_visibility(&item.visibility);
            render_docs(item, indent, &mut out);
            let sig = format_function_sig(name, f, &vis);
            out.push_str(&format!("{indent}{sig};\n"));
            Some(out)
        }
        ItemEnum::AssocType {
            generics: _,
            bounds: _,
            type_,
        } => {
            let name = item.name.as_deref().unwrap_or("?");
            if let Some(ty) = type_ {
                out.push_str(&format!("{indent}type {name} = {};\n", format_type(ty)));
            }
            Some(out)
        }
        ItemEnum::AssocConst { type_, value } => {
            let name = item.name.as_deref().unwrap_or("?");
            let val = value.as_deref().unwrap_or("_");
            out.push_str(&format!(
                "{indent}const {name}: {} = {val};\n",
                format_type(type_)
            ));
            Some(out)
        }
        _ => None,
    }
}

fn render_docs(item: &Item, indent: &str, output: &mut String) {
    if let Some(docs) = &item.docs {
        for line in docs.lines() {
            if line.is_empty() {
                output.push_str(&format!("{indent}///\n"));
            } else {
                output.push_str(&format!("{indent}/// {line}\n"));
            }
        }
    }
}

// === Type formatting ===

fn format_visibility(vis: &Visibility) -> String {
    match vis {
        Visibility::Public => "pub ".to_string(),
        Visibility::Crate => "pub(crate) ".to_string(),
        Visibility::Restricted { parent: _, path } => format!("pub(in {path}) "),
        Visibility::Default => String::new(),
    }
}

fn format_type(ty: &Type) -> String {
    match ty {
        Type::ResolvedPath(path) => format_path(path),
        Type::DynTrait(dyn_trait) => {
            let traits: Vec<String> = dyn_trait
                .traits
                .iter()
                .map(|pt| format_path(&pt.trait_))
                .collect();
            let lifetime = dyn_trait
                .lifetime
                .as_deref()
                .map(|l| format!(" + {l}"))
                .unwrap_or_default();
            format!("dyn {}{lifetime}", traits.join(" + "))
        }
        Type::Generic(name) => name.clone(),
        Type::Primitive(name) => name.clone(),
        Type::FunctionPointer(fp) => {
            let params: Vec<String> = fp.sig.inputs.iter().map(|(_n, t)| format_type(t)).collect();
            let ret = fp
                .sig
                .output
                .as_ref()
                .map(|t| format!(" -> {}", format_type(t)))
                .unwrap_or_default();
            format!("fn({}){ret}", params.join(", "))
        }
        Type::Tuple(types) => {
            let inner: Vec<String> = types.iter().map(format_type).collect();
            format!("({})", inner.join(", "))
        }
        Type::Slice(ty) => format!("[{}]", format_type(ty)),
        Type::Array { type_, len } => format!("[{}; {len}]", format_type(type_)),
        Type::Pat {
            type_,
            __pat_unstable_do_not_use: pat,
        } => {
            format!("{}: {pat}", format_type(type_))
        }
        Type::ImplTrait(bounds) => {
            let bound_strs: Vec<String> = bounds.iter().map(format_generic_bound).collect();
            format!("impl {}", bound_strs.join(" + "))
        }
        Type::Infer => "_".to_string(),
        Type::RawPointer { is_mutable, type_ } => {
            let mutability = if *is_mutable { "mut" } else { "const" };
            format!("*{mutability} {}", format_type(type_))
        }
        Type::BorrowedRef {
            lifetime,
            is_mutable,
            type_,
        } => {
            let lt = lifetime
                .as_deref()
                .map(|l| format!("{l} "))
                .unwrap_or_default();
            let mutability = if *is_mutable { "mut " } else { "" };
            format!("&{lt}{mutability}{}", format_type(type_))
        }
        Type::QualifiedPath {
            name,
            args: _,
            self_type,
            trait_,
        } => {
            let self_ty = format_type(self_type);
            if let Some(trait_path) = trait_ {
                format!("<{self_ty} as {}>::{name}", format_path(trait_path))
            } else {
                format!("{self_ty}::{name}")
            }
        }
    }
}

fn format_path(path: &rustdoc_types::Path) -> String {
    let name = &path.path;
    if let Some(args) = &path.args {
        let args_str = format_generic_args(args);
        if args_str.is_empty() {
            name.clone()
        } else {
            format!("{name}{args_str}")
        }
    } else {
        name.clone()
    }
}

fn format_generic_args(args: &GenericArgs) -> String {
    match args {
        GenericArgs::ReturnTypeNotation => "(..)".to_string(),
        GenericArgs::AngleBracketed { args, constraints } => {
            let mut parts = Vec::new();
            for arg in args {
                match arg {
                    GenericArg::Lifetime(lt) => parts.push(lt.clone()),
                    GenericArg::Type(ty) => parts.push(format_type(ty)),
                    GenericArg::Const(c) => parts.push(c.value.clone().unwrap_or_default()),
                    GenericArg::Infer => parts.push("_".to_string()),
                }
            }
            for c in constraints {
                parts.push(format!("{} = ...", c.name));
            }
            if parts.is_empty() {
                String::new()
            } else {
                format!("<{}>", parts.join(", "))
            }
        }
        GenericArgs::Parenthesized { inputs, output } => {
            let params: Vec<String> = inputs.iter().map(format_type).collect();
            let ret = output
                .as_ref()
                .map(|t| format!(" -> {}", format_type(t)))
                .unwrap_or_default();
            format!("({}){ret}", params.join(", "))
        }
    }
}

fn format_generics(generics: &rustdoc_types::Generics) -> String {
    if generics.params.is_empty() {
        return String::new();
    }

    let params: Vec<String> = generics
        .params
        .iter()
        .map(|p| {
            let name = &p.name;
            match &p.kind {
                GenericParamDefKind::Lifetime { outlives } => {
                    if outlives.is_empty() {
                        name.clone()
                    } else {
                        format!("{name}: {}", outlives.join(" + "))
                    }
                }
                GenericParamDefKind::Type {
                    bounds,
                    default,
                    is_synthetic: _,
                } => {
                    let bounds_str = if bounds.is_empty() {
                        String::new()
                    } else {
                        let b: Vec<String> = bounds.iter().map(format_generic_bound).collect();
                        format!(": {}", b.join(" + "))
                    };
                    let default_str = default
                        .as_ref()
                        .map(|d| format!(" = {}", format_type(d)))
                        .unwrap_or_default();
                    format!("{name}{bounds_str}{default_str}")
                }
                GenericParamDefKind::Const { type_, default } => {
                    let default_str = default
                        .as_deref()
                        .map(|d| format!(" = {d}"))
                        .unwrap_or_default();
                    format!("const {name}: {}{default_str}", format_type(type_))
                }
            }
        })
        .collect();

    format!("<{}>", params.join(", "))
}

fn format_function_sig(name: &str, f: &Function, vis: &str) -> String {
    let generics = format_generics(&f.generics);

    let header = &f.header;
    let is_async = header.is_async;
    let is_unsafe = header.is_unsafe;
    let is_const = header.is_const;

    let mut qualifiers = String::new();
    if is_const {
        qualifiers.push_str("const ");
    }
    if is_async {
        qualifiers.push_str("async ");
    }
    if is_unsafe {
        qualifiers.push_str("unsafe ");
    }

    let params: Vec<String> = f
        .sig
        .inputs
        .iter()
        .map(|(pname, ptype)| {
            let ty = format_type(ptype);
            // Detect self parameters
            if pname == "self" {
                match ptype {
                    Type::BorrowedRef { is_mutable, .. } => {
                        if *is_mutable {
                            "&mut self".to_string()
                        } else {
                            "&self".to_string()
                        }
                    }
                    _ => "self".to_string(),
                }
            } else {
                format!("{pname}: {ty}")
            }
        })
        .collect();

    let ret = f
        .sig
        .output
        .as_ref()
        .map(|t| format!(" -> {}", format_type(t)))
        .unwrap_or_default();

    format!(
        "{vis}{qualifiers}fn {name}{generics}({}){ret}",
        params.join(", ")
    )
}

fn format_generic_bound(bound: &GenericBound) -> String {
    match bound {
        GenericBound::TraitBound {
            trait_,
            generic_params: _,
            modifier,
        } => {
            let prefix = match modifier {
                rustdoc_types::TraitBoundModifier::None => "",
                rustdoc_types::TraitBoundModifier::Maybe => "?",
                rustdoc_types::TraitBoundModifier::MaybeConst => "~const ",
            };
            format!("{prefix}{}", format_path(trait_))
        }
        GenericBound::Outlives(lt) => lt.clone(),
        GenericBound::Use(_) => "use<...>".to_string(),
    }
}

fn last_segment(path: &str) -> &str {
    path.rsplit("::").next().unwrap_or(path)
}

use rustdoc_types::Id;