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
//! Rust core of the [`RustStream`](https://github.com/powersemmi/ruststream) messaging
//! framework: broker-agnostic traits, message types, codecs, router runtime, and a
//! conformance harness for broker authors.
//!
//! # A service
//!
//! A handler is an `async fn` over a decoded payload. The application object mounts it on a
//! broker, and the attribute writes the `main`:
//!
//! ```
//! # #[cfg(all(feature = "macros", feature = "memory", feature = "json"))]
//! # mod demo {
//! use ruststream::memory::prelude::*;
//! use serde::Deserialize;
//!
//! #[derive(Deserialize)]
//! struct Order {
//! id: u64,
//! }
//!
//! #[subscriber("orders")]
//! async fn handle(order: &Order) -> HandlerOutcome {
//! println!("got order {}", order.id);
//! HandlerOutcome::ack()
//! }
//!
//! #[ruststream::app]
//! fn app() -> RustStream {
//! RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
//! b.include(handle);
//! })
//! }
//! # }
//! # fn main() {}
//! ```
//!
//! `cargo run -- run` starts it. The broker here is the in-process one; a real broker is a
//! crate of its own with the same shape, and its prelude replaces `memory::prelude`.
//!
//! # Where things are
//!
//! * [`runtime`]: everything a service is made of. Subscribers and what a handler may take,
//! replies and `Out` slots, routers, the per-delivery context and the shared state, typed
//! headers, lifecycle hooks, middleware, failure policies, running beside another server.
//! * [`codec`]: how payload bytes become values and back, where a codec is chosen, and the byte
//! lanes that need none.
//! * [`memory`]: the in-process broker, a real broker and the reference implementation.
//! * [`testing`]: testing the service's own app, in process or against a running broker.
//! * [`asyncapi`]: the generated document. [`metrics`], [`logging`], [`otel`]: observability.
//! * [`runtime::cli`]: the generated entry point and the `ruststream` command.
//! * [`conformance`]: the contract suite a broker crate runs against itself.
//!
//! The broker crates document their own descriptors and publish policies. Installation, the
//! tutorial and the list of brokers are on the site:
//! <https://powersemmi.github.io/ruststream/>.
//!
//! # Cargo features
//!
//! The core traits, the [`runtime::RustStream`] application object, middleware, and dispatch are
//! always present. The rest is additive and opt-in. Codec features are mutually compatible and
//! enable only the deserializers you need.
//!
//! * `json` (default): [`codec::JsonCodec`].
//! * `msgpack`: [`codec::MsgpackCodec`].
//! * `cbor`: [`codec::CborCodec`].
//! * `memory`: [`memory::MemoryBroker`], an in-process broker usable in applications, prototypes
//! and tests.
//! * `macros`: the `#[subscriber]`, [`#[ruststream::app]`](macro@app),
//! [`#[derive(Outgoing)]`](macro@Outgoing) and [`#[derive(MessageInfo)]`](macro@MessageInfo)
//! macros.
//! * `asyncapi`: `AsyncAPI` document generation and the HTML viewer.
//! * `metrics`: Prometheus metrics middleware and exporter.
//! * `logging`: colored, `RUST_LOG`-driven console logging via `tracing-subscriber`
//! ([`logging::init`]). The generated `cli` `run` command installs it automatically.
//! * `otel`: OpenTelemetry SDK integration: OTLP export for traces and metrics via
//! [`otel::OtelBuilder::init`], plus per-handler dispatch metrics middleware and W3C
//! trace-context propagation ([`otel::propagation`]).
//! * `conformance`: the [`conformance::harness`] contract suite, per-capability suites in
//! [`conformance::capabilities`], the in-process transport held to the server in
//! [`conformance::in_process`], the checks of what a message carries in
//! [`conformance::message_shape`], and broker-agnostic [`conformance::helpers`] for application
//! tests. Generic over any broker's [`testing::TestableBroker`], so it pulls in no concrete broker
//! (enable `memory` too to run it against [`memory::MemoryBroker`]).
//! * `testing`: the [`testing::TestApp`] harness, which runs the service's production app in
//! process or against running brokers.
//! * `cli`: the `ruststream` binary (`run`, `asyncapi gen`, `new`).
//! * `poll-diagnostics`: every subscription samples the time its handler spends in `poll` and
//! warns when a handler computes on the app's runtime ([`runtime::PollDiagnostics`]).
//!
//! Disable defaults (`default-features = false`) to drop the bundled JSON codec; the core traits,
//! runtime, and dispatch remain. Add back only what you need.
//!
//! # Getting the imports
//!
//! [`prelude`] carries what a service writes every time (the application object, the handler
//! surface, the extractor parameters, the macros): `use ruststream::prelude::*;`. The broker and
//! the codec stay explicit, since those are the choices a service makes for itself.
/// Re-exported for the [`register_testable_broker!`] macro's expansion; not a stable API.
pub use inventory;
/// The buffer the crate's byte-producing surfaces speak: what a [`Codec`](codec::Codec) returns,
/// what a [`Serialized`](runtime::Serialized) value writes into, and the payload a publisher
/// that declared [`Take`] is handed.
///
/// Re-exported so that implementing either trait, or taking a published payload, does not also
/// mean depending on `bytes` directly; a service that already does keeps its own import, which
/// shadows this one.
pub use BytesMut;
/// The shared byte buffer the message path hands around: a payload, a header value taken with
/// [`HeaderMap::get_shared`].
///
/// Re-exported for the same reason as [`BytesMut`]: reading a value out of a header map, or
/// handing one to a publisher, should not also mean depending on `bytes` directly.
pub use Bytes;
/// The owned string of the message path: a destination name, a header key.
///
/// A `Bytes` that is valid UTF-8, so it reads as a `str`, clones by reference count and can be
/// taken as a slice of a buffer the broker already holds. `Str::from_static("orders")` costs
/// nothing at all; `String` moves in without a copy; `&str` copies once.
pub use Str;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use HeaderMap;
pub use ;
pub use ;
pub use ;
pub use Subscriber;
pub use ;
pub use ;
pub use RustStream;
/// Attribute macro that turns an `async fn` into a mountable subscriber definition.
///
/// Available with the `macros` feature. See [`ruststream_macros::subscriber`].
pub use subscriber;
/// Attribute macro that generates a `main` entry point from a `RustStream` builder function.
///
/// Available with the `macros` feature. See [`ruststream_macros::app`] and
/// [`runtime::cli`].
pub use app;
/// Derive macro for [`MessageInfo`] metadata (type name + doc description).
///
/// Available with the `macros` feature.
pub use MessageInfo;
/// Derive macro declaring everything a message type says about being sent: its destination
/// ([`OutgoingDestination`]) and its optional header contract ([`MessageHeaders`]), plus the
/// [`MessageInfo`] metadata the generated document reads.
///
/// Available with the `macros` feature.
pub use Outgoing;
/// Derive macro that implements [`FromRef`](runtime::FromRef) for each field of an application
/// state, so handlers can inject any field with [`State<T>`](runtime::State).
///
/// Available with the `macros` feature.
pub use FromRef;
/// Derive macro that makes a unit struct a slot marker ([`OutSlot`]) for
/// `Out<impl Publisher, Marker>` handler parameters.
///
/// Available with the `macros` feature.
pub use OutSlot;
/// Derive macro for a declared message set: an enum whose variants each wrap one message
/// model, named as the third `Out` argument (`Out<impl Publisher, Marker, SendSet>`). The enum
/// is a type-level declaration and is never constructed.
///
/// Available with the `macros` feature.
pub use OutMessages;
/// Derive macro for a self-deserializing input type
/// ([`Deserialized`](runtime::Deserialized)): a newtype or single-field struct over `&'a [u8]`
/// gains the construction and the [`Input`](runtime::Input) spelling; the batch spelling
/// (`&[Frame<'_>]`) follows from it.
///
/// Available with the `macros` feature.
pub use Deserialized;
/// Derive macro for a self-serialized outgoing type ([`Serialized`](runtime::Serialized)): a
/// newtype or single-field struct over a byte buffer gains the bytes accessor and the wire
/// spellings that route it onto the serialized wire ([`MessageWire`](runtime::MessageWire) for
/// a typed publish, [`ReplyShape`](runtime::ReplyShape) for the reply position).
///
/// Available with the `macros` feature.
pub use Serialized;
// The trait shares the derive's name at the root (the serde pattern), so one import serves
// both the `#[derive(OutSlot)]` and a broker's blanket impl bound.
pub use OutSlot;
/// Re-export of [`schemars`] so message types can derive `JsonSchema` without a direct dependency.
///
/// Derive it on a message type (`#[derive(ruststream::schemars::JsonSchema)]`) and its payload
/// schema is emitted into the generated [`AsyncAPI`](asyncapi) document. Available with the
/// `asyncapi` feature.
pub use schemars;
/// Implementation detail used by the `#[subscriber]` macro to capture a payload's JSON Schema.
///
/// Not part of the public API; no stability guarantees.
/// Builds a [`NonZero`](core::num::NonZero) integer from a literal, rejecting zero at compile
/// time.
///
/// The expansion is an inline `const` block, so `nonzero!(0)` fails the build instead of
/// panicking at runtime, and the `NonZero` width is inferred from the call site - the same
/// literal works for a batch size (`NonZeroUsize`) and any other `NonZero` parameter.
///
/// # Examples
///
/// ```
/// use ruststream::nonzero;
///
/// let batch: core::num::NonZeroUsize = nonzero!(128);
/// # let _ = batch;
/// ```
///
/// Zero does not compile:
///
/// ```compile_fail
/// let _: core::num::NonZeroUsize = ruststream::nonzero!(0);
/// ```