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
//! The contract a `#[subscriber]`-generated type implements so it can be mounted with
//! [`BrokerScope::include`](super::BrokerScope::include).
use super::dispatch::Workers;
use super::failure::FailurePolicies;
use super::input::InputKind;
use super::metadata::HandlerMetadata;
/// A handler definition produced by the `#[subscriber]` macro (with or without `raw`).
///
/// It bundles a [`Handler`](super::Handler) with the subscription [`Source`](Self::Source) it
/// binds to, so [`BrokerScope::include`](super::BrokerScope::include) can subscribe and wire
/// decoding without the caller repeating anything. The source is a [`Name`](crate::Name) for
/// `#[subscriber("topic")]`, or a broker descriptor for `#[subscriber(RedisStream::new(..))]`. The
/// generated type is itself the handler, so [`Handler`](Self::Handler) is usually `Self`.
pub trait SubscriberDef: Sized {
/// The input kind the handler consumes ([`Decoded<T>`](super::Decoded) for a typed `&T`
/// parameter, [`RawBytes`](super::RawBytes) for a raw `&[u8]` one).
type Input: InputKind;
/// The broker's typed per-delivery context the handler reads by key (`()` when the handler
/// names no context type).
type Context;
/// The concrete handler type over the input's borrowed target (`Handler<T>` for a
/// `Decoded<T>` input, `Handler<[u8]>` for a raw one) and [`Context`](Self::Context).
///
/// The handler bound is enforced where the def is mounted (against the app's state type `St`),
/// not on the trait: a handler that reads typed application state is
/// [`Handler<Target, Context, St>`](super::Handler) only for its declared `St`, while one that
/// ignores state is generic over it.
type Handler;
/// The subscription source this handler binds to. The bound to
/// [`SubscriptionSource`](crate::SubscriptionSource) for the target broker is applied where the
/// def is mounted, not on the trait, so a def can name any broker's descriptor.
type Source;
/// Builds the subscription source (fresh each call).
fn source(&self) -> Self::Source;
/// An optional human description (from the handler's doc comment), for `AsyncAPI`.
fn description(&self) -> Option<&str> {
None
}
/// The input type's serialized JSON Schema, when it implements [`schemars::JsonSchema`] and the
/// `asyncapi` feature is on. The macro fills this in; the default omits it.
fn input_schema(&self) -> Option<String> {
None
}
/// The serialized JSON Schema of the handler's typed header contract (its
/// [`Headers<T>`](super::Headers) parameter), when `T` implements
/// [`schemars::JsonSchema`] and the `asyncapi` feature is on. The macro fills this in; the
/// default omits it.
fn headers_schema(&self) -> Option<String> {
None
}
/// The input type's [`Message`](crate::MessageInfo) name, when it implements that trait. The macro
/// fills this in; the default omits it.
fn message_name(&self) -> Option<&'static str> {
None
}
/// The input type's [`Message`](crate::MessageInfo) description, when it implements that trait.
/// The macro fills this in; the default omits it.
fn message_description(&self) -> Option<&'static str> {
None
}
/// The concurrency policy for this subscriber's dispatch loop. The macro fills this in from
/// the `workers(..)` argument; the default is sequential dispatch.
fn workers(&self) -> Workers {
Workers::sequential()
}
/// The failure policy for a handler panic and a decode failure. The macro fills this in from
/// the `on_failure(panic = .., decode = ..)` argument; the default fails fast on a panic and
/// drops on a decode failure.
fn failure_policies(&self) -> FailurePolicies {
FailurePolicies::default()
}
/// Consumes the definition, returning the handler.
fn into_handler(self) -> Self::Handler;
}
/// Builds the registration metadata for a subscriber definition mounted under `name`.
pub(crate) fn subscriber_metadata<D: SubscriberDef>(name: String, def: &D) -> HandlerMetadata {
let mut meta = HandlerMetadata::raw(name).with_def_details(
def.description(),
def.input_schema(),
def.headers_schema(),
def.message_name(),
def.message_description(),
);
meta.input_type = <D::Input as InputKind>::input_label();
meta.deserialized = <D::Input as InputKind>::DESERIALIZED;
meta
}
#[cfg(test)]
mod tests {
use std::future::ready;
use super::{SubscriberDef, subscriber_metadata};
use crate::HeaderMap;
use crate::Name;
use crate::runtime::context::Context;
use crate::runtime::dispatch::{Delivery, Workers};
use crate::runtime::handler::{Handler, HandlerOutcome};
use crate::runtime::input::{Decoded, Provided};
struct Noop;
impl Handler<u32> for Noop {
fn handle(
&self,
_msg: &u32,
_ctx: &mut Context<'_>,
) -> impl Future<Output = HandlerOutcome> {
ready(HandlerOutcome::ack())
}
}
/// A hand-written def overriding nothing: pins the trait's default contract, which the
/// macro-generated defs always override.
struct ManualDef;
impl SubscriberDef for ManualDef {
type Input = Decoded<u32>;
type Context = ();
type Handler = Noop;
type Source = Name;
fn source(&self) -> Name {
Name::new("manual")
}
fn into_handler(self) -> Noop {
Noop
}
}
/// A frame family standing in for a `Deserialized` input on the low-level def.
struct Frame;
/// A raw def: the same trait with the self-deserializing input kind; pins that the metadata
/// label is the family type's own name.
struct ManualRawDef;
impl SubscriberDef for ManualRawDef {
type Input = Provided<Frame>;
type Context = ();
type Handler = ();
type Source = Name;
fn source(&self) -> Name {
Name::new("frames")
}
fn into_handler(self) {}
}
#[test]
fn a_raw_def_reports_the_family_label() {
let def = ManualRawDef;
let meta = subscriber_metadata("frames".to_owned(), &def);
assert_eq!(meta.name, "frames");
assert!(meta.input_type.ends_with("Frame"), "{}", meta.input_type);
assert!(meta.payload_schema.is_none());
def.into_handler();
}
#[tokio::test]
async fn defaults_omit_metadata_and_dispatch_sequentially() {
let def = ManualDef;
assert_eq!(def.workers(), Workers::sequential());
assert!(def.description().is_none());
assert!(def.input_schema().is_none());
assert!(def.message_name().is_none());
assert!(def.message_description().is_none());
let _source = def.source();
let meta = subscriber_metadata("manual".to_owned(), &def);
assert_eq!(meta.name, "manual");
assert_eq!(meta.input_type, "u32");
// Drive the consumed handler so source(), into_handler() and the Noop body are exercised.
let handler = def.into_handler();
let state = ();
let delivery = Delivery::empty();
let headers = HeaderMap::new();
let mut ctx = Context::new("manual", &headers, &state, (), &delivery);
assert!(handler.handle(&7u32, &mut ctx).await.is_ack());
}
}