Ниже показан английский оригинал.
Message sets
A subscription over an InMessages set lists one message per variant, each with its own name, description, headers and content type.
The receive operation names every model of the set, and the channel carries one message component per model.
A serde model reports the content type of the codec of the mount and its payload schema; a model that reads its own bytes reports neither, by design, and is not counted by messages_without_schema.
Under the header rule each message's headers schema carries the header with its const value, the counterpart of an OpenAPI discriminator mapping, so a reader of the document knows which header picks which message.
A model with a header contract of its own keeps it, and the rule's header joins its properties; a Headers<T> parameter of the handler joins every model's schema the same way.
A model that two sets name with different header values keeps the first value in the document, and generation logs a warning, as for two header contracts on one type.
A dead-letter or retry destination of such a subscription lists the same models.
The example runs with cargo test --example in_messages --features testing,macros,memory,asyncapi.
Example
- Macros
- Manual
/// Every event of an order on the `orders` subscription: the `event` header names the model.
#[derive(InMessages)]
#[in_messages(header = "event")]
enum OrderEvent {
#[in_messages(value = "created")]
Created(OrderCreated),
#[in_messages(value = "cancelled")]
Cancelled(OrderCancelled),
}
/// Every event of an order on the `orders` subscription: the `event` header names the model.
enum OrderEvent {
Created(OrderCreated),
Cancelled(OrderCancelled),
}
/// The kinds of the set, what a discriminator names; the derive generates this enum.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum OrderEventKind {
Created,
Cancelled,
}
impl<DecodeCodec: Codec> InMessages<DecodeCodec> for OrderEvent {
type Kind = OrderEventKind;
fn decode<M: IncomingMessage>(codec: &DecodeCodec, msg: &M) -> Result<Self, CodecError> {
let kind = match msg.headers().get("event") {
Some(b"created") => OrderEventKind::Created,
Some(b"cancelled") => OrderEventKind::Cancelled,
Some(other) => {
return Err(CodecError::Decode(
format!(
"the `event` header is `{}`, which names no variant of `OrderEvent`",
String::from_utf8_lossy(other)
)
.into(),
));
}
None => {
return Err(CodecError::Decode(
"the delivery carries no `event` header".into(),
));
}
};
// The chosen variant decodes once, through its model's own path.
let payload = msg.payload();
match kind {
OrderEventKind::Created => {
<OrderCreated as InVariant<Self>>::decode_variant(codec, payload)
.map(OrderEvent::Created)
}
OrderEventKind::Cancelled => {
<OrderCancelled as InVariant<Self>>::decode_variant(codec, payload)
.map(OrderEvent::Cancelled)
}
}
}
// One entry per model, in the order of the variants, each with the header value that names
// it; the document lists them as the messages of the subscription.
fn incoming() -> Vec<IncomingMessageMetadata> {
vec![
<OrderCreated as InVariant<Self>>::describe()
.with_discriminator(HeaderDiscriminator::new("event", "created")),
<OrderCancelled as InVariant<Self>>::describe()
.with_discriminator(HeaderDiscriminator::new("event", "cancelled")),
]
}
}
impl Input for OrderEvent {
type Axis = SoloSet<Self>;
}