ruststream 0.7.5

Async messaging framework for Rust: broker-agnostic traits, router, codecs, and a conformance harness for broker authors.
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
//! `AsyncAPI` 3.1 document generation from a [`RustStream`](crate::runtime::RustStream) service.
//!
//! [`build_spec`] turns the app `main` runs into a [`Spec`]: each subscriber becomes a channel
//! and a `receive` operation, a reply rides that operation, every `Out` slot message gets a `send`
//! operation, and payload and header types contribute schemas.
//!
//! # Generation
//!
//! - [AsyncAPI][asyncapi]: what the `asyncapi` feature adds to a service.
//! - [Generation][generation]: how the document is produced, serialized and served.
//! - [build_spec][build-spec]: [`build_spec`] reads the document from the app value.
//! - [Formats][formats]: [`Spec::to_json`] and [`Spec::to_yaml`].
//! - [Command][command]: `ruststream asyncapi gen` prints the document from the generated `main`
//!   (see [`cli`](crate::runtime::cli)).
//! - [Viewer][viewer]: [`render_viewer_html`] and [`ViewerOptions`] serve a page that renders it.
//! - [describe][describe]: the operation description of a manual registration.
//! - [undocumented][undocumented]: a manual registration left out of the document.
//!
//! # Schemas
//!
//! - [Schemas][schemas]: a payload type that derives `JsonSchema` (re-exported as [`schemars`])
//!   gets a payload schema.
//! - [MessageInfo][message-info]: [`MessageInfo`](crate::MessageInfo) names and describes a
//!   message component whatever the Rust type is called.
//! - [messages_without_schema][without-schema]: [`Spec::messages_without_schema`] lists the gaps
//!   for a test to fail on.
//!
//! # What the document reports
//!
//! - [What the document reports][reports]: what it says beyond the channels.
//! - [Replies][replies]: a reply is the `reply` of the operation it answers.
//! - [Parameters][parameters]: a name template becomes a channel with `parameters`.
//! - [Headers][headers]: a header contract becomes the message's headers schema.
//! - [Content][content]: each message names its codec's
//!   [`CONTENT_TYPE`](crate::codec::Codec::CONTENT_TYPE).
//! - [Retry][retry]: the attempt cap and the retry destinations as `x-ruststream-retry`.
//!
//! # Servers
//!
//! - [Servers][servers]: the brokers a service connects to, how clients authenticate, and the
//!   protocol bindings.
//! - [with_broker_labeled][with-broker-labeled]:
//!   [`with_broker_labeled`](crate::runtime::RustStream::with_broker_labeled) registers a broker
//!   under its server name, and a broker implementing [`DescribeServer`](crate::DescribeServer)
//!   describes the server.
//! - [Server][server]: [`server`](crate::runtime::RustStream::server) declares one from a
//!   [`ServerSpec`](crate::ServerSpec).
//! - [Security][security]: [`ServerSpec::security`](crate::ServerSpec::security) with a
//!   [`SecurityScheme`](crate::SecurityScheme).
//! - [Bindings][bindings]: [`Bindings`] carry what one protocol says about a server, a channel,
//!   an operation or a message.
//!
//! # Service info
//!
//! - [Service info][service-info]: the `info` section, what the service is and who owns it.
//! - [Info][app-info]: [`AppInfo`](crate::runtime::AppInfo), the title, the version and the
//!   description.
//! - [AppId][app-id]: [`AppId`], the service's identifier, parsed as a URI.
//! - [Contact][contact]: [`Contact`], who answers for the service.
//! - [License][license]: [`License`].
//! - [Tag][tag]: [`Tag`], a label that groups services.
//! - [ExternalDocs][external-docs]: [`ExternalDocs`], documentation kept
//!   outside the document.
//!
//! [asyncapi]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/
//! [generation]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/
//! [reports]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/reports/
//! [build-spec]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/build-spec/
//! [formats]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/to-json-yaml/
//! [command]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/cli-gen/
//! [viewer]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/viewer/
//! [describe]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/describe/
//! [undocumented]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/undocumented/
//! [schemas]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/schemas/
//! [message-info]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/message-info/
//! [without-schema]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/generation/messages-without-schema/
//! [replies]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/reports/replies/
//! [parameters]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/reports/channel-parameters/
//! [headers]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/reports/header-schemas/
//! [content]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/reports/content-type/
//! [retry]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/reports/retry-extension/
//! [servers]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/servers/
//! [with-broker-labeled]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/servers/with-broker-labeled/
//! [server]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/servers/server/
//! [security]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/servers/security-schemes/
//! [bindings]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/servers/bindings/
//! [service-info]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/service-info/
//! [app-info]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/service-info/app-info/
//! [app-id]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/service-info/app-id/
//! [contact]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/service-info/contact/
//! [license]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/service-info/license/
//! [tag]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/service-info/tag/
//! [external-docs]: https://powersemmi.github.io/ruststream/latest/components/asyncapi/service-info/external-docs/

use std::collections::BTreeMap;
use std::num::NonZeroU32;

use schemars::{JsonSchema, generate::SchemaSettings};
use serde::Serialize;
use serde_json::Value;
use tracing::warn;

mod bindings;
mod viewer;

pub use bindings::{Binding, BindingError, Bindings, PublishBindings, SubscriptionBindings};
pub use viewer::{ViewerOptions, render_viewer_html};

use crate::describe::{AppId, Contact, ExternalDocs, License, Tag};
use crate::runtime::{App, OutgoingKind};

/// The specification version every generated document declares.
const ASYNCAPI_VERSION: &str = "3.1.0";

/// An `AsyncAPI` 3.1 document.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Spec {
    /// The `AsyncAPI` specification version (always `"3.1.0"`).
    pub asyncapi: String,
    /// The service's own identifier, when it declared one
    /// ([`AppInfo::id`](method@crate::runtime::AppInfo::id)).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<AppId>,
    /// Service metadata.
    pub info: Info,
    /// Servers (one per broker the service connects to), keyed by server name.
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub servers: BTreeMap<String, Server>,
    /// The media type every message names, when all of them name the same one. A second format,
    /// or a message no codec reads or writes, leaves it out; each codec-encoded message still
    /// states its own.
    #[serde(rename = "defaultContentType", skip_serializing_if = "Option::is_none")]
    pub default_content_type: Option<String>,
    /// Channels, keyed by channel id (the name).
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub channels: BTreeMap<String, Channel>,
    /// Operations, keyed by operation id.
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub operations: BTreeMap<String, Operation>,
    /// Reusable components (message definitions).
    pub components: Components,
}

impl Spec {
    /// Serializes the document to pretty-printed JSON.
    ///
    /// # Errors
    ///
    /// Returns [`serde_json::Error`] if serialization fails (not expected for a well-formed spec).
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(all(feature = "asyncapi", feature = "macros", feature = "memory", feature = "json"))]
    /// # mod demo {
    /// use ruststream::asyncapi::build_spec;
    /// use ruststream::memory::prelude::*;
    /// use ruststream::schemars::JsonSchema;
    /// use serde::{Deserialize, Serialize};
    ///
    /// /// An order placed by a customer.
    /// #[derive(Deserialize, JsonSchema)]
    /// struct Order {
    ///     id: u64,
    /// }
    ///
    /// /// The confirmation an order gets back.
    /// #[derive(Serialize, Outgoing, JsonSchema)]
    /// struct Confirmed {
    ///     id: u64,
    /// }
    ///
    /// /// Answers every request on `requests` with a confirmation on `responses`.
    /// #[subscriber("requests", reply("responses"))]
    /// async fn confirm(order: &Order) -> Confirmed {
    ///     Confirmed { id: order.id }
    /// }
    ///
    /// fn document() -> Result<String, serde_json::Error> {
    ///     let app = RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker_labeled(
    ///         "in-process",
    ///         MemoryBroker::new(),
    ///         |b| {
    ///             b.include(confirm);
    ///         },
    ///     );
    ///     build_spec(&app).to_json()
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// The message components carrying no payload schema, excluding the deliberately
    /// schema-free ones (a [`Deserialized`](crate::runtime::Deserialized) input, a
    /// [`Serialized`](crate::runtime::Serialized) reply - their bytes are their own wire
    /// format): the models that would document better with a [`schemars::JsonSchema`] derive.
    ///
    /// [`build_spec`] logs a `WARN` per gap when the document is generated; this accessor makes
    /// the same list assertable, so a service can gate schema coverage in its test suite.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(all(feature = "macros", feature = "memory", feature = "json"))]
    /// # mod demo {
    /// use ruststream::asyncapi::build_spec;
    /// use ruststream::memory::prelude::*;
    /// use ruststream::schemars::JsonSchema;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize, JsonSchema)]
    /// struct Order {
    ///     id: u64,
    /// }
    ///
    /// #[subscriber("orders")]
    /// async fn accept(order: &Order) -> HandlerOutcome {
    ///     tracing::info!(order.id, "accepted");
    ///     HandlerOutcome::ack()
    /// }
    ///
    /// pub fn app() -> RustStream {
    ///     RustStream::new(AppInfo::new("orders", "1.0.0")).with_broker(MemoryBroker::new(), |b| {
    ///         b.include(accept);
    ///     })
    /// }
    ///
    /// /// A test in the service's suite: every model in the document carries a schema.
    /// pub fn every_message_has_a_schema() {
    ///     let spec = build_spec(&app());
    ///     assert_eq!(spec.messages_without_schema(), Vec::<&str>::new());
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    #[must_use]
    pub fn messages_without_schema(&self) -> Vec<&str> {
        self.components
            .messages
            .values()
            // The "bytes" name stays excluded next to the flag: a hand-rolled metadata entry
            // spells its schema-free intent through the label alone.
            .filter(|message| {
                message.payload.is_none() && !message.schemaless && message.name != "bytes"
            })
            .map(|message| message.name.as_str())
            .collect()
    }

    /// Serializes the document to YAML.
    ///
    /// # Errors
    ///
    /// Returns [`serde_norway::Error`] if serialization fails (not expected for a well-formed spec).
    pub fn to_yaml(&self) -> Result<String, serde_norway::Error> {
        serde_norway::to_string(self)
    }
}

/// An `AsyncAPI` server: where and how clients reach a broker.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Server {
    /// The host (and optional port), e.g. `"nats.example.com:4222"`. Absent for an in-process
    /// broker with no network address (the in-memory broker).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub host: Option<String>,
    /// The messaging protocol, e.g. `"nats"`.
    pub protocol: String,
    /// The version of that protocol, when the broker names one: `"0.9.1"` against `"1.0"` for
    /// AMQP, `"5"` for MQTT.
    #[serde(rename = "protocolVersion", skip_serializing_if = "Option::is_none")]
    pub protocol_version: Option<String>,
    /// Optional human description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// References into `components.securitySchemes` describing how clients authenticate. Empty
    /// (and absent from the document) unless the service author attached schemes with
    /// [`ServerSpec::security`](method@crate::ServerSpec::security).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub security: Vec<Reference>,
    /// What the broker says about this server in its own protocol's vocabulary.
    #[serde(skip_serializing_if = "Bindings::is_empty")]
    pub bindings: Bindings,
}

/// `AsyncAPI` `Info` object: what the service is, who owns it, and under what terms it runs.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Info {
    /// Service title.
    pub title: String,
    /// Service version.
    pub version: String,
    /// Optional description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Where the terms of service are published.
    #[serde(rename = "termsOfService", skip_serializing_if = "Option::is_none")]
    pub terms_of_service: Option<String>,
    /// Who to contact about the service.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contact: Option<Contact>,
    /// The licence the service is published under.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<License>,
    /// Labels grouping the service among its neighbours.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<Tag>,
    /// Where the prose about this service lives.
    #[serde(rename = "externalDocs", skip_serializing_if = "Option::is_none")]
    pub external_docs: Option<ExternalDocs>,
}

/// An `AsyncAPI` channel: an address plus the messages that flow over it.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Channel {
    /// The channel address (the broker name / subject). A templated address keeps its
    /// `{placeholder}` segments, and every one of them is declared in
    /// [`parameters`](Self::parameters).
    ///
    /// `None` - rendered as `null`, which the specification reads as "decided at runtime" - on
    /// the reply channel of a registration whose transform names the destination per delivery.
    /// The operation's `reply.address.location` then says where a client reads it.
    pub address: Option<String>,
    /// Messages on this channel, keyed by message name, referencing component definitions.
    pub messages: BTreeMap<String, Reference>,
    /// The address's parameters, one per `{placeholder}` segment; empty (and omitted) for a
    /// fixed address.
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub parameters: BTreeMap<String, Parameter>,
    /// The servers this channel exists on, as references into the document's `servers` map.
    ///
    /// Filled from the label the registration's broker was registered under
    /// ([`with_broker_labeled`](crate::runtime::RustStream::with_broker_labeled)), and from the
    /// single server of a one-broker document. Empty where neither applies, which per the
    /// specification means the channel is available on every server: that is the honest answer
    /// when the registration named no broker, and it is also the limit of what the core can say
    /// about a publish that leaves through a [`Bound`](crate::runtime::Bound) token, since such a
    /// publish reaches the token's own broker rather than the registration's.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub servers: Vec<Reference>,
    /// What the broker says about this channel in its own protocol's vocabulary.
    #[serde(skip_serializing_if = "Bindings::is_empty")]
    pub bindings: Bindings,
}

/// An `AsyncAPI` channel parameter: one `{placeholder}` segment of a templated address.
///
/// The declaration names the segments; what a service puts in them is a run-time value.
#[derive(Debug, Clone, Default, Serialize)]
#[non_exhaustive]
pub struct Parameter {
    /// Optional human description of the segment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// An `AsyncAPI` operation: an action on a channel.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Operation {
    /// The action: `"receive"` for subscribers, `"send"` for declared outgoing messages.
    pub action: String,
    /// Reference to the channel this operation acts on.
    pub channel: Reference,
    /// The messages this operation handles.
    pub messages: Vec<Reference>,
    /// Optional human description, typically from the handler's doc comment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// What answers this operation, for a request-reply registration: the channel the reply goes
    /// to and the messages it carries. Absent on an operation nothing answers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply: Option<OperationReply>,
    /// Labels grouping the operation. The core contributes the protocol of the server the
    /// operation's channel lives on, so a multi-broker document reads by transport.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<Tag>,
    /// What the registration declared about retrying a failed delivery, under the extension key
    /// `x-ruststream-retry`. Absent unless the mount site declared a cap or a dead-letter
    /// destination.
    ///
    /// It is an extension because no binding carries it: the specification models a dead-letter
    /// queue only for `sqs` and `sns`, and nothing at all for the attempt cap.
    #[serde(rename = "x-ruststream-retry", skip_serializing_if = "Option::is_none")]
    pub retry: Option<RetryExtension>,
    /// What the broker says about this operation in its own protocol's vocabulary.
    #[serde(skip_serializing_if = "Bindings::is_empty")]
    pub bindings: Bindings,
}

/// What answers a `receive` operation: the `reply` of an `AsyncAPI` operation.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct OperationReply {
    /// Where the reply goes when the registration decides that per delivery, as a runtime
    /// expression. Absent where the reply goes to the channel's own address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address: Option<ReplyAddress>,
    /// Reference to the channel the reply is published to.
    pub channel: Reference,
    /// The messages the reply carries.
    pub messages: Vec<Reference>,
}

/// Where a client finds the address a reply is published to: the `AsyncAPI` operation reply
/// address object.
///
/// Filled from the publish policy's
/// [`reply_address_location`](crate::PublishPolicy::reply_address_location), which a broker
/// answers with the header carrying the address (`$message.header#/reply-to`).
///
/// # Examples
///
/// ```
/// use ruststream::asyncapi::Spec;
///
/// /// Where a client generator finds the address to read an operation's reply from.
/// fn reply_location<'a>(spec: &'a Spec, operation: &str) -> Option<&'a str> {
///     let reply = spec.operations.get(operation)?.reply.as_ref()?;
///     reply
///         .address
///         .as_ref()
///         .map(|address| address.location.as_str())
/// }
/// ```
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct ReplyAddress {
    /// The runtime expression naming the address, per the specification's grammar.
    pub location: String,
}

impl ReplyAddress {
    /// The address a reply is published to, as a runtime expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use ruststream::asyncapi::{ReplyAddress, Spec};
    ///
    /// /// The gateway in front of the service answers on the inbox a request names, which the
    /// /// broker underneath cannot say, so the published document is completed here.
    /// fn add_gateway_reply(spec: &mut Spec) {
    ///     if let Some(reply) = spec
    ///         .operations
    ///         .get_mut("receive_requests")
    ///         .and_then(|operation| operation.reply.as_mut())
    ///     {
    ///         reply.address = Some(ReplyAddress::new("$message.header#/reply-to"));
    ///     }
    /// }
    /// ```
    #[must_use]
    pub fn new(location: impl Into<String>) -> Self {
        Self {
            location: location.into(),
        }
    }
}

/// What a registration declared about retrying a failed delivery, rendered under
/// `x-ruststream-retry`.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct RetryExtension {
    /// How many times a delivery is handed to the handler before it is given up on.
    #[serde(rename = "maxAttempts", skip_serializing_if = "Option::is_none")]
    pub max_attempts: Option<u32>,
    /// Where a delivery out of attempts goes. The channel is in the document too, with a `send`
    /// operation: the traffic is real.
    #[serde(rename = "deadLetter", skip_serializing_if = "Option::is_none")]
    pub dead_letter: Option<String>,
    /// Where the registration publishes a deferred copy of a delivery, when the mount site named
    /// a destination for its retry copies. That channel is in the document too, with a `send`
    /// operation. Absent where the copies go back to the subscription's own address, which is a
    /// channel the document already carries.
    #[serde(rename = "retryDestination", skip_serializing_if = "Option::is_none")]
    pub retry_destination: Option<String>,
}

/// Reusable `AsyncAPI` components.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Components {
    /// Message definitions, keyed by message name.
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub messages: BTreeMap<String, MessageObject>,
    /// Security scheme definitions the servers reference, keyed by scheme name (the server's
    /// name, `-N`-suffixed when a server declares several).
    #[serde(rename = "securitySchemes", skip_serializing_if = "BTreeMap::is_empty")]
    pub security_schemes: BTreeMap<String, Value>,
}

/// An `AsyncAPI` message definition.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct MessageObject {
    /// The message name.
    pub name: String,
    /// The human-readable title, from the payload schema's own, when it says something the
    /// machine name does not.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Optional human description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// The media type the payload is encoded in, from the codec that decodes this message.
    #[serde(rename = "contentType", skip_serializing_if = "Option::is_none")]
    pub content_type: Option<String>,
    /// The JSON Schema of the payload, when the message type implements
    /// [`schemars::JsonSchema`]. Absent for raw-bytes handlers and types without a schema.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payload: Option<Value>,
    /// The JSON Schema of the application headers, when the handler declares a typed header
    /// contract (a `Headers<T>` parameter). The schema describes the logical contract; on
    /// the wire, header values are string-encoded.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<Value>,
    /// True when the message rides a self-carrying lane
    /// ([`Deserialized`](crate::runtime::Deserialized) input /
    /// [`Serialized`](crate::runtime::Serialized) reply): the bytes are their own wire format,
    /// so the missing payload schema is by design. Generation bookkeeping, not part of the
    /// rendered document.
    #[serde(skip)]
    pub schemaless: bool,
    /// What the broker says about this message in its own protocol's vocabulary.
    #[serde(skip_serializing_if = "Bindings::is_empty")]
    pub bindings: Bindings,
}

/// A JSON `$ref` pointer.
#[derive(Debug, Clone, Serialize)]
pub struct Reference {
    /// The reference target, e.g. `#/components/messages/Order`.
    #[serde(rename = "$ref")]
    pub reference: String,
}

impl Reference {
    fn new(target: impl Into<String>) -> Self {
        Self {
            reference: target.into(),
        }
    }
}

/// Builds an [`AsyncAPI`](Spec) 3.1.0 document from a service's handlers and metadata.
///
/// Each registered subscriber becomes a channel (addressed by its name), a `receive` operation,
/// and a message component named after the handler's input type.
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "macros", feature = "memory", feature = "json"))]
/// # mod demo {
/// use std::error::Error;
/// use std::fs;
///
/// use ruststream::asyncapi::build_spec;
/// use ruststream::memory::prelude::*;
/// use ruststream::schemars::JsonSchema;
/// use serde::Deserialize;
///
/// #[derive(Deserialize, JsonSchema)]
/// struct Order {
///     id: u64,
/// }
///
/// #[subscriber("orders")]
/// async fn accept(order: &Order) -> HandlerOutcome {
///     tracing::info!(order.id, "accepted");
///     HandlerOutcome::ack()
/// }
///
/// pub fn app() -> RustStream {
///     RustStream::new(AppInfo::new("orders", "1.0.0")).with_broker(MemoryBroker::new(), |b| {
///         b.include(accept);
///     })
/// }
///
/// /// Writes the document the service publishes next to its code, from the app `main` runs.
/// pub fn write_document() -> Result<(), Box<dyn Error>> {
///     let spec = build_spec(&app());
///     fs::write("asyncapi.json", spec.to_json()?)?;
///     Ok(())
/// }
/// # }
/// # fn main() {}
/// ```
#[must_use]
pub fn build_spec<A: App>(app: &A) -> Spec {
    let contact = app.info().contact.clone();
    let info = Info {
        title: app.info().title.clone(),
        version: app.info().version.clone(),
        description: app.info().description.clone(),
        terms_of_service: app.info().terms_of_service.clone(),
        contact: (!contact.is_empty()).then_some(contact),
        license: app.info().license.clone(),
        tags: app.info().tags.clone(),
        external_docs: app.info().external_docs.clone(),
    };

    let (servers, security_schemes) = build_servers(app);

    let mut channels = BTreeMap::new();
    let mut operations = BTreeMap::new();
    let mut messages = BTreeMap::new();

    for handler in app.handlers() {
        let server = resolve_server(handler.server.as_deref(), &servers);
        let on_servers = server
            .map(|name| vec![Reference::new(format!("#/servers/{name}"))])
            .unwrap_or_default();
        let tags = server
            .and_then(|name| servers.get(name))
            .map(|server| vec![Tag::new(server.protocol.clone())])
            .unwrap_or_default();

        // The reply contributes its channel and its message before the `receive` operation
        // points at them, and it takes no `send` operation of its own: what answers a request
        // is the request operation's `reply`.
        let reply = handler
            .outgoing
            .iter()
            .find(|outgoing| outgoing.kind == OutgoingKind::Answer)
            .map(|outgoing| {
                let refs = add_outgoing(outgoing, &on_servers, &mut channels, &mut messages);
                OperationReply {
                    address: outgoing.reply_address_location.map(ReplyAddress::new),
                    channel: refs.channel_ref(),
                    messages: vec![refs.message_ref()],
                }
            });

        add_receive(
            handler,
            reply,
            &on_servers,
            &tags,
            &mut channels,
            &mut operations,
            &mut messages,
        );

        // One `send` operation per remaining declared outgoing message: every Out slot
        // dictionary entry, the dead-letter destination of a `dead_letter(..)` declaration, and
        // the destination a `.to(name)` gave the registration's retry copies.
        // A channel several messages flow through lists them all; the message component is
        // shared with any handler that receives it.
        for outgoing in &handler.outgoing {
            if outgoing.kind == OutgoingKind::Answer {
                continue;
            }
            let refs = add_outgoing(outgoing, &on_servers, &mut channels, &mut messages);
            operations.insert(
                send_operation_id(&operations, handler.name.as_ref(), &refs.channel),
                Operation {
                    action: "send".to_owned(),
                    channel: refs.channel_ref(),
                    messages: vec![refs.message_ref()],
                    description: None,
                    reply: None,
                    tags: tags.clone(),
                    retry: None,
                    bindings: outgoing.bindings.operation.clone(),
                },
            );
        }
    }

    let default_content_type = agreed_content_type(&messages);

    Spec {
        asyncapi: ASYNCAPI_VERSION.to_owned(),
        id: app.info().id.clone(),
        info,
        servers,
        default_content_type,
        channels,
        operations,
        components: Components {
            messages,
            security_schemes,
        },
    }
}

/// Renders the service's registered servers, collecting the security schemes they reference into
/// the components map as it goes.
fn build_servers<A: App>(app: &A) -> (BTreeMap<String, Server>, BTreeMap<String, Value>) {
    let mut security_schemes = BTreeMap::new();
    let servers = app
        .servers()
        .iter()
        .map(|(name, spec)| {
            let security = spec
                .security
                .iter()
                .enumerate()
                .map(|(index, scheme)| {
                    let key = if index == 0 {
                        name.clone()
                    } else {
                        format!("{name}-{index}")
                    };
                    security_schemes.insert(key.clone(), security_scheme_object(scheme));
                    Reference::new(format!("#/components/securitySchemes/{key}"))
                })
                .collect();
            (
                name.clone(),
                Server {
                    host: spec.host.clone(),
                    protocol: spec.protocol.clone(),
                    protocol_version: spec.protocol_version.clone(),
                    description: spec.description.clone(),
                    security,
                    bindings: spec.bindings.clone(),
                },
            )
        })
        .collect();
    (servers, security_schemes)
}

/// The server a registration's channels live on: the label the broker was registered under, or
/// the single server of a one-broker document.
///
/// A document with several servers and an unlabeled registration gets no answer, and the channel
/// then says nothing rather than claiming a server it cannot name.
fn resolve_server<'a>(
    label: Option<&'a str>,
    servers: &'a BTreeMap<String, Server>,
) -> Option<&'a str> {
    match label {
        Some(label) if servers.contains_key(label) => Some(label),
        _ if servers.len() == 1 => servers.keys().next().map(String::as_str),
        _ => None,
    }
}

/// The media type every message in the document names, for the root `defaultContentType`.
///
/// A reader applies the root value to the whole document, and `AsyncAPI` gives it to each message
/// that names no media type of its own. So it stands only while every message names the same one:
/// a second format, or a message no codec reads, would be misdescribed by it.
fn agreed_content_type(messages: &BTreeMap<String, MessageObject>) -> Option<String> {
    let mut agreed: Option<&str> = None;
    for message in messages.values() {
        let content_type = message.content_type.as_deref()?;
        match agreed {
            None => agreed = Some(content_type),
            Some(existing) if existing == content_type => {}
            Some(_) => return None,
        }
    }
    agreed.map(str::to_owned)
}

/// Renders a [`SecurityScheme`](crate::SecurityScheme) as its `AsyncAPI` security scheme object.
fn security_scheme_object(scheme: &crate::SecurityScheme) -> Value {
    use crate::capability::SecuritySchemeKind as Kind;

    // Raw payloads round-trip through the string the constructor serialized, so parsing them
    // back cannot fail; Null is the unreachable fallback, not an error path.
    let parse = |raw: &str| serde_json::from_str::<Value>(raw).unwrap_or(Value::Null);
    let mut object = match &scheme.kind {
        Kind::UserPassword => serde_json::json!({ "type": "userPassword" }),
        Kind::ApiKey { location } => {
            serde_json::json!({ "type": "apiKey", "in": location.as_api() })
        }
        Kind::X509 => serde_json::json!({ "type": "X509" }),
        Kind::Plain => serde_json::json!({ "type": "plain" }),
        Kind::ScramSha256 => serde_json::json!({ "type": "scramSha256" }),
        Kind::ScramSha512 => serde_json::json!({ "type": "scramSha512" }),
        Kind::Gssapi => serde_json::json!({ "type": "gssapi" }),
        Kind::Http { scheme } => serde_json::json!({ "type": "http", "scheme": scheme }),
        Kind::HttpApiKey { name, location } => serde_json::json!({
            "type": "httpApiKey",
            "name": name,
            "in": location.as_api(),
        }),
        Kind::OpenIdConnect { url } => serde_json::json!({
            "type": "openIdConnect",
            "openIdConnectUrl": url,
        }),
        Kind::Oauth2 { flows } => serde_json::json!({ "type": "oauth2", "flows": parse(flows) }),
        Kind::Custom { object } => parse(object),
    };
    if let (Some(description), Some(fields)) = (&scheme.description, object.as_object_mut()) {
        fields.insert("description".to_owned(), Value::String(description.clone()));
    }
    object
}

/// Serializes the JSON Schema of `T` in the draft the document's Schema Object is a superset of.
///
/// The `AsyncAPI` Schema Object extends JSON Schema Draft 07, while `schemars` generates
/// 2020-12 by default: a payload generated with the default settings claims one draft inside a
/// document that says another. Every schema in the document goes through here.
pub(crate) fn schema_json<T: JsonSchema + ?Sized>() -> Option<String> {
    let schema = SchemaSettings::draft07()
        .into_generator()
        .into_root_schema_for::<T>();
    serde_json::to_string(&schema).ok()
}

/// Takes the final path segment of a type name as the message name (`a::b::Order` -> `Order`).
fn message_name(type_name: &str) -> String {
    type_name
        .rsplit("::")
        .next()
        .unwrap_or(type_name)
        .to_owned()
}

/// Adds one registered subscriber to the document: its channel entry, its `receive` operation,
/// and its message component (with the payload and headers schemas the handler declared).
fn add_receive(
    handler: &crate::runtime::HandlerMetadata,
    reply: Option<OperationReply>,
    on_servers: &[Reference],
    tags: &[Tag],
    channels: &mut BTreeMap<String, Channel>,
    operations: &mut BTreeMap<String, Operation>,
    messages: &mut BTreeMap<String, MessageObject>,
) {
    let name = handler.name.as_ref();
    let payload = handler
        .payload_schema
        .as_deref()
        .and_then(|json| serde_json::from_str::<Value>(json).ok());
    // A self-deserializing input is deliberately schema-free (its bytes are its own wire
    // format); a typed model without a schema is a documentation gap worth flagging at
    // generation time.
    if payload.is_none() && !handler.deserialized && handler.input_type != "bytes" {
        warn!(
            target: "ruststream::asyncapi",
            subscription = %name,
            message_type = handler.input_type,
            "message type has no JSON Schema; its AsyncAPI message carries no payload schema - \
             derive schemars::JsonSchema on it",
        );
    }
    let headers = handler
        .headers_schema
        .as_deref()
        .and_then(|json| serde_json::from_str::<Value>(json).ok());
    // The JsonSchema derive captures the type's own doc comment (and a schemars title /
    // rename), so a documented payload type feeds the component without a Message impl.
    let schema_str = |key: &str| {
        payload
            .as_ref()
            .and_then(|schema| schema.get(key))
            .and_then(Value::as_str)
            .map(str::to_owned)
    };
    // A `Message` impl on the input type names the component; the schema title is next; the
    // type name is the fallback.
    let message_name = handler
        .message_name
        .as_ref()
        .map(ToString::to_string)
        .or_else(|| schema_str("title"))
        .unwrap_or_else(|| message_name(handler.input_type));

    let channel = channels.entry(name.to_owned()).or_insert_with(|| Channel {
        address: Some(name.to_owned()),
        messages: BTreeMap::new(),
        parameters: BTreeMap::new(),
        servers: on_servers.to_vec(),
        bindings: Bindings::new(),
    });
    // A subscription reads a real address, whatever a publish that reached this channel first
    // said about its own destination.
    channel.address = Some(name.to_owned());
    // A channel a publish created first carries no binding: the descriptor that reads it is what
    // describes it, and it may reach the channel second.
    channel.bindings.fill_from(&handler.bindings.channel);
    channel.messages.insert(
        message_name.clone(),
        Reference::new(format!("#/components/messages/{message_name}")),
    );

    operations.insert(
        receive_operation_id(operations, name),
        Operation {
            action: "receive".to_owned(),
            channel: Reference::new(format!("#/channels/{name}")),
            messages: vec![Reference::new(format!(
                "#/channels/{name}/messages/{message_name}"
            ))],
            description: handler.description.as_ref().map(ToString::to_string),
            reply,
            tags: tags.to_vec(),
            retry: retry_extension(handler),
            bindings: handler.bindings.operation.clone(),
        },
    );

    // Message::DESCRIPTION wins, then the type's own doc comment from the schema, then the
    // handler doc (already on the operation) so plain types keep their description.
    let message_description = handler
        .message_description
        .as_ref()
        .map(ToString::to_string)
        .or_else(|| schema_str("description"))
        .or_else(|| handler.description.as_ref().map(ToString::to_string));

    merge_message(
        messages,
        MessageContribution {
            name: message_name,
            title: schema_str("title"),
            description: message_description,
            content_type: handler.content_type.map(str::to_owned),
            payload,
            headers,
            schemaless: handler.deserialized,
            bindings: handler.bindings.message.clone(),
        },
        name,
    );
}

/// Renders what a registration declared about retrying as the `x-ruststream-retry` extension, or
/// nothing when it declared nothing: the cap, where a spent delivery is dead-lettered, and where
/// the deferred copies go when the mount site named a destination for them.
fn retry_extension(handler: &crate::runtime::HandlerMetadata) -> Option<RetryExtension> {
    let retry_destination = handler
        .outgoing
        .iter()
        .find(|outgoing| outgoing.kind == OutgoingKind::RetryCopy)
        .map(|outgoing| outgoing.channel.as_ref().to_owned());
    let declaration = &handler.retry;
    if declaration.declares_nothing() && retry_destination.is_none() {
        return None;
    }
    Some(RetryExtension {
        max_attempts: declaration.max_attempts().map(NonZeroU32::get),
        dead_letter: declaration.dead_letter().map(str::to_owned),
        retry_destination,
    })
}

/// One side's contribution to a shared message component.
struct MessageContribution {
    name: String,
    title: Option<String>,
    description: Option<String>,
    content_type: Option<String>,
    payload: Option<Value>,
    headers: Option<Value>,
    schemaless: bool,
    bindings: Bindings,
}

/// Merges one contribution into a shared message component: an absent title, description,
/// media type, payload, or headers schema fills in from a later contributor (component identity
/// is the message name, and several handlers may carry different slices of its metadata), while
/// a conflicting headers schema keeps the first one with a WARN - one component cannot carry two
/// contracts, and the conflict usually means two handlers read the same type with different
/// `Headers` declarations.
fn merge_message(
    messages: &mut BTreeMap<String, MessageObject>,
    contribution: MessageContribution,
    context: &str,
) {
    let MessageContribution {
        name,
        title,
        description,
        content_type,
        payload,
        headers,
        schemaless,
        bindings,
    } = contribution;
    let entry = messages
        .entry(name.clone())
        .or_insert_with(|| MessageObject {
            name,
            title: None,
            description: None,
            content_type: None,
            payload: None,
            headers: None,
            schemaless: false,
            bindings: Bindings::new(),
        });
    entry.bindings.fill_from(&bindings);
    // A title repeating the machine name says nothing the reader does not already see.
    if entry.title.is_none() && title.as_deref().is_some_and(|title| title != entry.name) {
        entry.title = title;
    }
    if entry.description.is_none() {
        entry.description = description;
    }
    if entry.content_type.is_none() {
        entry.content_type = content_type;
    }
    // One schema-free-by-design contributor is enough: the shared component's missing schema
    // is then explained, whichever side contributed it.
    entry.schemaless |= schemaless;
    if entry.payload.is_none() {
        entry.payload = payload;
    }
    match (&entry.headers, headers) {
        (None, Some(headers)) => entry.headers = Some(headers),
        (Some(existing), Some(headers)) if *existing != headers => {
            warn!(
                target: "ruststream::asyncapi",
                message = %entry.name,
                context = %context,
                "conflicting headers schemas for one message component; keeping the first",
            );
        }
        _ => {}
    }
}

/// Where an operation points to reach one declared outgoing message.
struct OutgoingRefs {
    channel: String,
    message: String,
}

impl OutgoingRefs {
    fn channel_ref(&self) -> Reference {
        Reference::new(format!("#/channels/{}", self.channel))
    }

    fn message_ref(&self) -> Reference {
        Reference::new(format!(
            "#/channels/{}/messages/{}",
            self.channel, self.message
        ))
    }
}

/// Adds one declared outgoing message to the document: its channel entry and its message
/// component (shared with any handler that receives the same type), answering the references an
/// operation uses to point at them. The message name falls back like the receive side: `Message`
/// impl, then schema title, then the type name.
///
/// What operation the caller then writes is the entry's kind: a slot publish and a dead-letter
/// destination each get a `send` operation, while a reply becomes the `reply` of the `receive`
/// operation it answers.
fn add_outgoing(
    outgoing: &crate::runtime::OutgoingMessageMetadata,
    on_servers: &[Reference],
    channels: &mut BTreeMap<String, Channel>,
    messages: &mut BTreeMap<String, MessageObject>,
) -> OutgoingRefs {
    let payload = outgoing
        .payload_schema
        .as_deref()
        .and_then(|json| serde_json::from_str::<Value>(json).ok());
    // A serialized-wire message is deliberately schema-free (its bytes are its own wire
    // format); a typed model without a schema is a documentation gap worth flagging at
    // generation time.
    if payload.is_none() && !outgoing.serialized && outgoing.message_type != "bytes" {
        warn!(
            target: "ruststream::asyncapi",
            channel = %outgoing.channel,
            message_type = outgoing.message_type,
            "outgoing message type has no JSON Schema; its AsyncAPI message carries no payload \
             schema - derive schemars::JsonSchema on it",
        );
    }
    let headers = outgoing
        .headers_schema
        .as_deref()
        .and_then(|json| serde_json::from_str::<Value>(json).ok());
    let title = payload
        .as_ref()
        .and_then(|schema| schema.get("title"))
        .and_then(Value::as_str)
        .map(str::to_owned);
    let name = outgoing
        .message_name
        .map(str::to_owned)
        .or_else(|| title.clone())
        .unwrap_or_else(|| message_name(outgoing.message_type));
    let description = outgoing.message_description.map(str::to_owned).or_else(|| {
        payload
            .as_ref()
            .and_then(|schema| schema.get("description"))
            .and_then(Value::as_str)
            .map(str::to_owned)
    });
    let channel = outgoing.channel.as_ref();

    let entry = channels
        .entry(channel.to_owned())
        .or_insert_with(|| Channel {
            // A destination named per delivery has no address to report: the operation's reply
            // says where a client reads it instead.
            address: outgoing
                .reply_address_location
                .is_none()
                .then(|| channel.to_owned()),
            messages: BTreeMap::new(),
            // A templated address declares its placeholders, so the document says what the
            // segments are instead of showing an address nothing describes.
            parameters: outgoing
                .parameters
                .iter()
                .map(|segment| ((*segment).to_owned(), Parameter::default()))
                .collect(),
            servers: on_servers.to_vec(),
            bindings: Bindings::new(),
        });
    entry.bindings.fill_from(&outgoing.bindings.channel);
    entry.messages.insert(
        name.clone(),
        Reference::new(format!("#/components/messages/{name}")),
    );

    merge_message(
        messages,
        MessageContribution {
            name: name.clone(),
            title,
            description,
            // The media type comes from the codec the mount chain bound on this position; a
            // message also received somewhere contributes the receiving side's too, and the
            // first contributor wins.
            content_type: outgoing.content_type.map(str::to_owned),
            payload,
            headers,
            schemaless: outgoing.serialized,
            bindings: outgoing.bindings.message.clone(),
        },
        channel,
    );

    OutgoingRefs {
        channel: channel.to_owned(),
        message: name,
    }
}

/// Derives a stable `receive` operation id from a subscription name.
///
/// Several handlers may share one channel (each opens its own subscription), so the name alone
/// does not identify an operation: a collision takes a deterministic numeric suffix instead of
/// overwriting the operation already there, as on the send side.
fn receive_operation_id(operations: &BTreeMap<String, Operation>, name: &str) -> String {
    let base = format!("receive_{}", sanitize_id(name));
    if !operations.contains_key(&base) {
        return base;
    }
    let mut suffix = 2;
    loop {
        let candidate = format!("{base}_{suffix}");
        if !operations.contains_key(&candidate) {
            return candidate;
        }
        suffix += 1;
    }
}

/// Derives a stable `send` operation id from the publishing handler's subscription name and
/// the target channel: `send_<name>_<channel>`.
fn send_operation_id(
    operations: &BTreeMap<String, Operation>,
    name: &str,
    channel: &str,
) -> String {
    let base = format!("send_{}_{}", sanitize_id(name), sanitize_id(channel));
    if !operations.contains_key(&base) {
        return base;
    }
    // A residual collision (several handlers on one subject publishing to one channel, or a
    // lossy sanitization) gets a deterministic numeric suffix instead of overwriting.
    let mut suffix = 2;
    loop {
        let candidate = format!("{base}_{suffix}");
        if !operations.contains_key(&candidate) {
            return candidate;
        }
        suffix += 1;
    }
}

/// Replaces every non-alphanumeric character with `_` for use in an operation id.
fn sanitize_id(name: &str) -> String {
    name.chars()
        .map(|c| if c.is_alphanumeric() { c } else { '_' })
        .collect()
}

#[cfg(test)]
mod tests {
    use crate::{ApiKeyLocation, HttpApiKeyLocation, SecurityScheme};

    use super::security_scheme_object;

    #[test]
    fn every_scheme_kind_renders_its_document_object() {
        let cases = [
            (
                SecurityScheme::user_password(),
                serde_json::json!({ "type": "userPassword" }),
            ),
            (
                SecurityScheme::api_key(ApiKeyLocation::Password),
                serde_json::json!({ "type": "apiKey", "in": "password" }),
            ),
            (
                SecurityScheme::x509(),
                serde_json::json!({ "type": "X509" }),
            ),
            (
                SecurityScheme::plain(),
                serde_json::json!({ "type": "plain" }),
            ),
            (
                SecurityScheme::scram_sha256(),
                serde_json::json!({ "type": "scramSha256" }),
            ),
            (
                SecurityScheme::scram_sha512(),
                serde_json::json!({ "type": "scramSha512" }),
            ),
            (
                SecurityScheme::gssapi(),
                serde_json::json!({ "type": "gssapi" }),
            ),
            (
                SecurityScheme::http("bearer"),
                serde_json::json!({ "type": "http", "scheme": "bearer" }),
            ),
            (
                SecurityScheme::http_api_key("X-Api-Key", HttpApiKeyLocation::Header),
                serde_json::json!({ "type": "httpApiKey", "name": "X-Api-Key", "in": "header" }),
            ),
            (
                SecurityScheme::open_id_connect("https://idp.example.com/.well-known"),
                serde_json::json!({
                    "type": "openIdConnect",
                    "openIdConnectUrl": "https://idp.example.com/.well-known",
                }),
            ),
            (
                SecurityScheme::oauth2(serde_json::json!({ "clientCredentials": {} })),
                serde_json::json!({ "type": "oauth2", "flows": { "clientCredentials": {} } }),
            ),
            (
                SecurityScheme::custom(serde_json::json!({ "type": "symmetricEncryption" })),
                serde_json::json!({ "type": "symmetricEncryption" }),
            ),
        ];
        for (scheme, expected) in cases {
            assert_eq!(security_scheme_object(&scheme), expected);
        }
    }
}