mcp-forge 0.1.2

Generate Rust MCP servers from OpenAPI specs
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
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
//! Typed Rust emission surface.
//!
//! Per ★★ TYPED EMISSION, generated Rust is built as a typed tree and
//! rendered once, never spliced together with `format!()`. This module is
//! the third sanctioned shape: a typed AST builder.
//!
//! It follows the `NixValue` precedent in `iac-forge/src/nix.rs` — a
//! target-language-specific value tree owning its own renderer — rather
//! than the `SExpr` precedent in `lava-api-forge/src/emit.rs`. `SExpr` is
//! the right surface for a homoiconic target, where every form is a list.
//! Rust is not homoiconic: it has distinct identifier, type, expression,
//! statement and item categories, and keeping those categories as separate
//! Rust types is exactly what makes a category error un-writable. An
//! `SExpr`-shaped surface would flatten all five into `List` and give that
//! guarantee away.
//!
//! # What this surface makes impossible
//!
//! - **A category error.** [`Expr`], [`TypeExpr`], [`Ident`] and [`Stmt`]
//!   are distinct types. Passing an identifier where an expression belongs,
//!   or an expression where a type belongs, does not compile. There is no
//!   `Raw(String)` variant anywhere in this module — no escape hatch back
//!   into untyped syntax.
//! - **An unescaped string literal.** [`StrLit`] holds the *raw* value.
//!   Escaping happens in the renderer and nowhere else, so a literal
//!   carrying a quote or a backslash cannot be emitted unescaped. There is
//!   no constructor that accepts pre-escaped text.
//! - **A malformed `format!` template.** [`FormatTemplate`] is a sequence of
//!   literal chunks and holes. Braces inside a literal chunk are doubled by
//!   the renderer, so a literal `{` in generated output cannot accidentally
//!   open a format hole — the failure mode of building a nested `format!`
//!   template by hand.
//!
//! # What it does not catch
//!
//! [`Ident::new`] rejects text that is not a Rust identifier, but it accepts
//! keywords (`crate`, `Self`, `match`), which are legal in path position and
//! illegal in binding position. That distinction is not modelled. Callers
//! that build an identifier from unvalidated input get a rejection at the
//! parse boundary; callers that build one from a keyword get valid-looking
//! output that may not compile.

use std::fmt::Write as _;

use crate::ir::RustType;

// ── Identifiers and paths ──────────────────────────────────────────────────

/// Text that is not a legal Rust identifier.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidIdent {
    text: String,
    reason: &'static str,
}

impl std::fmt::Display for InvalidIdent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "invalid Rust identifier {:?}: {}", self.text, self.reason)
    }
}

impl std::error::Error for InvalidIdent {}

/// A validated Rust identifier.
///
/// The only way to build one is [`Ident::new`], which rejects anything that
/// is not `[A-Za-z_][A-Za-z0-9_]*`. This is the parse boundary for every
/// name that reaches generated output.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Ident(String);

impl Ident {
    /// Validate `text` as a Rust identifier.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidIdent`] if `text` is empty, starts with a digit, or
    /// contains a character outside `[A-Za-z0-9_]`.
    pub fn new(text: &str) -> Result<Self, InvalidIdent> {
        let invalid = |reason| InvalidIdent { text: text.to_string(), reason };

        let mut chars = text.chars();
        let Some(first) = chars.next() else {
            return Err(invalid("identifiers cannot be empty"));
        };
        if !(first.is_ascii_alphabetic() || first == '_') {
            return Err(invalid("identifiers must start with a letter or underscore"));
        }
        if let Some(bad) = chars.find(|c| !(c.is_ascii_alphanumeric() || *c == '_')) {
            return Err(match bad {
                ' ' => invalid("identifiers cannot contain spaces"),
                '-' => invalid("identifiers cannot contain hyphens"),
                '.' | ':' => invalid("identifiers cannot contain path separators"),
                _ => invalid("identifiers may only contain letters, digits and underscores"),
            });
        }
        Ok(Self(text.to_string()))
    }

    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// A `::`-separated path, e.g. `std::time::Duration` or `Self`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Path(Vec<Ident>);

impl Path {
    /// Build a path from its segments.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidIdent`] if any segment is not a legal identifier, or
    /// if `segments` is empty.
    pub fn new(segments: &[&str]) -> Result<Self, InvalidIdent> {
        if segments.is_empty() {
            return Err(InvalidIdent {
                text: String::new(),
                reason: "a path needs at least one segment",
            });
        }
        segments.iter().map(|s| Ident::new(s)).collect::<Result<Vec<_>, _>>().map(Self)
    }

    /// A single-segment path.
    #[must_use]
    pub fn from_ident(ident: Ident) -> Self {
        Self(vec![ident])
    }

    /// Append a segment, e.g. `Error` onto `crate::error`.
    #[must_use]
    pub fn join(mut self, segment: Ident) -> Self {
        self.0.push(segment);
        self
    }
}

// ── String literals ────────────────────────────────────────────────────────

/// A Rust string literal, holding its **raw** value.
///
/// Escaping is performed by the renderer. There is no constructor that takes
/// pre-escaped text, so an unescaped literal is not representable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StrLit {
    value: String,
    /// Collapse newlines to spaces before escaping.
    ///
    /// Rust string literals may span lines, but a literal inside a
    /// single-line attribute (`#[doc = "…"]`, `#[tool(description = "…")]`)
    /// must not. This flag is the difference, named in the type rather than
    /// hidden in a helper applied by hand at each call site.
    one_line: bool,
}

impl StrLit {
    /// A literal that preserves its value exactly, escaping newlines as `\n`.
    #[must_use]
    pub fn new(value: impl Into<String>) -> Self {
        Self { value: value.into(), one_line: false }
    }

    /// A literal for single-line attribute position: newlines become spaces.
    #[must_use]
    pub fn one_line(value: impl Into<String>) -> Self {
        Self { value: value.into(), one_line: true }
    }

    /// Render as a Rust string literal, quotes included.
    #[must_use]
    pub fn to_rust(&self) -> String {
        let mut out = String::new();
        self.render(&mut out);
        out
    }

    fn render(&self, out: &mut String) {
        out.push('"');
        for c in self.value.chars() {
            match c {
                '"' => out.push_str("\\\""),
                '\\' => out.push_str("\\\\"),
                '\n' if self.one_line => out.push(' '),
                '\n' => out.push_str("\\n"),
                '\t' => out.push_str("\\t"),
                '\r' => out.push_str("\\r"),
                c => out.push(c),
            }
        }
        out.push('"');
    }
}

// ── format! templates ──────────────────────────────────────────────────────

/// One piece of a [`FormatTemplate`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TemplatePart {
    /// Literal text. Braces are doubled by the renderer.
    Lit(String),
    /// A substitution hole: `{}` positional, or `{name}` captured.
    Hole(Option<Ident>),
}

/// The template of a generated `format!` invocation.
///
/// Modelling holes separately from literal text is what stops a literal brace
/// in the output from being read as a hole — the classic failure of writing
/// `"{}={{}}"` to emit `name={}`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FormatTemplate(Vec<TemplatePart>);

impl FormatTemplate {
    #[must_use]
    pub fn new() -> Self {
        Self(Vec::new())
    }

    #[must_use]
    pub fn lit(mut self, text: impl Into<String>) -> Self {
        self.0.push(TemplatePart::Lit(text.into()));
        self
    }

    /// A positional hole, `{}`.
    #[must_use]
    pub fn hole(mut self) -> Self {
        self.0.push(TemplatePart::Hole(None));
        self
    }

    /// A captured hole, `{name}`.
    #[must_use]
    pub fn captured(mut self, name: Ident) -> Self {
        self.0.push(TemplatePart::Hole(Some(name)));
        self
    }

    /// The template text, with literal braces doubled.
    #[must_use]
    pub fn text(&self) -> String {
        let mut s = String::new();
        for part in &self.0 {
            match part {
                TemplatePart::Lit(t) => {
                    for c in t.chars() {
                        match c {
                            '{' => s.push_str("{{"),
                            '}' => s.push_str("}}"),
                            c => s.push(c),
                        }
                    }
                }
                TemplatePart::Hole(None) => s.push_str("{}"),
                TemplatePart::Hole(Some(id)) => {
                    s.push('{');
                    s.push_str(id.as_str());
                    s.push('}');
                }
            }
        }
        s
    }
}

// ── Types ──────────────────────────────────────────────────────────────────

/// A Rust type expression.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeExpr {
    /// A type carried by the IR, rendered through its `Display` impl.
    Ir(RustType),
    /// A named path type, e.g. `reqwest::Response`.
    Path(Path),
    /// `&T`.
    Ref(Box<TypeExpr>),
    /// `Name<A, B>`.
    App(Path, Vec<TypeExpr>),
    /// `dyn Trait`.
    Dyn(Path),
    /// `()`.
    Unit,
}

impl TypeExpr {
    /// Render this type on its own, e.g. `Option<String>`.
    #[must_use]
    pub fn to_rust(&self) -> String {
        let mut out = String::new();
        self.render(&mut out);
        out
    }

    /// `&str`.
    ///
    /// # Panics
    ///
    /// Never: `str` is a valid identifier.
    #[must_use]
    pub fn str_ref() -> Self {
        Self::Ref(Box::new(Self::Path(
            Path::new(&["str"]).expect("`str` is a valid identifier"),
        )))
    }

    fn render(&self, out: &mut String) {
        match self {
            Self::Ir(rt) => {
                let _ = write!(out, "{rt}");
            }
            Self::Path(p) => render_path(p, out),
            Self::Ref(inner) => {
                out.push('&');
                inner.render(out);
            }
            Self::App(p, args) => {
                render_path(p, out);
                out.push('<');
                for (i, a) in args.iter().enumerate() {
                    if i > 0 {
                        out.push_str(", ");
                    }
                    a.render(out);
                }
                out.push('>');
            }
            Self::Dyn(p) => {
                out.push_str("dyn ");
                render_path(p, out);
            }
            Self::Unit => out.push_str("()"),
        }
    }
}

fn render_path(p: &Path, out: &mut String) {
    for (i, seg) in p.0.iter().enumerate() {
        if i > 0 {
            out.push_str("::");
        }
        out.push_str(seg.as_str());
    }
}

// ── Expressions ────────────────────────────────────────────────────────────

/// One link in a wrapped method chain.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChainLink {
    /// `.name(args)`.
    Call(Ident, Vec<Expr>),
    /// `.name` — a field access, distinct from a nullary method call.
    Field(Ident),
    /// `.await`.
    Await,
    /// A link that contributes no text but still occupies a line.
    ///
    /// This exists to model an absent optional step — an unauthenticated
    /// client emits no auth call, but the blank line it leaves behind is part
    /// of the established output and is preserved deliberately.
    Absent,
}

/// A struct-literal field.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldInit {
    /// `name` — shorthand for `name: name`.
    Shorthand(Ident),
    /// `name: value`.
    Named(Ident, Expr),
    /// `..expr` — functional update, e.g. `..Default::default()`.
    Rest(Expr),
}

/// How a struct literal is laid out.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Braces {
    /// `X { a, b }`.
    Inline,
    /// One field per line.
    Multiline,
}

/// A Rust expression.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr {
    /// A path or variable reference.
    Path(Path),
    /// A string literal.
    Str(StrLit),
    /// An integer literal.
    Int(i64),
    /// A character literal. Escaped by the renderer, as strings are.
    Char(char),
    /// `true` / `false`.
    Bool(bool),
    /// `receiver.field`.
    Field(Box<Expr>, Ident),
    /// `&expr`.
    Ref(Box<Expr>),
    /// `&ref` binding-free shared reference to a `ref` pattern binding.
    Not(Box<Expr>),
    /// `expr?`.
    Try(Box<Expr>),
    /// `expr.await`.
    Await(Box<Expr>),
    /// `func(args)`.
    Call(Path, Vec<Expr>),
    /// `receiver.name(args)`, rendered inline.
    MethodCall(Box<Expr>, Ident, Vec<Expr>),
    /// A method chain broken across lines, one link per line.
    Chain { receiver: Box<Expr>, links: Vec<ChainLink> },
    /// `Path { fields }`.
    StructLit { path: Path, fields: Vec<FieldInit>, braces: Braces },
    /// `format!(template, args)`.
    Format(FormatTemplate, Vec<Expr>),
    /// `if cond { a } else { b }`, rendered inline.
    IfElseInline { cond: Box<Expr>, then: Box<Expr>, otherwise: Box<Expr> },
    /// `|param| body`.
    Closure(Ident, Box<Expr>),
    /// A call whose single argument sits on its own line, with a trailing
    /// comma, as the established output breaks it out.
    CallWrapped { func: Path, arg: Box<Expr> },
    /// `()` — the unit value.
    Unit,
    /// A turbofish member access: `Option::<&str>::None`.
    ///
    /// The type argument is a [`TypeExpr`], not text, so the turbofish cannot
    /// be spelled with something that is not a type.
    Turbofish { base: Path, ty: Box<TypeExpr>, member: Ident },
}

impl Expr {
    /// A bare variable reference.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidIdent`] if `name` is not a legal identifier.
    pub fn var(name: &str) -> Result<Self, InvalidIdent> {
        Path::new(&[name]).map(Self::Path)
    }

    /// A string literal from a raw value.
    #[must_use]
    pub fn string(value: impl Into<String>) -> Self {
        Self::Str(StrLit::new(value))
    }

    fn render(&self, out: &mut String, indent: usize) {
        match self {
            Self::Path(p) => render_path(p, out),
            Self::Str(s) => s.render(out),
            Self::Int(n) => {
                let _ = write!(out, "{n}");
            }
            Self::Char(c) => {
                out.push('\'');
                match c {
                    '\'' => out.push_str("\\'"),
                    '\\' => out.push_str("\\\\"),
                    '\n' => out.push_str("\\n"),
                    '\t' => out.push_str("\\t"),
                    c => out.push(*c),
                }
                out.push('\'');
            }
            Self::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
            Self::Field(recv, name) => {
                recv.render(out, indent);
                out.push('.');
                out.push_str(name.as_str());
            }
            Self::Ref(inner) => {
                out.push('&');
                inner.render(out, indent);
            }
            Self::Not(inner) => {
                out.push('!');
                inner.render(out, indent);
            }
            Self::Try(inner) => {
                inner.render(out, indent);
                out.push('?');
            }
            Self::Await(inner) => {
                inner.render(out, indent);
                out.push_str(".await");
            }
            Self::Call(p, args) => {
                render_path(p, out);
                render_args(args, out, indent);
            }
            Self::MethodCall(recv, name, args) => {
                recv.render(out, indent);
                out.push('.');
                out.push_str(name.as_str());
                render_args(args, out, indent);
            }
            Self::Chain { receiver, links } => {
                receiver.render(out, indent);
                for link in links {
                    newline(out, indent + 1);
                    match link {
                        ChainLink::Call(name, args) => {
                            out.push('.');
                            out.push_str(name.as_str());
                            render_args(args, out, indent + 1);
                        }
                        ChainLink::Field(name) => {
                            out.push('.');
                            out.push_str(name.as_str());
                        }
                        ChainLink::Await => out.push_str(".await"),
                        ChainLink::Absent => {}
                    }
                }
            }
            Self::StructLit { path, fields, braces } => {
                render_path(path, out);
                match braces {
                    Braces::Inline => {
                        out.push_str(" { ");
                        for (i, f) in fields.iter().enumerate() {
                            if i > 0 {
                                out.push_str(", ");
                            }
                            render_field_init(f, out, indent);
                        }
                        out.push_str(" }");
                    }
                    Braces::Multiline => {
                        out.push_str(" {");
                        for f in fields {
                            newline(out, indent + 1);
                            render_field_init(f, out, indent + 1);
                            if !matches!(f, FieldInit::Rest(_)) {
                                out.push(',');
                            }
                        }
                        newline(out, indent);
                        out.push('}');
                    }
                }
            }
            Self::Format(template, args) => {
                out.push_str("format!(");
                StrLit::new(template.text()).render(out);
                for a in args {
                    out.push_str(", ");
                    a.render(out, indent);
                }
                out.push(')');
            }
            Self::IfElseInline { cond, then, otherwise } => {
                out.push_str("if ");
                cond.render(out, indent);
                out.push_str(" { ");
                then.render(out, indent);
                out.push_str(" } else { ");
                otherwise.render(out, indent);
                out.push_str(" }");
            }
            Self::Closure(param, body) => {
                out.push('|');
                out.push_str(param.as_str());
                out.push_str("| ");
                body.render(out, indent);
            }
            Self::CallWrapped { func, arg } => {
                render_path(func, out);
                out.push('(');
                newline(out, indent + 1);
                arg.render(out, indent + 1);
                out.push(',');
                newline(out, indent);
                out.push(')');
            }
            Self::Unit => out.push_str("()"),
            Self::Turbofish { base, ty, member } => {
                render_path(base, out);
                out.push_str("::<");
                ty.render(out);
                out.push_str(">::");
                out.push_str(member.as_str());
            }
        }
    }
}

fn render_field_init(f: &FieldInit, out: &mut String, indent: usize) {
    match f {
        FieldInit::Shorthand(name) => out.push_str(name.as_str()),
        FieldInit::Named(name, value) => {
            out.push_str(name.as_str());
            out.push_str(": ");
            value.render(out, indent);
        }
        FieldInit::Rest(value) => {
            out.push_str("..");
            value.render(out, indent);
        }
    }
}

fn render_args(args: &[Expr], out: &mut String, indent: usize) {
    out.push('(');
    for (i, a) in args.iter().enumerate() {
        if i > 0 {
            out.push_str(", ");
        }
        a.render(out, indent);
    }
    out.push(')');
}

fn newline(out: &mut String, indent: usize) {
    out.push('\n');
    for _ in 0..indent {
        out.push_str("    ");
    }
}

// ── Statements ─────────────────────────────────────────────────────────────

/// One arm of a `match`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MatchArm {
    /// The pattern text, e.g. `Ok(result)`. Built as an [`Expr`] so patterns
    /// that are really call shapes stay typed.
    pub pattern: Expr,
    pub body: Expr,
}

/// A statement inside a [`Block`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Stmt {
    /// `let [mut] name[: Ty] = init;`
    Let { name: Ident, mutable: bool, ty: Option<TypeExpr>, init: Expr },
    /// `let name =` followed by the initialiser on the next line, indented.
    LetWrapped { name: Ident, init: Expr },
    /// `lhs = rhs;`
    Assign { lhs: Expr, rhs: Expr },
    /// `expr;`
    Semi(Expr),
    /// A trailing expression with no semicolon.
    Tail(Expr),
    /// `return expr;`
    Return(Expr),
    /// An empty line.
    Blank,
    /// `if cond { … }`
    If { cond: Expr, then: Block },
    /// `if let Some(ref name) = scrutinee { … }`
    IfLetSomeRef { name: Ident, scrutinee: Expr, then: Block },
    /// `match scrutinee { arms }`
    Match { scrutinee: Expr, arms: Vec<MatchArm> },
}

impl Stmt {
    fn render(&self, out: &mut String, indent: usize) {
        match self {
            Self::Blank => {}
            Self::Let { name, mutable, ty, init } => {
                out.push_str("let ");
                if *mutable {
                    out.push_str("mut ");
                }
                out.push_str(name.as_str());
                if let Some(t) = ty {
                    out.push_str(": ");
                    t.render(out);
                }
                out.push_str(" = ");
                init.render(out, indent);
                out.push(';');
            }
            Self::LetWrapped { name, init } => {
                out.push_str("let ");
                out.push_str(name.as_str());
                out.push_str(" =");
                newline(out, indent + 1);
                init.render(out, indent + 1);
                out.push(';');
            }
            Self::Assign { lhs, rhs } => {
                lhs.render(out, indent);
                out.push_str(" = ");
                rhs.render(out, indent);
                out.push(';');
            }
            Self::Semi(e) => {
                e.render(out, indent);
                out.push(';');
            }
            Self::Tail(e) => e.render(out, indent),
            Self::Return(e) => {
                out.push_str("return ");
                e.render(out, indent);
                out.push(';');
            }
            Self::If { cond, then } => {
                out.push_str("if ");
                cond.render(out, indent);
                out.push_str(" {");
                then.render(out, indent + 1);
                newline(out, indent);
                out.push('}');
            }
            Self::IfLetSomeRef { name, scrutinee, then } => {
                out.push_str("if let Some(ref ");
                out.push_str(name.as_str());
                out.push_str(") = ");
                scrutinee.render(out, indent);
                out.push_str(" {");
                then.render(out, indent + 1);
                newline(out, indent);
                out.push('}');
            }
            Self::Match { scrutinee, arms } => {
                out.push_str("match ");
                scrutinee.render(out, indent);
                out.push_str(" {");
                for arm in arms {
                    newline(out, indent + 1);
                    arm.pattern.render(out, indent + 1);
                    out.push_str(" => ");
                    arm.body.render(out, indent + 1);
                    out.push(',');
                }
                newline(out, indent);
                out.push('}');
            }
        }
    }
}

/// A brace-delimited sequence of statements.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Block(pub Vec<Stmt>);

impl Block {
    /// Render each statement on its own line at `indent`.
    ///
    /// The caller has already emitted the opening brace; this starts with a
    /// newline and does **not** emit the closing brace.
    fn render(&self, out: &mut String, indent: usize) {
        for stmt in &self.0 {
            if matches!(stmt, Stmt::Blank) {
                out.push('\n');
            } else {
                newline(out, indent);
                stmt.render(out, indent);
            }
        }
    }
}

// ── Items ──────────────────────────────────────────────────────────────────

/// A generic parameter with an optional single bound.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GenericParam {
    pub name: Ident,
    pub bound: Option<Path>,
}

/// A function parameter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Param {
    /// `&self`.
    SelfRef,
    /// `name: Ty`.
    Typed(Ident, TypeExpr),
    /// A destructuring parameter, e.g. `Parameters(input): Parameters<T>`.
    Destructured { pattern: Expr, ty: TypeExpr },
}

/// How a parameter list is laid out.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Params {
    /// All on the signature line.
    Inline,
    /// One per line, trailing comma.
    OnePerLine,
}

/// Documentation attached to an item, emitted verbatim after `/// `.
///
/// The text is **not** re-wrapped or escaped: a doc string containing a
/// newline produces a line that is no longer a comment. That is the
/// established behaviour of this generator and is preserved rather than
/// silently corrected; fixing it changes emitted bytes.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Doc(pub Vec<String>);

/// A function definition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnDef {
    pub doc: Doc,
    pub attrs: Vec<Attr>,
    pub public: bool,
    pub is_async: bool,
    pub name: Ident,
    pub generics: Vec<GenericParam>,
    pub params: Vec<Param>,
    pub params_layout: Params,
    pub ret: TypeExpr,
    pub body: Block,
}

/// An attribute, e.g. `#[derive(Debug, Clone)]` or `#[tool(description = "…")]`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Attr {
    /// `#[word]`, e.g. `#[tool_router]`.
    Word(Ident),
    /// `#[name(a, b, c)]`, e.g. `#[derive(Debug, Clone)]`.
    List(Ident, Vec<Path>),
    /// `#[name(key = "value")]`, e.g. `#[schemars(description = "…")]`.
    KeyValue { name: Path, key: Ident, value: StrLit },
}

impl Attr {
    fn render(&self, out: &mut String) {
        out.push_str("#[");
        match self {
            Self::Word(w) => out.push_str(w.as_str()),
            Self::List(name, items) => {
                out.push_str(name.as_str());
                out.push('(');
                for (i, it) in items.iter().enumerate() {
                    if i > 0 {
                        out.push_str(", ");
                    }
                    render_path(it, out);
                }
                out.push(')');
            }
            Self::KeyValue { name, key, value } => {
                render_path(name, out);
                out.push('(');
                out.push_str(key.as_str());
                out.push_str(" = ");
                value.render(out);
                out.push(')');
            }
        }
        out.push(']');
    }
}

/// A struct field.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldDecl {
    pub attrs: Vec<Attr>,
    pub public: bool,
    pub name: Ident,
    pub ty: TypeExpr,
}

/// A struct definition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructDef {
    pub doc: Doc,
    pub attrs: Vec<Attr>,
    pub public: bool,
    pub name: Ident,
    pub fields: Vec<FieldDecl>,
}

/// A member of an `impl` block.
///
/// Blank lines are explicit members rather than separators inserted between
/// items. Vertical spacing is part of the emitted bytes, so the caller states
/// it instead of the renderer guessing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImplItem {
    Fn(FnDef),
    /// A `//` comment line.
    Comment(String),
    /// An empty line.
    Blank,
}

/// An `impl` block.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImplBlock {
    pub attrs: Vec<Attr>,
    /// `Some(Trait)` for `impl Trait for Ty`.
    pub trait_path: Option<Path>,
    pub self_ty: Path,
    pub items: Vec<ImplItem>,
}

/// One entry in a `use` group.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UseTree {
    /// A plain path, e.g. `ServerHandler` or `transport::stdio`.
    Leaf(Path),
    /// A nested group, e.g. `handler::server::{a, b}`.
    Group(Path, Vec<UseTree>),
}

impl UseTree {
    fn render(&self, out: &mut String) {
        match self {
            Self::Leaf(p) => render_path(p, out),
            Self::Group(p, children) => {
                render_path(p, out);
                out.push_str("::{");
                for (i, c) in children.iter().enumerate() {
                    if i > 0 {
                        out.push_str(", ");
                    }
                    c.render(out);
                }
                out.push('}');
            }
        }
    }
}

/// A top-level item.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Item {
    /// `use path::{a, b};` — `leaves` empty means `use path;`.
    Use { path: Path, leaves: Vec<Path>, glob: bool },
    /// A `use` whose group is broken across lines, one `lines` entry per line.
    UseLines { path: Path, lines: Vec<Vec<UseTree>> },
    Struct(StructDef),
    Impl(ImplBlock),
    Fn(FnDef),
    /// A `//` comment line. Empty string emits a bare `//`.
    Comment(String),
    /// An empty line.
    Blank,
}

// ── Rendering ──────────────────────────────────────────────────────────────

fn render_doc(doc: &Doc, out: &mut String, indent: usize, first: &mut bool) {
    for line in &doc.0 {
        if *first {
            *first = false;
        } else {
            newline(out, indent);
        }
        if line.is_empty() {
            out.push_str("///");
        } else {
            out.push_str("/// ");
            out.push_str(line);
        }
    }
}

impl FnDef {
    fn render(&self, out: &mut String, indent: usize) {
        let mut first = true;
        render_doc(&self.doc, out, indent, &mut first);

        for attr in &self.attrs {
            if first {
                first = false;
            } else {
                newline(out, indent);
            }
            attr.render(out);
        }

        if !first {
            newline(out, indent);
        }
        if self.public {
            out.push_str("pub ");
        }
        if self.is_async {
            out.push_str("async ");
        }
        out.push_str("fn ");
        out.push_str(self.name.as_str());

        if !self.generics.is_empty() {
            out.push('<');
            for (i, g) in self.generics.iter().enumerate() {
                if i > 0 {
                    out.push_str(", ");
                }
                out.push_str(g.name.as_str());
                if let Some(b) = &g.bound {
                    out.push_str(": ");
                    render_path(b, out);
                }
            }
            out.push('>');
        }

        out.push('(');
        match self.params_layout {
            Params::Inline => {
                for (i, p) in self.params.iter().enumerate() {
                    if i > 0 {
                        out.push_str(", ");
                    }
                    render_param(p, out);
                }
            }
            Params::OnePerLine => {
                for p in &self.params {
                    newline(out, indent + 1);
                    render_param(p, out);
                    out.push(',');
                }
                newline(out, indent);
            }
        }
        out.push(')');

        if !matches!(self.ret, TypeExpr::Unit) {
            out.push_str(" -> ");
            self.ret.render(out);
        }

        out.push_str(" {");
        self.body.render(out, indent + 1);
        newline(out, indent);
        out.push('}');
    }
}

fn render_param(p: &Param, out: &mut String) {
    match p {
        Param::SelfRef => out.push_str("&self"),
        Param::Typed(name, ty) => {
            out.push_str(name.as_str());
            out.push_str(": ");
            ty.render(out);
        }
        Param::Destructured { pattern, ty } => {
            pattern.render(out, 0);
            out.push_str(": ");
            ty.render(out);
        }
    }
}

impl Item {
    fn render(&self, out: &mut String) {
        match self {
            Self::Blank => {}
            Self::Comment(text) => {
                if text.is_empty() {
                    out.push_str("//");
                } else {
                    out.push_str("// ");
                    out.push_str(text);
                }
            }
            Self::Use { path, leaves, glob } => {
                out.push_str("use ");
                render_path(path, out);
                if *glob {
                    out.push_str("::*");
                } else if !leaves.is_empty() {
                    out.push_str("::{");
                    for (i, l) in leaves.iter().enumerate() {
                        if i > 0 {
                            out.push_str(", ");
                        }
                        render_path(l, out);
                    }
                    out.push('}');
                }
                out.push(';');
            }
            Self::UseLines { path, lines } => {
                out.push_str("use ");
                render_path(path, out);
                out.push_str("::{");
                for line in lines {
                    newline(out, 1);
                    for (i, t) in line.iter().enumerate() {
                        if i > 0 {
                            out.push_str(", ");
                        }
                        t.render(out);
                    }
                    out.push(',');
                }
                newline(out, 0);
                out.push_str("};");
            }
            Self::Struct(s) => {
                let mut first = true;
                render_doc(&s.doc, out, 0, &mut first);
                for attr in &s.attrs {
                    if first {
                        first = false;
                    } else {
                        newline(out, 0);
                    }
                    attr.render(out);
                }
                if !first {
                    newline(out, 0);
                }
                if s.public {
                    out.push_str("pub ");
                }
                out.push_str("struct ");
                out.push_str(s.name.as_str());
                out.push_str(" {");
                for f in &s.fields {
                    for attr in &f.attrs {
                        newline(out, 1);
                        attr.render(out);
                    }
                    newline(out, 1);
                    if f.public {
                        out.push_str("pub ");
                    }
                    out.push_str(f.name.as_str());
                    out.push_str(": ");
                    f.ty.render(out);
                    out.push(',');
                }
                newline(out, 0);
                out.push('}');
            }
            Self::Impl(b) => {
                for attr in &b.attrs {
                    attr.render(out);
                    newline(out, 0);
                }
                out.push_str("impl ");
                if let Some(t) = &b.trait_path {
                    render_path(t, out);
                    out.push_str(" for ");
                }
                render_path(&b.self_ty, out);
                out.push_str(" {");
                for item in &b.items {
                    match item {
                        ImplItem::Blank => out.push('\n'),
                        ImplItem::Comment(text) => {
                            newline(out, 1);
                            out.push_str("// ");
                            out.push_str(text);
                        }
                        ImplItem::Fn(f) => {
                            newline(out, 1);
                            f.render(out, 1);
                        }
                    }
                }
                newline(out, 0);
                out.push('}');
            }
            Self::Fn(f) => f.render(out, 0),
        }
    }
}

/// Render a sequence of items as a source file.
///
/// Each item is followed by a newline; [`Item::Blank`] contributes an empty
/// line. The result always ends with a newline.
#[must_use]
pub fn render_file(items: &[Item]) -> String {
    let mut out = String::with_capacity(16384);
    for item in items {
        item.render(&mut out);
        out.push('\n');
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn id(s: &str) -> Ident {
        Ident::new(s).unwrap()
    }

    fn path(segs: &[&str]) -> Path {
        Path::new(segs).unwrap()
    }

    // -- Ident validation (the parse boundary) --

    #[test]
    fn accepts_ordinary_identifiers() {
        for good in ["x", "_x", "snake_case", "Pascal", "a1", "_", "T"] {
            assert!(Ident::new(good).is_ok(), "{good} should be a valid identifier");
        }
    }

    #[test]
    fn rejects_non_identifiers() {
        for bad in ["", "2bad", "foo bar", "a-b", "a.b", "a::b", "há", "x!"] {
            assert!(Ident::new(bad).is_err(), "{bad:?} should be rejected");
        }
    }

    #[test]
    fn rejection_explains_itself() {
        let err = Ident::new("foo bar").unwrap_err();
        assert!(err.to_string().contains("spaces"), "got: {err}");
    }

    #[test]
    fn empty_path_is_rejected() {
        assert!(Path::new(&[]).is_err());
    }

    // -- String literals: escaping is the renderer's job --

    fn rendered(lit: &StrLit) -> String {
        let mut s = String::new();
        lit.render(&mut s);
        s
    }

    #[test]
    fn escapes_quotes_and_backslashes() {
        assert_eq!(rendered(&StrLit::new(r#"a"b\c"#)), r#""a\"b\\c""#);
    }

    #[test]
    fn escapes_newline_by_default() {
        assert_eq!(rendered(&StrLit::new("a\nb")), r#""a\nb""#);
    }

    #[test]
    fn one_line_flattens_newline_to_space() {
        assert_eq!(rendered(&StrLit::one_line("a\nb")), r#""a b""#);
    }

    #[test]
    fn one_line_still_escapes_quotes() {
        assert_eq!(rendered(&StrLit::one_line(r#"say "hi"\ok"#)), r#""say \"hi\"\\ok""#);
    }

    // -- format! templates --

    #[test]
    fn positional_hole_renders_empty_braces() {
        assert_eq!(FormatTemplate::new().lit("limit=").hole().text(), "limit={}");
    }

    #[test]
    fn captured_hole_renders_its_name() {
        assert_eq!(
            FormatTemplate::new().lit("/items/").captured(id("item_id")).text(),
            "/items/{item_id}"
        );
    }

    #[test]
    fn literal_braces_are_doubled() {
        // The whole point: a literal brace in the output cannot open a hole.
        assert_eq!(FormatTemplate::new().lit("a{b}c").text(), "a{{b}}c");
    }

    #[test]
    fn literal_brace_survives_a_round_trip_through_a_hole() {
        let t = FormatTemplate::new().lit("{").hole().lit("}");
        assert_eq!(t.text(), "{{{}}}");
    }

    // -- TypeExpr conformance with the IR's own Display --
    //
    // The generator hands IR types straight to the emitter. If TypeExpr ever
    // rendered them differently the output would silently drift, so pin the
    // correspondence across every RustType variant.

    #[test]
    fn ir_types_render_exactly_as_the_ir_displays_them() {
        let variants = [
            RustType::String,
            RustType::I64,
            RustType::U64,
            RustType::F64,
            RustType::Bool,
            RustType::Value,
            RustType::Named("Pet".into()),
            RustType::Vec(Box::new(RustType::Named("Pet".into()))),
            RustType::Option(Box::new(RustType::String)),
            RustType::Option(Box::new(RustType::Vec(Box::new(RustType::I64)))),
        ];
        for rt in variants {
            let mut out = String::new();
            TypeExpr::Ir(rt.clone()).render(&mut out);
            assert_eq!(out, rt.to_string(), "TypeExpr drifted from RustType::Display");
        }
    }

    #[test]
    fn str_ref_renders() {
        let mut out = String::new();
        TypeExpr::str_ref().render(&mut out);
        assert_eq!(out, "&str");
    }

    // -- Expressions --

    fn expr_text(e: &Expr) -> String {
        let mut s = String::new();
        e.render(&mut s, 0);
        s
    }

    #[test]
    fn renders_a_method_call_chain_inline() {
        let e = Expr::Await(Box::new(Expr::MethodCall(
            Box::new(Expr::var("resp").unwrap()),
            id("text"),
            vec![],
        )));
        assert_eq!(expr_text(&e), "resp.text().await");
    }

    #[test]
    fn renders_a_wrapped_chain_one_link_per_line() {
        let e = Expr::Chain {
            receiver: Box::new(Expr::var("self").unwrap()),
            links: vec![
                ChainLink::Call(id("inner"), vec![]),
                ChainLink::Await,
            ],
        };
        assert_eq!(expr_text(&e), "self\n    .inner()\n    .await");
    }

    #[test]
    fn an_absent_chain_link_still_occupies_a_line() {
        let e = Expr::Chain {
            receiver: Box::new(Expr::var("x").unwrap()),
            links: vec![ChainLink::Absent, ChainLink::Call(id("send"), vec![])],
        };
        assert_eq!(expr_text(&e), "x\n    \n    .send()");
    }

    #[test]
    fn renders_format_with_args() {
        let e = Expr::Format(
            FormatTemplate::new().lit("limit=").hole(),
            vec![Expr::var("v").unwrap()],
        );
        assert_eq!(expr_text(&e), r#"format!("limit={}", v)"#);
    }

    #[test]
    fn renders_a_multiline_struct_literal() {
        let e = Expr::StructLit {
            path: path(&["Self"]),
            fields: vec![
                FieldInit::Shorthand(id("inner")),
                FieldInit::Named(id("n"), Expr::Bool(true)),
            ],
            braces: Braces::Multiline,
        };
        assert_eq!(expr_text(&e), "Self {\n    inner,\n    n: true,\n}");
    }

    #[test]
    fn renders_an_inline_struct_literal() {
        let e = Expr::StructLit {
            path: path(&["E", "Api"]),
            fields: vec![FieldInit::Shorthand(id("status"))],
            braces: Braces::Inline,
        };
        assert_eq!(expr_text(&e), "E::Api { status }");
    }

    #[test]
    fn renders_inline_if_else() {
        let e = Expr::IfElseInline {
            cond: Box::new(Expr::var("has_query").unwrap()),
            then: Box::new(Expr::string("&")),
            otherwise: Box::new(Expr::string("?")),
        };
        assert_eq!(expr_text(&e), r#"if has_query { "&" } else { "?" }"#);
    }

    #[test]
    fn renders_a_closure() {
        let e = Expr::Closure(
            id("e"),
            Box::new(Expr::MethodCall(
                Box::new(Expr::var("e").unwrap()),
                id("to_string"),
                vec![],
            )),
        );
        assert_eq!(expr_text(&e), "|e| e.to_string()");
    }

    // -- Items --

    #[test]
    fn renders_a_struct_with_attrs_and_doc() {
        let item = Item::Struct(StructDef {
            doc: Doc(vec!["A widget.".into()]),
            attrs: vec![Attr::List(id("derive"), vec![path(&["Debug"]), path(&["Clone"])])],
            public: true,
            name: id("Widget"),
            fields: vec![FieldDecl {
                attrs: vec![],
                public: false,
                name: id("inner"),
                ty: TypeExpr::Path(path(&["reqwest", "Client"])),
            }],
        });
        assert_eq!(
            render_file(&[item]),
            "/// A widget.\n\
             #[derive(Debug, Clone)]\n\
             pub struct Widget {\n\
             \x20   inner: reqwest::Client,\n\
             }\n"
        );
    }

    #[test]
    fn renders_a_use_with_leaves() {
        let item = Item::Use {
            path: path(&["crate", "error"]),
            leaves: vec![path(&["ApiError"]), path(&["Result"])],
            glob: false,
        };
        assert_eq!(render_file(&[item]), "use crate::error::{ApiError, Result};\n");
    }

    #[test]
    fn renders_a_glob_use() {
        let item = Item::Use {
            path: path(&["crate", "api", "types"]),
            leaves: vec![],
            glob: true,
        };
        assert_eq!(render_file(&[item]), "use crate::api::types::*;\n");
    }

    #[test]
    fn renders_an_async_fn_with_generics_and_one_param_per_line() {
        let f = FnDef {
            doc: Doc::default(),
            attrs: vec![],
            public: false,
            is_async: true,
            name: id("post"),
            generics: vec![GenericParam {
                name: id("T"),
                bound: Some(path(&["serde", "Serialize"])),
            }],
            params: vec![Param::SelfRef, Param::Typed(id("path"), TypeExpr::str_ref())],
            params_layout: Params::OnePerLine,
            ret: TypeExpr::App(path(&["Result"]), vec![TypeExpr::Path(path(&["T"]))]),
            body: Block(vec![Stmt::Tail(Expr::var("x").unwrap())]),
        };
        assert_eq!(
            render_file(&[Item::Fn(f)]),
            "async fn post<T: serde::Serialize>(\n\
             \x20   &self,\n\
             \x20   path: &str,\n\
             ) -> Result<T> {\n\
             \x20   x\n\
             }\n"
        );
    }

    #[test]
    fn a_unit_return_type_is_elided() {
        let f = FnDef {
            doc: Doc::default(),
            attrs: vec![],
            public: false,
            is_async: false,
            name: id("go"),
            generics: vec![],
            params: vec![],
            params_layout: Params::Inline,
            ret: TypeExpr::Unit,
            body: Block::default(),
        };
        assert_eq!(render_file(&[Item::Fn(f)]), "fn go() {\n}\n");
    }

    #[test]
    fn blank_statements_emit_an_empty_line() {
        let f = FnDef {
            doc: Doc::default(),
            attrs: vec![],
            public: false,
            is_async: false,
            name: id("go"),
            generics: vec![],
            params: vec![],
            params_layout: Params::Inline,
            ret: TypeExpr::Unit,
            body: Block(vec![
                Stmt::Semi(Expr::var("a").unwrap()),
                Stmt::Blank,
                Stmt::Tail(Expr::var("b").unwrap()),
            ]),
        };
        assert_eq!(render_file(&[Item::Fn(f)]), "fn go() {\n    a;\n\n    b\n}\n");
    }

    #[test]
    fn renders_a_trait_impl_with_an_attribute() {
        let b = ImplBlock {
            attrs: vec![Attr::Word(id("tool_handler"))],
            trait_path: Some(path(&["ServerHandler"])),
            self_ty: path(&["Mcp"]),
            items: vec![],
        };
        assert_eq!(
            render_file(&[Item::Impl(b)]),
            "#[tool_handler]\nimpl ServerHandler for Mcp {\n}\n"
        );
    }

    #[test]
    fn impl_spacing_is_stated_by_the_caller_not_inferred() {
        let f = |name: &str| {
            ImplItem::Fn(FnDef {
                doc: Doc::default(),
                attrs: vec![],
                public: false,
                is_async: false,
                name: id(name),
                generics: vec![],
                params: vec![],
                params_layout: Params::Inline,
                ret: TypeExpr::Unit,
                body: Block::default(),
            })
        };
        let b = ImplBlock {
            attrs: vec![],
            trait_path: None,
            self_ty: path(&["C"]),
            items: vec![
                f("a"),
                ImplItem::Blank,
                ImplItem::Comment("-- section --".into()),
                ImplItem::Blank,
                f("b"),
                ImplItem::Blank,
            ],
        };
        assert_eq!(
            render_file(&[Item::Impl(b)]),
            "impl C {\n\
             \x20   fn a() {\n\
             \x20   }\n\
             \n\
             \x20   // -- section --\n\
             \n\
             \x20   fn b() {\n\
             \x20   }\n\
             \n\
             }\n"
        );
    }

    #[test]
    fn renders_a_key_value_attribute_with_escaping() {
        let a = Attr::KeyValue {
            name: path(&["schemars"]),
            key: id("description"),
            value: StrLit::one_line("says \"hi\"\nbye"),
        };
        let mut out = String::new();
        a.render(&mut out);
        assert_eq!(out, r#"#[schemars(description = "says \"hi\" bye")]"#);
    }

    #[test]
    fn renders_match_arms() {
        let s = Stmt::Match {
            scrutinee: Expr::var("x").unwrap(),
            arms: vec![MatchArm {
                pattern: Expr::Call(path(&["Ok"]), vec![Expr::var("v").unwrap()]),
                body: Expr::var("v").unwrap(),
            }],
        };
        let mut out = String::new();
        s.render(&mut out, 0);
        assert_eq!(out, "match x {\n    Ok(v) => v,\n}");
    }

    #[test]
    fn renders_if_let_some_ref() {
        let s = Stmt::IfLetSomeRef {
            name: id("v"),
            scrutinee: Expr::var("cursor").unwrap(),
            then: Block(vec![Stmt::Assign {
                lhs: Expr::var("has_query").unwrap(),
                rhs: Expr::Bool(true),
            }]),
        };
        let mut out = String::new();
        s.render(&mut out, 0);
        assert_eq!(out, "if let Some(ref v) = cursor {\n    has_query = true;\n}");
    }
}