dropshot_endpoint 0.17.0

macro used by dropshot consumers for registering handlers
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
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
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
// Copyright 2024 Oxide Computer Company

//! Support for the `#[dropshot::api_description]` attribute macro.
//!
//! The server macro tends to follow the same structure as `endpoint.rs`, but is
//! overall quite a bit more complex than function-based macros. The main source
//! of complexity comes from having to parse the entire trait which consists of
//! many endpoints and unmanaged items.
//!
//! * Each endpoint item is parsed and validated separately, and it's unhelpful
//!   to just bail out on the first error -- hence we use a hierarchical error
//!   collection scheme.
//! * There are some overall constraints on the trait itself, such as not having
//!   generics or where clauses.
//! * Syntax errors within endpoint implementations are passed through as-is,
//!   through lazy parsing with `crate::syn_parsing::ItemTraitPartParsed`.
//! * If the trait is valid, a support module is also generated. To generate the
//!   functions inside this module, we need to mutate the `RequestContext` type
//!   in particular.
//!
//! Code that is common to both the server and endpoint macros lives in
//! `endpoint.rs`.

use std::collections::HashMap;

use heck::ToSnakeCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned, ToTokens};
use serde::Deserialize;
use serde_tokenstream::{
    from_tokenstream, from_tokenstream_spanned, ParseWrapper,
};
use syn::{parse_quote, parse_quote_spanned, spanned::Spanned, Error};

use crate::{
    channel::ChannelParams,
    doc::{string_to_doc_attrs, ExtractedDoc},
    endpoint::EndpointParams,
    error_store::{ErrorSink, ErrorStore},
    metadata::{
        ApiEndpointKind, ChannelMetadata, EndpointMetadata,
        ValidatedChannelMetadata, ValidatedEndpointMetadata,
    },
    params::RqctxKind,
    syn_parsing::{
        ItemTraitPartParsed, TraitItemFnForSignature, TraitItemPartParsed,
        UnparsedBlock,
    },
    util::{get_crate, MacroKind},
};

pub(crate) fn do_trait(
    attr: proc_macro2::TokenStream,
    item: proc_macro2::TokenStream,
) -> (proc_macro2::TokenStream, Vec<Error>) {
    let mut error_store = ErrorStore::new();
    let errors = error_store.sink();

    // Parse attributes. (Do this before parsing the trait since that's the
    // order they're in, in source code.)
    let api_metadata = match from_tokenstream::<ApiMetadata>(&attr) {
        Ok(m) => Some(m),
        Err(e) => {
            errors.push(e);
            None
        }
    };

    // Attempt to parse the trait.
    let item_trait = match syn::parse2::<ItemTraitPartParsed>(item) {
        Ok(item_trait) => Some(item_trait),
        Err(e) => {
            errors.push(e);
            None
        }
    };

    let output = match (api_metadata, item_trait.as_ref()) {
        (Some(api_metadata), Some(item_trait)) => {
            // The happy path.
            let parser = ApiParser::new(api_metadata, &item_trait, errors);
            parser.to_output()
        }
        (None, Some(item_trait)) => {
            // This is a case where we can do something useful. Don't try and
            // validate the input, but we can at least regenerate the same type
            // with endpoint and channel attributes stripped.
            let parser = ApiParser::invalid_no_metadata(&item_trait, &errors);
            parser.to_output()
        }
        (_, None) => {
            // Can't do anything here, just return errors.
            ApiOutput {
                output: quote! {},
                context: "Self::Context".to_string(),
                has_endpoint_param_errors: false,
                has_channel_param_errors: false,
            }
        }
    };

    let mut errors = error_store.into_inner();

    // If there are any errors, we also want to provide a usage message as an error.
    if output.has_endpoint_param_errors {
        let item_trait = item_trait
            .as_ref()
            .expect("has_endpoint_param_errors is true => item_fn is Some");
        errors.insert(
            0,
            Error::new_spanned(
                &item_trait.ident,
                crate::endpoint::usage_str(&output.context),
            ),
        );
    }
    if output.has_channel_param_errors {
        let item_trait = item_trait
            .as_ref()
            .expect("has_channel_param_errors is true => item_fn is Some");
        errors.insert(
            0,
            Error::new_spanned(
                &item_trait.ident,
                crate::channel::usage_str(&output.context),
            ),
        );
    }

    (output.output, errors)
}

#[derive(Deserialize, Debug)]
struct ApiMetadata {
    #[serde(default)]
    context: Option<String>,
    module: Option<String>,
    tag_config: Option<ApiTagConfig>,
    _dropshot_crate: Option<String>,
}

impl ApiMetadata {
    /// The default name for the associated context type: `Self::Context`.
    const DEFAULT_CONTEXT_NAME: &'static str = "Context";

    /// Returns the dropshot crate value as a TokenStream.
    fn dropshot_crate(&self) -> TokenStream {
        get_crate(self._dropshot_crate.as_deref())
    }

    fn context_name(&self) -> &str {
        self.context.as_deref().unwrap_or(Self::DEFAULT_CONTEXT_NAME)
    }

    fn module_name(&self, trait_ident: &syn::Ident) -> String {
        self.module
            .clone()
            .unwrap_or_else(|| trait_ident.to_string().to_snake_case() + "_mod")
    }
}

/// A mirror of dropshot's `TagConfig`, used as part of arguments to the
/// top-level `api_description` macro.
#[derive(Deserialize, Debug)]
struct ApiTagConfig {
    #[serde(default)]
    allow_other_tags: bool,
    // This is an expression of type `dropshot::EndpointTagPolicy`. If not
    // specified, we'll substitute the default value `EndpointTagPolicy::Any`,
    // using the path to the dropshot crate determined by the macro invocation.
    #[serde(default)]
    policy: Option<ParseWrapper<syn::Expr>>,
    // tags is required
    tags: HashMap<String, ApiTagDetails>,
}

/// A mirror of dropshot's `TagDetails`, used as part of arguments to the
/// top-level `api_description` macro.
#[derive(Deserialize, Debug)]
struct ApiTagDetails {
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    external_docs: Option<ApiTagExternalDocs>,
}

/// A mirror of dropshot's `TagExternalDocs`, used as part of arguments to the
/// top-level `api_description` macro.
#[derive(Deserialize, Debug)]
struct ApiTagExternalDocs {
    #[serde(default)]
    description: Option<String>,
    url: String,
}

struct ApiParser<'ast> {
    dropshot: TokenStream,
    item_trait: ApiItemTrait<'ast>,
    // We want to maintain the order of items in the trait (other than the
    // Context associated type which we're always going to move to the top), so
    // we use a single list to store all of them.
    items: Vec<ApiItem<'ast>>,
    tag_config: Option<ApiTagConfig>,

    context_item: ContextItem<'ast>,

    // None indicates invalid metadata.
    module_ident: Option<syn::Ident>,
}

const ENDPOINT_IDENT: &str = "endpoint";
const CHANNEL_IDENT: &str = "channel";

impl<'ast> ApiParser<'ast> {
    fn new(
        metadata: ApiMetadata,
        item_trait: &'ast ItemTraitPartParsed,
        errors: ErrorSink<'_, Error>,
    ) -> Self {
        let dropshot = metadata.dropshot_crate();
        let mut items = Vec::with_capacity(item_trait.items.len());

        // First, validate the top-level properties of the trait itself.
        let trait_ident = &item_trait.ident;
        let item_trait = ApiItemTrait::new(item_trait, &errors);

        let context_name = metadata.context_name();

        // Do a first pass to look for the context item. We could forgo this
        // pass and always generate a context ident from the API metadata, but
        // that would lose valuable span information.
        let mut context_item = None;
        for item in &item_trait.item.items {
            if let TraitItemPartParsed::Other(syn::TraitItem::Type(ty)) = item {
                if ty.ident == context_name {
                    // This is the context item.
                    context_item = Some(ContextItem::new(ty, &errors));
                    break;
                }
            }
        }

        let context_item = if let Some(context_item) = context_item {
            context_item
        } else {
            ContextItem::new_missing(context_name, trait_ident, &errors)
        };

        let module_ident =
            format_ident!("{}", metadata.module_name(trait_ident));

        for item in &item_trait.item.items {
            match item {
                // Functions need to be parsed to see if they are endpoints or
                // channels.
                TraitItemPartParsed::Fn(f) => {
                    items.push(ApiItem::Fn(ApiFnItem::new(
                        &dropshot,
                        f,
                        trait_ident,
                        context_item.ident(),
                        &errors,
                    )));
                }

                // Everything else is permissible -- just ensure that they
                // aren't marked as `endpoint` or `channel`.
                TraitItemPartParsed::Other(other) => {
                    let should_push = match other {
                        syn::TraitItem::Const(c) => {
                            check_endpoint_or_channel_on_non_fn(
                                "const",
                                &c.ident.to_string(),
                                &c.attrs,
                                &errors,
                            );
                            true
                        }
                        syn::TraitItem::Fn(_) => {
                            unreachable!(
                                "function items should have been handled above"
                            )
                        }
                        syn::TraitItem::Type(t) => {
                            check_endpoint_or_channel_on_non_fn(
                                "type",
                                &t.ident.to_string(),
                                &t.attrs,
                                &errors,
                            );

                            // We'll handle the context type separately.
                            t.ident != context_name
                        }
                        syn::TraitItem::Macro(m) => {
                            check_endpoint_or_channel_on_non_fn(
                                "macro",
                                &m.mac.path.to_token_stream().to_string(),
                                &m.attrs,
                                &errors,
                            );
                            true
                        }
                        _ => true,
                    };

                    if should_push {
                        items.push(ApiItem::Other(item));
                    }
                }
            }
        }

        Self {
            dropshot,
            item_trait,
            items,
            tag_config: metadata.tag_config,
            context_item,
            module_ident: Some(module_ident),
        }
    }

    /// Creates an `ApiParser` for invalid metadata.
    ///
    /// In this case, no further checking is done on the items, and they are all
    /// stored as `Other`. The trait will be output as-is, with `#[endpoint]`
    /// and `#[channel]` attributes stripped.
    fn invalid_no_metadata(
        item_trait: &'ast ItemTraitPartParsed,
        errors: &ErrorSink<'_, Error>,
    ) -> Self {
        // Validate top-level properties of the trait itself.
        let item_trait = ApiItemTrait::new(item_trait, errors);

        // Just store all the items as "Other", to indicate that we haven't
        // performed any validation on them.
        let items = item_trait.item.items.iter().map(ApiItem::Other);

        Self {
            // The "dropshot" token is not available, so the best we can do is
            // to use the default "dropshot".
            dropshot: get_crate(None),
            item_trait,
            items: items.collect(),
            tag_config: None,
            context_item: ContextItem::new_invalid_metadata(),
            module_ident: None,
        }
    }

    fn to_output(&self) -> ApiOutput {
        let context = format!("Self::{}", self.context_item.ident());
        let context_item =
            self.make_context_trait_item().map(TraitItemPartParsed::Other);
        let other_items =
            self.items.iter().map(|item| item.to_out_trait_item());
        let out_items = context_item.into_iter().chain(other_items);

        // Output the trait whether or not it is valid.
        let item_trait = self.item_trait.item;

        // We need a 'static bound on the trait itself, otherwise we get `T
        // doesn't live long enough` errors.
        let mut supertraits = item_trait.supertraits.clone();
        supertraits.push(parse_quote!('static));

        // Also add dead_code if the visibility is not `pub`. (If it is `pub`,
        // then it is expected to be exported, and so the dead code warning
        // won't fire.)
        //
        // Why check for non-`pub` visibility? Because there is a downside to
        // allow(dead_code): it also applies to any items defined within the
        // trait. For example, if a provided method on the trait defines an
        // unused function inside of it, then allow(dead_code) would suppress
        // that.
        //
        // It would be ideal if there were a way to say "allow(dead_code), but
        // don't propagate this to child items", but sadly there isn't as of
        // Rust 1.81.
        let mut attrs = item_trait.attrs.clone();
        if !matches!(item_trait.vis, syn::Visibility::Public(_)) {
            attrs.push(parse_quote!(#[allow(dead_code)]));
        }

        // Everything else about the trait stays the same -- just the items change.
        let out_trait = ItemTraitPartParsed {
            attrs,
            supertraits,
            items: out_items.collect(),
            ..item_trait.clone()
        };

        // Also generate the support module.
        let module = self.make_module();

        let output = quote! {
            #out_trait

            #module
        };

        // Dig through the items to see if any of them have parameter errors.
        let has_endpoint_param_errors =
            self.items.iter().any(|item| match item {
                ApiItem::Fn(ApiFnItem::Invalid {
                    kind: InvalidApiItemKind::Endpoint(summary),
                    ..
                }) => summary.has_param_errors,
                _ => false,
            });
        let has_channel_param_errors =
            self.items.iter().any(|item| match item {
                ApiItem::Fn(ApiFnItem::Invalid {
                    kind: InvalidApiItemKind::Channel(summary),
                    ..
                }) => summary.has_param_errors,
                _ => false,
            });

        ApiOutput {
            output,
            context,
            has_endpoint_param_errors,
            has_channel_param_errors,
        }
    }

    fn make_context_trait_item(&self) -> Option<syn::TraitItem> {
        let dropshot = &self.dropshot;
        // In this context, invalid items should be passed through as-is.
        let item = self.context_item.original_item()?;
        let mut bounds = item.bounds.clone();
        // Generate these bounds for the associated type. We could require that
        // users specify them and error out if they don't, but this is much
        // easier to do, and also produces better errors.
        bounds.push(parse_quote!(#dropshot::ServerContext));

        let out_item = syn::TraitItemType { bounds, ..item.clone() };
        Some(syn::TraitItem::Type(out_item))
    }

    /// Generate the support module corresponding to the trait, with ways to
    /// make real servers and stub API descriptions.
    fn make_module(&self) -> TokenStream {
        let item_trait = self.item_trait.valid_item();
        let context_item = self.context_item.valid_item();
        let module_ident = self.module_ident.as_ref();

        match (item_trait, context_item, module_ident) {
            (Some(item_trait), Some(context_item), Some(module_ident)) => {
                let module_gen = SupportModuleGenerator {
                    dropshot: &self.dropshot,
                    module_ident,
                    item_trait,
                    context_item,
                    items: &self.items,
                    tag_config: self.tag_config.as_ref(),
                };
                module_gen.to_token_stream()
            }
            (_, _, Some(module_ident)) => {
                // If the module ident is known but one of the other parts is
                // invalid, generate an empty support module. (We can't generate
                // the API description functions, even ones that immediately
                // panic, because those depend on the trait and context items
                // being valid.)
                let doc = ModuleDocComments::generate_invalid(
                    &self.item_trait.item.ident,
                );
                let outer = doc.outer();
                let vis = &self.item_trait.item.vis;

                quote! {
                    #outer
                    #vis mod #module_ident {}
                }
            }
            _ => {
                // Can't do anything if the module name is missing.
                quote! {}
            }
        }
    }
}

/// The result of calling [`ApiParser::to_output`].
struct ApiOutput {
    /// The actual output.
    output: TokenStream,

    /// The context type (typically `Self::Context`), provided as a string.
    context: String,

    /// Whether there were any endpoint parameter-related errors.
    ///
    /// If there were, then we provide a usage message.
    has_endpoint_param_errors: bool,

    /// Whether there were any channel parameter-related errors.
    ///
    /// If there were, then we provide a usage message.
    has_channel_param_errors: bool,
}

#[derive(Clone, Copy)]
struct ApiItemTrait<'ast> {
    item: &'ast ItemTraitPartParsed,
    is_valid: bool,
}

impl<'ast> ApiItemTrait<'ast> {
    fn new(
        item: &'ast ItemTraitPartParsed,
        errors: &ErrorSink<'_, Error>,
    ) -> Self {
        let trait_ident = &item.ident;
        let errors = errors.new();

        if item.unsafety.is_some() {
            errors.push(Error::new_spanned(
                &item.unsafety,
                format!(
                    "API trait `{trait_ident}` must not be marked as `unsafe`"
                ),
            ));
        }

        if item.auto_token.is_some() {
            errors.push(Error::new_spanned(
                &item.auto_token,
                format!("API trait `{trait_ident}` must not be an auto trait"),
            ));
        }

        if !item.generics.params.is_empty() {
            errors.push(Error::new_spanned(
                &item.generics,
                format!("API trait `{trait_ident}` must not have generics"),
            ));
        }

        if let Some(where_clause) = &item.generics.where_clause {
            // Empty where clauses are no-ops and therefore permitted.
            if !where_clause.predicates.is_empty() {
                errors.push(Error::new_spanned(
                    where_clause,
                    format!(
                        "API trait `{trait_ident}` must not have a where clause"
                    ),
                ));
            }
        }

        Self { item, is_valid: !errors.has_errors() }
    }

    fn valid_item(&self) -> Option<&'ast ItemTraitPartParsed> {
        self.is_valid.then_some(self.item)
    }
}

/// The context item as present within an API trait (or not).
#[derive(Clone, Debug)]
enum ContextItem<'ast> {
    /// The item is valid.
    Valid(&'ast syn::TraitItemType),

    /// The item is invalid but present.
    Invalid(&'ast syn::TraitItemType),

    /// The item is missing.
    Missing {
        /// A conjured-up ident for the missing item.
        ident: syn::Ident,
    },
}

impl<'ast> ContextItem<'ast> {
    fn new(
        ty: &'ast syn::TraitItemType,
        errors: &ErrorSink<'_, Error>,
    ) -> Self {
        let errors = errors.new();

        // The context type must not have generics.
        if !ty.generics.params.is_empty() {
            errors.push(Error::new_spanned(
                &ty.generics,
                format!("context type `{}` must not have generics", ty.ident),
            ));
        }

        // Don't return the type if there were errors.
        if errors.has_errors() {
            Self::Invalid(ty)
        } else {
            Self::Valid(ty)
        }
    }

    fn new_missing(
        context_name: &str,
        trait_ident: &syn::Ident,
        errors: &ErrorSink<'_, Error>,
    ) -> Self {
        errors.push(Error::new_spanned(
            &trait_ident,
            format!(
                "API trait `{trait_ident}` does not have associated type \
                `{context_name}`\n\
                 (this type specifies the shared context for endpoints)",
            ),
        ));

        Self::Missing { ident: format_ident!("{context_name}") }
    }

    fn new_invalid_metadata() -> Self {
        Self::Missing {
            ident: format_ident!("{}", ApiMetadata::DEFAULT_CONTEXT_NAME),
        }
    }

    fn ident(&self) -> &syn::Ident {
        match self {
            Self::Valid(ty) => &ty.ident,
            Self::Invalid(ty) => &ty.ident,
            Self::Missing { ident } => ident,
        }
    }

    /// Return the original item, or None if it is missing.
    fn original_item(&self) -> Option<&'ast syn::TraitItemType> {
        match self {
            Self::Valid(ty) | Self::Invalid(ty) => Some(ty),
            Self::Missing { .. } => None,
        }
    }

    /// Return a valid item, or None if the item is invalid or missing.
    fn valid_item(&self) -> Option<&'ast syn::TraitItemType> {
        match self {
            Self::Valid(ty) => Some(ty),
            Self::Invalid(_) | Self::Missing { .. } => None,
        }
    }
}

/// Code that creates the support module for an API trait.
///
/// The support module should only be created in cases where the overall
/// structure of the trait is valid. (If it isn't, then that leads to some very
/// bad error messages). This serves as a statically typed guarantee regarding
/// that.
struct SupportModuleGenerator<'ast> {
    dropshot: &'ast TokenStream,
    module_ident: &'ast syn::Ident,

    // Invariant: the corresponding `ApiItemTrait::is_valid` is true.
    item_trait: &'ast ItemTraitPartParsed,

    // Invariant: the corresponding `ContextItem::is_valid` is true.
    context_item: &'ast syn::TraitItemType,

    // These items might or might not be valid individually.
    items: &'ast [ApiItem<'ast>],

    tag_config: Option<&'ast ApiTagConfig>,
}

impl SupportModuleGenerator<'_> {
    fn make_api_description(&self, doc: TokenStream) -> TokenStream {
        let dropshot = &self.dropshot;
        let trait_ident = &self.item_trait.ident;
        let context_ident = &self.context_item.ident;
        // Note we always use "pub" visibility for the items inside, since it
        // can be tricky to compute the exact visibility required here. The item
        // won't actually be exported unless the parent module is public, or it
        // is re-exported.

        let body = self.make_api_factory_body(FactoryKind::Regular);

        quote! {
            #doc
            #[automatically_derived]
            pub fn api_description<ServerImpl: #trait_ident>() -> ::std::result::Result<
                #dropshot::ApiDescription<<ServerImpl as #trait_ident>::#context_ident>,
                #dropshot::ApiDescriptionBuildErrors,
            > {
                #body
            }
        }
    }

    fn make_stub_api_description(&self, doc: TokenStream) -> TokenStream {
        let dropshot = &self.dropshot;
        // Note we always use "pub" visibility for the items inside. See the
        // comment in `make_api_description`.

        let body = self.make_api_factory_body(FactoryKind::Stub);

        quote! {
            #doc
            #[automatically_derived]
            pub fn stub_api_description() -> ::std::result::Result<
                #dropshot::ApiDescription<#dropshot::StubContext>,
                #dropshot::ApiDescriptionBuildErrors,
            > {
                #body
            }
        }
    }

    /// Generates the body for the API factory function, as well as the stub
    /// generator function.
    ///
    /// This code is shared across the real and stub API description functions.
    fn make_api_factory_body(&self, kind: FactoryKind) -> TokenStream {
        let dropshot = &self.dropshot;
        let trait_ident = &self.item_trait.ident;
        let trait_ident_str = trait_ident.to_string();

        if self.has_invalid_fn_items() {
            let err_msg = format!(
                "invalid endpoints encountered while parsing API trait `{}`",
                trait_ident_str
            );
            quote! {
                panic!(#err_msg);
            }
        } else {
            let tag_config = self.make_tag_config();
            let endpoints = self.items.iter().filter_map(|item| match item {
                ApiItem::Fn(ApiFnItem::Endpoint(e)) => {
                    Some(e.to_api_endpoint(&self.dropshot, kind))
                }

                ApiItem::Fn(ApiFnItem::Channel(c)) => {
                    Some(c.to_api_endpoint(&self.dropshot, kind))
                }

                ApiItem::Fn(ApiFnItem::Invalid { .. })
                | ApiItem::Fn(ApiFnItem::Unmanaged(_))
                | ApiItem::Other(_) => None,
            });

            quote! {
                let mut dropshot_api = #dropshot::ApiDescription::new()#tag_config;
                let mut dropshot_errors: Vec<#dropshot::ApiDescriptionRegisterError> = Vec::new();

                #(#endpoints)*

                if !dropshot_errors.is_empty() {
                    Err(#dropshot::ApiDescriptionBuildErrors::new(dropshot_errors))
                } else {
                    Ok(dropshot_api)
                }
            }
        }
    }

    fn make_tag_config(&self) -> Option<TokenStream> {
        let dropshot = self.dropshot;
        let tag_config = self.tag_config.as_ref()?;

        let allow_other_tags = tag_config.allow_other_tags;
        let policy = tag_config.policy.as_ref().map_or_else(
            || {
                quote! { #dropshot::EndpointTagPolicy::Any }
            },
            |wrapper| wrapper.to_token_stream(),
        );
        let tags = tag_config.tags.iter().map(|(tag, details)| {
            let description =
                quote_project_option(details.description.as_deref());
            let external_docs = details.external_docs.as_ref().map(|ed| {
                let description =
                    quote_project_option(ed.description.as_deref());
                let url = &ed.url;
                quote! {
                    #dropshot::TagExternalDocs {
                        description: #description,
                        url: #url.to_string(),
                    }
                }
            });
            let external_docs = quote_project_option(external_docs);

            quote! {
                tags.insert(
                    #tag.to_string(),
                    #dropshot::TagDetails {
                        description: #description,
                        external_docs: #external_docs,
                    }
                );
            }
        });
        Some(quote! {
            .tag_config({
                let mut tags = ::std::collections::HashMap::new();
                #(#tags)*

                #dropshot::TagConfig {
                    allow_other_tags: #allow_other_tags,
                    policy: #policy,
                    tags,
                }
            })
        })
    }

    fn make_doc_comments(&self) -> ModuleDocComments {
        if self.has_invalid_fn_items() {
            ModuleDocComments::generate_invalid(&self.item_trait.ident)
        } else {
            ModuleDocComments::generate(
                self.dropshot,
                &self.item_trait.ident,
                &self.context_item.ident,
                self.module_ident,
            )
        }
    }

    fn make_type_checks(&self) -> impl Iterator<Item = TokenStream> + '_ {
        self.items.iter().filter_map(|item| match item {
            ApiItem::Fn(ApiFnItem::Endpoint(_)) => {
                // We don't need to generate type checks the way we do with
                // function-based macros, because we get error messages that are
                // roughly as good through the stub API description generator.
                // (Also, adding type checks would end up duplicating a ton of
                // error messages.)
                None
            }
            ApiItem::Fn(ApiFnItem::Channel(c)) => {
                // Since we use an adapter function, the stub function doesn't
                // quite capture all the type checks desired. We do need to
                // generate a subset of the typechecks.
                Some(c.params.to_trait_type_checks())
            }
            _ => None,
        })
    }

    fn has_invalid_fn_items(&self) -> bool {
        self.items.iter().any(|item| item.is_invalid())
    }
}

impl ToTokens for SupportModuleGenerator<'_> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let vis = &self.item_trait.vis;
        let module_ident = self.module_ident;

        let doc_comments = self.make_doc_comments();

        // Generate two API description functions: one for the real trait, and
        // one for a stub impl meant for OpenAPI generation.
        let api = self.make_api_description(doc_comments.api_description());
        let stub_api =
            self.make_stub_api_description(doc_comments.stub_api_description());

        let type_checks = self.make_type_checks();

        let outer = doc_comments.outer();

        tokens.extend(quote! {
            #outer
            #[automatically_derived]
            #vis mod #module_ident {
                // super::* pulls in definitions from the surrounding scope.
                // This is not ideal because it means that the macro can only be
                // applied to traits defined in modules, not in functions.
                //
                // A much better approach would be to be able to annotate the
                // module and say "don't create a new scope", similar to
                // `#[transparent]` as proposed in
                // https://github.com/rust-lang/rust/issues/79260.
                //
                // There does not appear to be a workaround for this, short of
                // not using a module at all. There are two other options:
                //
                // 1. Put the functions below into the parent scope. This adds a
                //    bunch of items to the scope rather than one, which seems
                //    worse on balance.
                // 2. Make these methods on a type rather than free functions in
                //    a module. This approach works for functions, but not other
                //    items like macros we may wish to define in the future.
                //
                // In RFD 479, we determined that on balance, the current
                // approach has the fewest downsides.
                use super::*;

                #(#type_checks)*

                // We don't need to generate type checks the way we do with
                // function-based macros, because we get error messages that are
                // roughly as good through the stub API description generator.
                // (Also, adding type checks would end up duplicating a ton of
                // error messages.)
                //
                // For that reason, put it above the real API description --
                // that way, the best error messages appear first.
                #stub_api

                #api
            }
        });
    }
}

/// Turn Some<T> into quote! { Some(T.into()) }, and None into quote! { None }.
///
/// The `.into()` assists with string conversions.
fn quote_project_option<T: ToTokens>(t: Option<T>) -> TokenStream {
    t.map_or_else(|| quote! { None }, |t| quote! { Some(#t.into()) })
}

/// Generated documentation comments for the support module.
struct ModuleDocComments {
    /// Doc comment for the module type.
    outer: String,

    /// Doc comment for the API description factory.
    api_description: String,

    /// Doc comment for the stub API description function.
    stub_api_description: String,
}

impl ModuleDocComments {
    fn generate(
        dropshot: &TokenStream,
        trait_ident: &syn::Ident,
        context_ident: &syn::Ident,
        module_ident: &syn::Ident,
    ) -> ModuleDocComments {
        let outer = format!(
            "Support module for the Dropshot API trait \
            [`{trait_ident}`]({trait_ident}).",
        );

        let api_description = format!(
"Given an implementation of [`{trait_ident}`], generate an API description.

This function accepts a single type argument `ServerImpl`, turning it into a
Dropshot [`ApiDescription`]`<ServerImpl::`[`{context_ident}`]`>`. 
The returned `ApiDescription` can then be turned into a Dropshot server that
accepts a concrete `{context_ident}`.

## Example

```rust,ignore
/// A type used to define the concrete implementation for `{trait_ident}`.
/// 
/// This type is never constructed -- it is just a place to define your
/// implementation of `{trait_ident}`.
enum {trait_ident}Impl {{}}

impl {trait_ident} for {trait_ident}Impl {{
    type {context_ident} = /* context type */;

    // ... trait methods
}}

#[tokio::main]
async fn main() {{
    // Generate the description for `{trait_ident}Impl`.
    let description = {module_ident}::api_description::<{trait_ident}Impl>().unwrap();

    // Create a value of the concrete context type.
    let context = /* some value of type `{trait_ident}Impl::{context_ident}` */;

    // Create a Dropshot server from the description.
    let log = /* ... */;
    let server = dropshot::ServerBuilder::new(description, context, log)
        .start()
        .unwrap();

    // Run the server.
    server.await
}}
```

[`ApiDescription`]: {dropshot}::ApiDescription
[`{trait_ident}`]: {trait_ident}
[`{context_ident}`]: {trait_ident}::{context_ident}
",
        );

        let stub_api_description = format!(
"Generate a _stub_ API description for [`{trait_ident}`], meant for OpenAPI
generation.

Unlike [`api_description`], this function does not require an implementation
of [`{trait_ident}`] to be available, instead generating handlers that panic.
The return value is of type [`ApiDescription`]`<`[`StubContext`]`>`.

The main use of this function is in cases where [`{trait_ident}`] is defined
in a separate crate from its implementation. The OpenAPI spec can then be
generated directly from the stub API description.

## Example

A function that prints the OpenAPI spec to standard output:

```rust,ignore
fn print_openapi_spec() {{
    let stub = {module_ident}::stub_api_description().unwrap();

    // Generate OpenAPI spec from `stub`.
    let spec = stub.openapi(\"{trait_ident}\", \"0.1.0\");
    spec.write(&mut std::io::stdout()).unwrap();
}}
```

[`{trait_ident}`]: {trait_ident}
[`api_description`]: {module_ident}::api_description
[`ApiDescription`]: {dropshot}::ApiDescription
[`StubContext`]: {dropshot}::StubContext
");

        ModuleDocComments { outer, api_description, stub_api_description }
    }

    fn generate_invalid(trait_ident: &syn::Ident) -> Self {
        let outer = format!(
"**Invalid**: Support module for the Dropshot API trait `{trait_ident}`.

Errors were encountered while parsing the API.
");

        let api_description = format!(
"**Invalid, panics:** Given an implementation of `{trait_ident}`, generate an API description.

Errors were encountered while parsing the API, so this function panics.
");

        let stub_api_description = format!(
"**Invalid, panics:** Generate a _stub_ API description for `{trait_ident}`, meant for OpenAPI
generation.

Errors were encountered while parsing the API, so this function panics.
");

        ModuleDocComments { outer, api_description, stub_api_description }
    }

    fn outer(&self) -> TokenStream {
        string_to_doc_attrs(&self.outer)
    }

    fn api_description(&self) -> TokenStream {
        string_to_doc_attrs(&self.api_description)
    }

    fn stub_api_description(&self) -> TokenStream {
        string_to_doc_attrs(&self.stub_api_description)
    }
}

#[derive(Clone, Copy, Debug)]
enum FactoryKind {
    Regular,
    Stub,
}

fn check_endpoint_or_channel_on_non_fn(
    kind: &str,
    name: &str,
    attrs: &[syn::Attribute],
    errors: &ErrorSink<'_, Error>,
) {
    if let Some(attr) = attrs.iter().find(|a| a.path().is_ident(ENDPOINT_IDENT))
    {
        errors.push(Error::new_spanned(
            attr,
            format!("{kind} `{name}` marked as endpoint is not a method"),
        ));
    }

    if let Some(attr) = attrs.iter().find(|a| a.path().is_ident(CHANNEL_IDENT))
    {
        errors.push(Error::new_spanned(
            attr,
            format!("{kind} `{name}` marked as channel is not a method"),
        ));
    }
}

#[allow(clippy::large_enum_variant)]
enum ApiItem<'ast> {
    Fn(ApiFnItem<'ast>),
    Other(&'ast TraitItemPartParsed),
}

impl ApiItem<'_> {
    fn is_invalid(&self) -> bool {
        matches!(self, Self::Fn(ApiFnItem::Invalid { .. }))
    }

    fn to_out_trait_item(&self) -> TraitItemPartParsed {
        match self {
            Self::Fn(f) => TraitItemPartParsed::Fn(f.to_out_trait_item()),
            Self::Other(o) => {
                // Strip recognized attributes, retaining all others.
                o.clone_and_strip_recognized_attrs()
            }
        }
    }
}

#[allow(clippy::large_enum_variant)]
enum ApiFnItem<'ast> {
    Endpoint(ApiEndpoint<'ast>),
    Channel(ApiChannel<'ast>),
    // An item managed by the macro that was somehow invalid.
    Invalid { f: &'ast TraitItemFnForSignature, kind: InvalidApiItemKind },
    // A item without an endpoint or channel attribute.
    Unmanaged(&'ast TraitItemFnForSignature),
}

impl<'ast> ApiFnItem<'ast> {
    fn new(
        dropshot: &TokenStream,
        f: &'ast TraitItemFnForSignature,
        trait_ident: &'ast syn::Ident,
        context_ident: &syn::Ident,
        errors: &ErrorSink<'_, Error>,
    ) -> Self {
        // We must have zero or one endpoint or channel attributes.
        let attrs = f
            .attrs
            .iter()
            .filter_map(|attr| {
                if attr.path().is_ident(ENDPOINT_IDENT) {
                    Some(ApiAttr::Endpoint(attr))
                } else if attr.path().is_ident(CHANNEL_IDENT) {
                    Some(ApiAttr::Channel(attr))
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();

        match attrs.as_slice() {
            [] => {
                // This is just a normal method.
                Self::Unmanaged(f)
            }
            [ApiAttr::Endpoint(eattr)] => {
                match ApiEndpoint::new(
                    dropshot,
                    f,
                    eattr,
                    trait_ident,
                    context_ident,
                    errors,
                ) {
                    Ok(endpoint) => Self::Endpoint(endpoint),
                    Err(summary) => Self::Invalid {
                        f,
                        kind: InvalidApiItemKind::Endpoint(summary),
                    },
                }
            }
            [ApiAttr::Channel(cattr)] => {
                match ApiChannel::new(
                    dropshot,
                    f,
                    cattr,
                    trait_ident,
                    context_ident,
                    errors,
                ) {
                    Ok(channel) => Self::Channel(channel),
                    Err(summary) => Self::Invalid {
                        f,
                        kind: InvalidApiItemKind::Channel(summary),
                    },
                }
            }
            [first, rest @ ..] => {
                // We must have exactly one endpoint or channel attribute, so
                // this is an error. Produce errors for all the rest of the
                // attrs.
                let name = &f.sig.ident;

                for attr in rest {
                    let msg = match (first, attr) {
                        (ApiAttr::Endpoint(_), ApiAttr::Endpoint(_)) => {
                            format!("method `{name}` marked as endpoint multiple times")
                        }
                        (ApiAttr::Channel(_), ApiAttr::Channel(_)) => {
                            format!("method `{name}` marked as channel multiple times")
                        }
                        _ => {
                            format!("method `{name}` marked as both endpoint and channel")
                        }
                    };

                    errors.push(Error::new_spanned(attr, msg));
                }

                Self::Invalid { f, kind: InvalidApiItemKind::Unknown }
            }
        }
    }

    fn to_out_trait_item(&self) -> TraitItemFnForSignature {
        match self {
            Self::Endpoint(e) => e.to_out_trait_item(),
            Self::Channel(c) => c.to_out_trait_item(),
            Self::Invalid { f, .. } | Self::Unmanaged(f) => {
                // Strip recognized attributes, retaining all others.
                f.clone_and_strip_recognized_attrs()
            }
        }
    }
}

fn parse_endpoint_metadata(
    name_str: &str,
    attr: &syn::Attribute,
    errors: &ErrorSink<'_, Error>,
) -> Option<ValidatedEndpointMetadata> {
    // Attempt to parse the metadata -- it must be a list.
    let l = match &attr.meta {
        syn::Meta::List(l) => l,
        _ => {
            errors.push(Error::new_spanned(
                &attr,
                format!(
                    "endpoint `{name_str}` must be of the form \
                     #[endpoint {{ method = GET, path = \"/path\", ... }}]"
                ),
            ));
            return None;
        }
    };

    match from_tokenstream_spanned::<EndpointMetadata>(
        l.delimiter.span(),
        &l.tokens,
    ) {
        Ok(m) => m.validate(name_str, attr, MacroKind::Trait, errors),
        Err(error) => {
            errors.push(Error::new(
                error.span(),
                format!(
                    "endpoint `{name_str}` has invalid attributes: {error}"
                ),
            ));
            return None;
        }
    }
}

struct ApiEndpoint<'ast> {
    f: &'ast TraitItemFnForSignature,
    attr: &'ast syn::Attribute,
    trait_ident: &'ast syn::Ident,
    metadata: ValidatedEndpointMetadata,
    params: EndpointParams<'ast>,
}

impl<'ast> ApiEndpoint<'ast> {
    /// Parses endpoint metadata to create a new `ApiEndpoint`.
    ///
    /// If the return value is None, at least one error occurred while parsing.
    fn new(
        dropshot: &TokenStream,
        f: &'ast TraitItemFnForSignature,
        attr: &'ast syn::Attribute,
        trait_ident: &'ast syn::Ident,
        context_ident: &syn::Ident,
        errors: &ErrorSink<'_, Error>,
    ) -> Result<Self, ApiItemErrorSummary> {
        let name_str = f.sig.ident.to_string();

        let metadata = parse_endpoint_metadata(&name_str, attr, errors);
        let params = EndpointParams::new(
            dropshot,
            &f.sig,
            RqctxKind::Trait { trait_ident, context_ident },
            errors,
        );

        match (metadata, params) {
            (Some(metadata), Some(params)) => {
                Ok(Self { f, attr, trait_ident, metadata, params })
            }
            // This means that something failed.
            (_, params) => {
                Err(ApiItemErrorSummary { has_param_errors: params.is_none() })
            }
        }
    }

    fn to_out_trait_item(&self) -> TraitItemFnForSignature {
        let mut f = self.f.clone();
        transform_signature(&mut f, &self.params.ret_ty);
        f
    }

    fn to_api_endpoint(
        &self,
        dropshot: &TokenStream,
        kind: FactoryKind,
    ) -> TokenStream {
        match kind {
            FactoryKind::Regular => {
                let name = &self.f.sig.ident;
                let trait_ident = self.trait_ident;
                // Adding the span information to path_to_name leads to fewer
                // call_site errors.
                let path_to_name = quote_spanned! {self.attr.span()=>
                    <ServerImpl as #trait_ident>::#name
                };
                self.to_api_endpoint_impl(
                    dropshot,
                    &ApiEndpointKind::Regular(&path_to_name),
                )
            }
            FactoryKind::Stub => {
                let extractor_types = self.params.extractor_types().collect();
                let ret_ty = self.params.ret_ty;
                self.to_api_endpoint_impl(
                    dropshot,
                    &ApiEndpointKind::Stub {
                        attr: &self.attr,
                        extractor_types,
                        ret_ty,
                    },
                )
            }
        }
    }

    fn to_api_endpoint_impl(
        &self,
        dropshot: &TokenStream,
        kind: &ApiEndpointKind<'_>,
    ) -> TokenStream {
        let name = &self.f.sig.ident;
        let name_str = name.to_string();

        let doc = ExtractedDoc::from_attrs(&self.f.attrs);

        let endpoint_fn =
            self.metadata.to_api_endpoint_fn(dropshot, &name_str, kind, &doc);

        // Note that we use name_str (string) rather than name (ident) here
        // because we deliberately want to lose the span information. If we
        // don't do that, then rust-analyzer will get confused and believe that
        // the name is both a method and a variable.
        //
        // Note that there isn't any possible variable name collision here,
        // since all names are prefixed with "endpoint_".
        let endpoint_name = format_ident!("endpoint_{}", name_str);

        quote_spanned! {self.attr.span()=>
            {
                let #endpoint_name = #endpoint_fn;
                if let Err(error) = dropshot_api.register(#endpoint_name) {
                    dropshot_errors.push(error);
                }
            }
        }
    }
}

fn parse_channel_metadata(
    name_str: &str,
    attr: &syn::Attribute,
    errors: &ErrorSink<'_, Error>,
) -> Option<ValidatedChannelMetadata> {
    // Attempt to parse the metadata -- it must be a list.
    let l = match &attr.meta {
        syn::Meta::List(l) => l,
        _ => {
            errors.push(Error::new_spanned(
                &attr,
                format!(
                    "endpoint `{name_str}` must be of the form \
                     #[channel {{ protocol = WEBSOCKETS, path = \"/path\", ... }}]"
                ),
            ));
            return None;
        }
    };

    match from_tokenstream_spanned::<ChannelMetadata>(
        l.delimiter.span(),
        &l.tokens,
    ) {
        Ok(m) => m.validate(name_str, attr, MacroKind::Trait, errors),
        Err(error) => {
            errors.push(Error::new(
                error.span(),
                format!(
                    "endpoint `{name_str}` has invalid attributes: {error}"
                ),
            ));
            return None;
        }
    }
}

struct ApiChannel<'ast> {
    f: &'ast TraitItemFnForSignature,
    attr: &'ast syn::Attribute,
    trait_ident: &'ast syn::Ident,
    metadata: ValidatedChannelMetadata,
    params: ChannelParams<'ast>,
}

impl<'ast> ApiChannel<'ast> {
    /// Parses endpoint metadata to create a new `ServerEndpoint`.
    ///
    /// If the return value is None, at least one error occurred while parsing.
    fn new(
        dropshot: &TokenStream,
        f: &'ast TraitItemFnForSignature,
        attr: &'ast syn::Attribute,
        trait_ident: &'ast syn::Ident,
        context_ident: &syn::Ident,
        errors: &ErrorSink<'_, Error>,
    ) -> Result<Self, ApiItemErrorSummary> {
        let name_str = f.sig.ident.to_string();

        let metadata = parse_channel_metadata(&name_str, attr, errors);
        let params = ChannelParams::new(
            dropshot,
            &f.sig,
            RqctxKind::Trait { trait_ident, context_ident },
            errors,
        );

        match (metadata, params) {
            (Some(metadata), Some(params)) => {
                Ok(Self { f, attr, trait_ident, metadata, params })
            }
            // This means that something failed.
            (_, params) => {
                Err(ApiItemErrorSummary { has_param_errors: params.is_none() })
            }
        }
    }

    fn to_out_trait_item(&self) -> TraitItemFnForSignature {
        let mut f = self.f.clone();
        transform_signature(&mut f, &self.params.ret_ty);
        f
    }

    fn to_api_endpoint(
        &self,
        dropshot: &TokenStream,
        kind: FactoryKind,
    ) -> TokenStream {
        match kind {
            FactoryKind::Regular => {
                // For channels, generate the adapter function here.
                let adapter_fn =
                    self.params.to_trait_adapter_fn(self.trait_ident);
                // In this case, the adapter name needs to have its type
                // parameter specified.
                let adapter_name = &self.params.adapter_name;
                let path_to_name = quote_spanned! {self.attr.span()=>
                    #adapter_name::<ServerImpl>
                };

                let endpoint = self.to_api_endpoint_impl(
                    &dropshot,
                    &ApiEndpointKind::Regular(&path_to_name),
                );

                quote_spanned! {self.attr.span()=>
                    {
                        #adapter_fn
                        #endpoint
                    }
                }
            }
            FactoryKind::Stub => {
                let extractor_types = self.params.extractor_types().collect();
                // The stub receives the adapter function's
                // signature.
                let ret_ty = &self.params.endpoint_result_ty;
                self.to_api_endpoint_impl(
                    dropshot,
                    &ApiEndpointKind::Stub {
                        attr: &self.attr,
                        extractor_types,
                        ret_ty,
                    },
                )
            }
        }
    }

    fn to_api_endpoint_impl(
        &self,
        dropshot: &TokenStream,
        kind: &ApiEndpointKind<'_>,
    ) -> TokenStream {
        let name = &self.f.sig.ident;
        let name_str = name.to_string();

        let doc = ExtractedDoc::from_attrs(&self.f.attrs);

        let endpoint_fn =
            self.metadata.to_api_endpoint_fn(dropshot, &name_str, kind, &doc);

        // Note that we use name_str (string) rather than name (ident) here
        // because we deliberately want to lose the span information. If we
        // don't do that, then rust-analyzer will get confused and believe that
        // the name is both a method and a variable.
        //
        // Note that there isn't any possible variable name collision here,
        // since all names are prefixed with "endpoint_".
        let endpoint_name = format_ident!("endpoint_{}", name_str);

        quote_spanned! {self.attr.span()=>
            {
                let #endpoint_name = #endpoint_fn;
                if let Err(error) = dropshot_api.register(#endpoint_name) {
                    dropshot_errors.push(error);
                }
            }
        }
    }
}

/// Transform the signature of an endpoint function to prepare it for output.
///
/// * Strip recognized attributes from the function.
/// * Add `Send + 'static` bounds to the output type, replacing the `async` with
///   `Future`.
fn transform_signature(f: &mut TraitItemFnForSignature, ret_ty: &syn::Type) {
    f.strip_recognized_attrs();

    // Below code adapted from https://github.com/rust-lang/impl-trait-utils
    // and used under the MIT and Apache 2.0 licenses.
    let output_ty = {
        let bounds = parse_quote_spanned! {ret_ty.span()=>
            ::core::future::Future<Output = #ret_ty> + Send + 'static
        };
        syn::Type::ImplTrait(syn::TypeImplTrait {
            impl_token: Default::default(),
            bounds,
        })
    };

    // If there's a block, then surround it with `async move`, to match the
    // fact that we're going to remove `async` from the signature.
    let block = f.block.as_ref().map(|block| {
        let block = block.clone();
        let tokens = quote_spanned! {block.span()=> async move #block };
        UnparsedBlock { brace_token: block.brace_token, tokens }
    });

    f.sig.asyncness = None;
    f.sig.output =
        syn::ReturnType::Type(Default::default(), Box::new(output_ty));
    f.block = block;
}

enum ApiAttr<'ast> {
    Endpoint(&'ast syn::Attribute),
    Channel(&'ast syn::Attribute),
}

impl ToTokens for ApiAttr<'_> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            ApiAttr::Endpoint(attr) => attr.to_tokens(tokens),
            ApiAttr::Channel(attr) => attr.to_tokens(tokens),
        }
    }
}

/// The kind of API item that's invalid, along with potentially the reasons for
/// it being so.
#[derive(Clone, Copy, Debug)]
enum InvalidApiItemKind {
    /// An invalid endpoint.
    Endpoint(ApiItemErrorSummary),

    /// An invalid channel.
    Channel(ApiItemErrorSummary),

    /// We're not sure if it's an endpoint or a channel, or the endpoint/channel
    /// annotations were provided multiple times.
    Unknown,
}

#[derive(Clone, Copy, Debug)]
struct ApiItemErrorSummary {
    // We have no need for a similar "has_metadata_errors" right now, but we
    // could add it later.
    has_param_errors: bool,
}

trait StripRecognizedAttrs {
    fn strip_recognized_attrs(&mut self);

    fn clone_and_strip_recognized_attrs(&self) -> Self
    where
        Self: Clone,
    {
        let mut cloned = self.clone();
        cloned.strip_recognized_attrs();
        cloned
    }
}

impl StripRecognizedAttrs for TraitItemPartParsed {
    fn strip_recognized_attrs(&mut self) {
        match self {
            TraitItemPartParsed::Fn(f) => f.strip_recognized_attrs(),
            TraitItemPartParsed::Other(o) => o.strip_recognized_attrs(),
        }
    }
}

impl StripRecognizedAttrs for TraitItemFnForSignature {
    fn strip_recognized_attrs(&mut self) {
        self.attrs.strip_recognized_attrs();
    }
}

impl StripRecognizedAttrs for syn::TraitItem {
    fn strip_recognized_attrs(&mut self) {
        match self {
            syn::TraitItem::Const(c) => {
                c.attrs.strip_recognized_attrs();
            }
            syn::TraitItem::Fn(f) => {
                f.attrs.strip_recognized_attrs();
            }
            syn::TraitItem::Type(t) => {
                t.attrs.strip_recognized_attrs();
            }
            syn::TraitItem::Macro(m) => {
                m.attrs.strip_recognized_attrs();
            }
            _ => {}
        }
    }
}

impl StripRecognizedAttrs for Vec<syn::Attribute> {
    fn strip_recognized_attrs(&mut self) {
        self.retain(|a| {
            !(a.path().is_ident(ENDPOINT_IDENT)
                || a.path().is_ident(CHANNEL_IDENT))
        });
    }
}

#[cfg(test)]
mod tests {
    use expectorate::assert_contents;

    use crate::{test_util::assert_banned_idents, util::DROPSHOT};

    use super::*;

    #[test]
    fn test_api_trait_basic() {
        let (item, errors) = do_trait(
            quote! {},
            quote! {
                trait MyTrait {
                    type Context;

                    #[endpoint {
                        method = GET,
                        path = "/xyz",
                        versions = "1.2.3"..
                    }]
                    async fn handler_xyz(
                        rqctx: RequestContext<Self::Context>,
                    ) -> Result<HttpResponseOk<()>, HttpError>;

                    #[channel {
                        protocol = WEBSOCKETS,
                        path = "/ws",
                        versions = ..
                    }]
                    async fn handler_ws(
                        rqctx: RequestContext<Self::Context>,
                        upgraded: WebsocketConnection,
                    ) -> WebsocketChannelResult;
                }
            },
        );

        assert!(errors.is_empty());
        assert_contents(
            "tests/output/api_trait_basic.rs",
            &prettyplease::unparse(&parse_quote! { #item }),
        );
    }

    #[test]
    fn test_api_trait_with_custom_params() {
        // Provide custom parameters and ensure that the original ones are
        // not used.
        let (item, errors) = do_trait(
            quote! {
                context = Situation,
                module = my_support_module,
                tag_config = {
                    allow_other_tags = true,
                    policy = EndpointTagPolicy::Any,
                    tags = {
                        topspin = {
                            description =
                                "Topspin is a tennis shot that \
                                 causes the ball to spin forward",
                            external_docs = {
                                description = "Wikipedia entry",
                                url = "https://en.wikipedia.org/wiki/Topspin",
                            },
                        },
                    },
                },
                _dropshot_crate = "topspin",
            },
            quote! {
                pub trait MyTrait {
                    type Situation;

                    #[endpoint { method = GET, path = "/xyz" }]
                    async fn handler_xyz(
                        rqctx: RequestContext<Self::Situation>,
                    ) -> Result<HttpResponseOk<()>, HttpError>;

                    #[channel { protocol = WEBSOCKETS, path = "/ws" }]
                    async fn handler_ws(
                        rqctx: RequestContext<Self::Situation>,
                        upgraded: WebsocketConnection,
                    ) -> WebsocketChannelResult;
                }
            },
        );

        eprintln!("errors: {:#?}", errors);
        assert!(errors.is_empty());

        let file = parse_quote! { #item };
        // Write out the file before checking it, so that we can see what it
        // looks like.
        assert_contents(
            "tests/output/api_trait_with_custom_params.rs",
            &prettyplease::unparse(&file),
        );

        // Check banned identifiers.
        let banned = [ApiMetadata::DEFAULT_CONTEXT_NAME, DROPSHOT, "my_trait"];
        assert_banned_idents(&file, banned);
    }

    // Test output for a server with no endpoints.
    //
    // This currently does not produce an error or warning -- this fact is
    // presented as a snapshot test. If we decide to add a warning or error in
    // the future, this test will change.
    #[test]
    fn test_api_trait_no_endpoints() {
        let (item, errors) = do_trait(
            quote! {},
            quote! {
                pub(crate) trait MyTrait {
                    type Context;
                }
            },
        );

        assert!(errors.is_empty());
        assert_contents(
            "tests/output/api_trait_no_endpoints.rs",
            &prettyplease::unparse(&parse_quote! { #item }),
        );
    }

    #[test]
    fn test_api_trait_operation_id() {
        let (item, errors) = do_trait(
            quote! {},
            quote! {
                pub trait MyTrait {
                    type Context;

                    #[endpoint {
                        operation_id = "vzerolower",
                        method = GET,
                        path = "/xyz"
                    }]
                    async fn handler_xyz(
                        rqctx: RequestContext<Self::Context>,
                    ) -> Result<HttpResponseOk<()>, HttpError>;

                    #[channel {
                        protocol = WEBSOCKETS,
                        path = "/ws",
                        operation_id = "vzeroupper",
                    }]
                    async fn handler_ws(
                        rqctx: RequestContext<Self::Context>,
                        upgraded: WebsocketConnection,
                    ) -> WebsocketChannelResult;
                }
            },
        );

        assert!(errors.is_empty());
        assert_contents(
            "tests/output/api_trait_operation_id.rs",
            &prettyplease::unparse(&parse_quote! { #item }),
        );
    }
}