ruststream 0.7.0-rc.12

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
//! The reply wiring a mount site's chain builds up.
//!
//! A mount site names the broker's publish policy and then the knobs that policy alone does not
//! carry: the codec the reply encodes with, the static transform stacks that run on it, and
//! whether a batch's replies ride one broker transaction. Each step fills its own slot in the
//! wiring's type, so naming one twice is a compile error rather than a silent overwrite, and the
//! whole value stays pure declaration - it pairs with the connected broker at startup, like any
//! other [`PublishPolicy`].

use std::fmt;
use std::marker::PhantomData;

use super::{
    BatchPublishTransformStack, BatchTransformIdentity, CallCodec, PublishCodec,
    PublishTransformIdentity, PublishTransformStack, Transactional, TypedPublisher, UnnamedCodec,
};
#[cfg(feature = "asyncapi")]
use crate::asyncapi::Bindings;
use crate::{ConnectedBroker, PairError, PublishPolicy, TransactionalPublisher};

/// The publish policy of a byte-for-byte reply, as the mount chain carries it.
///
/// A [`Serialized`](super::Serialized) reply leaves with no codec and no transform stack, so its
/// wiring is the policy and nothing else; the newtype exists so the two wires are different type
/// constructors, which is what lets `.codec(..)`, `.transform(..)`, `.batch_transform(..)` and
/// `.transactional()` report "not on this wire" instead of "no such method". Machinery; the chain
/// builds it and the mount pairs it.
pub struct RawReplyWiring<Policy>(Policy);

impl<Policy> RawReplyWiring<Policy> {
    /// The wiring `.out(Reply, policy)` produces on the serialized wire.
    pub(crate) const fn new(policy: Policy) -> Self {
        Self(policy)
    }
}

impl<Policy> fmt::Debug for RawReplyWiring<Policy> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RawReplyWiring").finish_non_exhaustive()
    }
}

// The wiring is transparent to the pairing: the bytes leave through the policy's own live form.
impl<CB: ConnectedBroker, Policy: PublishPolicy<CB> + Send> PublishPolicy<CB>
    for RawReplyWiring<Policy>
{
    type Live = Policy::Live;

    async fn pair(self, connected: &CB) -> Result<Self::Live, PairError> {
        self.0.pair(connected).await
    }

    #[cfg(feature = "asyncapi")]
    fn channel_bindings(&self, channel: &str) -> Bindings {
        self.0.channel_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn operation_bindings(&self, channel: &str) -> Bindings {
        self.0.operation_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn message_bindings(&self, channel: &str) -> Bindings {
        self.0.message_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn reply_address_location(&self) -> Option<&'static str> {
        self.0.reply_address_location()
    }
}

/// Replaces the publish policy a wiring carries, keeping every step the chain already named:
/// the hook [`map_publisher`](crate::runtime::MapPublisher::map_publisher) - and so a broker
/// crate's own publisher settings - reaches the policy through.
///
/// Implemented for both reply wires and for one [`Out`](crate::runtime::Out) slot's attachment,
/// so a broker's settings trait is written once and applies wherever a policy is named.
#[doc(hidden)]
pub trait MapReplyPolicy: Sized {
    /// The policy the wiring carries.
    type Policy;

    /// Replaces it with one the broker's own settings produced.
    #[must_use]
    fn map_policy(self, f: impl FnOnce(Self::Policy) -> Self::Policy) -> Self;
}

impl<Policy, Enc, PL, BL, Tx> MapReplyPolicy for ReplyWiring<Policy, Enc, PL, BL, Tx> {
    type Policy = Policy;

    fn map_policy(self, f: impl FnOnce(Policy) -> Policy) -> Self {
        Self {
            policy: f(self.policy),
            enc: self.enc,
            layers: self.layers,
            batch_layers: self.batch_layers,
            _tx: PhantomData,
        }
    }
}

impl<Policy> MapReplyPolicy for RawReplyWiring<Policy> {
    type Policy = Policy;

    fn map_policy(self, f: impl FnOnce(Policy) -> Policy) -> Self {
        Self(f(self.0))
    }
}

/// The transaction state a wiring starts in: each reply publishes on its own.
#[derive(Debug, Clone, Copy, Default)]
pub struct Direct;

/// The transaction state [`transactional`](TransactionalReply::into_transactional) moves a wiring
/// to: a batch's replies publish inside one broker transaction.
#[derive(Debug, Clone, Copy, Default)]
pub struct InTransaction;

/// A reply publish policy with the mount site's chain steps folded into its type.
///
/// Built by `.out(Reply, <policy>)` on a mount site's chain and grown by `.codec(..)`,
/// `.transform(..)`, `.batch_transform(..)` and `.transactional()`; the runtime pairs it with the
/// connected broker at startup, which is where the policy becomes a live publisher and the
/// wiring becomes the reply sink the dispatch publishes through. Machinery: the chain builds it
/// and the mount consumes it, so it is never named in user code.
pub struct ReplyWiring<
    Policy,
    Enc = UnnamedCodec,
    PL = PublishTransformIdentity,
    BL = BatchTransformIdentity,
    Tx = Direct,
> {
    policy: Policy,
    enc: Enc,
    layers: PL,
    batch_layers: BL,
    _tx: PhantomData<fn() -> Tx>,
}

impl<Policy> ReplyWiring<Policy> {
    /// The wiring a bare `.out(Reply, policy)` produces: no codec named (the default applies), no
    /// transforms, one broker call per reply.
    pub(crate) fn new(policy: Policy) -> Self {
        Self {
            policy,
            enc: UnnamedCodec::new(),
            layers: PublishTransformIdentity,
            batch_layers: BatchTransformIdentity,
            _tx: PhantomData,
        }
    }
}

impl<Policy, Enc, PL, BL, Tx> fmt::Debug for ReplyWiring<Policy, Enc, PL, BL, Tx> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ReplyWiring").finish_non_exhaustive()
    }
}

/// Naming the reply codec: the `.codec(..)` step of a mount site's chain.
///
/// Implemented for every reply wiring and for nothing else, so the call on a byte-for-byte reply
/// - which carries the publish policy alone and has no codec slot at all - fails here. Whether
/// the slot is still open is the separate question [`CodecSlotOpen`] answers, so that a second
/// `.codec(..)` reports the slot rather than the whole wiring.
#[doc(hidden)]
#[diagnostic::on_unimplemented(
    message = "`{Self}` does not take a reply codec",
    label = "this reply's codec cannot be named here",
    note = "`.codec(..)` names an encoded reply's codec, right after `.out(Reply, ..)`; a \
            `Serialized` reply carries its own bytes and takes no codec at all"
)]
pub trait NameReplyCodec<C> {
    /// The codec slot the wiring holds right now: [`UnnamedCodec`] until a `.codec(..)` fills it.
    type Slot;

    /// The wiring with the codec named.
    type Out;

    /// Names it.
    fn name_codec(self, codec: C) -> Self::Out;
}

impl<Policy, Enc, PL, BL, Tx, C> NameReplyCodec<C> for ReplyWiring<Policy, Enc, PL, BL, Tx> {
    type Slot = Enc;
    type Out = ReplyWiring<Policy, CallCodec<C>, PL, BL, Tx>;

    fn name_codec(self, codec: C) -> Self::Out {
        ReplyWiring {
            policy: self.policy,
            enc: CallCodec(codec),
            layers: self.layers,
            batch_layers: self.batch_layers,
            _tx: PhantomData,
        }
    }
}

/// A reply codec slot still open: what a `.codec(..)` step fills.
///
/// The step states this about the slot it is about to fill
/// ([`NameReplyCodec::Slot`]) rather than about the wiring, so a second `.codec(..)` fails on the
/// slot the first one already took - and this message, not a bare "no method", is what the call
/// site reads.
#[doc(hidden)]
#[diagnostic::on_unimplemented(
    message = "this reply's codec is already named",
    label = "`.codec(..)` fills the reply's codec slot, and it is filled",
    note = "a reply encodes with exactly one codec: drop one of the `.codec(..)` calls"
)]
pub trait CodecSlotOpen {}

impl CodecSlotOpen for UnnamedCodec {}

/// Composing a static [`PublishTransform`](super::PublishTransform) onto the reply: the
/// `.transform(..)` step. The stack grows, so the step repeats; the first one added runs first
/// (closest to the encoded value).
#[doc(hidden)]
#[diagnostic::on_unimplemented(
    message = "`{Self}` does not take a publish transform",
    label = "this reply has no transform stack",
    note = "`.transform(..)` composes a `PublishTransform` onto an encoded reply, right after \
            `.out(Reply, ..)`; a `Serialized` reply's bytes leave as they are"
)]
pub trait AddReplyTransform<N> {
    /// The wiring with the transform on top of its stack.
    type Out;

    /// Composes it.
    fn add_transform(self, transform: N) -> Self::Out;
}

impl<Policy, Enc, PL, BL, Tx, N> AddReplyTransform<N> for ReplyWiring<Policy, Enc, PL, BL, Tx> {
    type Out = ReplyWiring<Policy, Enc, PublishTransformStack<PL, N>, BL, Tx>;

    fn add_transform(self, transform: N) -> Self::Out {
        ReplyWiring {
            policy: self.policy,
            enc: self.enc,
            layers: PublishTransformStack {
                inner: self.layers,
                outer: transform,
            },
            batch_layers: self.batch_layers,
            _tx: PhantomData,
        }
    }
}

/// Composing a [`BatchPublishTransform`](super::BatchPublishTransform) onto a batch's replies: the
/// `.batch_transform(..)` step. It runs on the batch path only; a per-message transform wanted on
/// both is added to each, reused here through [`for_batch`](super::for_batch).
#[doc(hidden)]
#[diagnostic::on_unimplemented(
    message = "`{Self}` does not take a batch publish transform",
    label = "this reply has no batch transform stack",
    note = "`.batch_transform(..)` composes a `BatchPublishTransform` onto an encoded batch reply \
            (`&[T]` plus `reply(..)`), right after `.out(Reply, ..)`"
)]
pub trait AddBatchReplyTransform<N> {
    /// The wiring with the transform on top of its batch stack.
    type Out;

    /// Composes it.
    fn add_batch_transform(self, transform: N) -> Self::Out;
}

impl<Policy, Enc, PL, BL, Tx, N> AddBatchReplyTransform<N>
    for ReplyWiring<Policy, Enc, PL, BL, Tx>
{
    type Out = ReplyWiring<Policy, Enc, PL, BatchPublishTransformStack<BL, N>, Tx>;

    fn add_batch_transform(self, transform: N) -> Self::Out {
        ReplyWiring {
            policy: self.policy,
            enc: self.enc,
            layers: self.layers,
            batch_layers: BatchPublishTransformStack {
                inner: self.batch_layers,
                outer: transform,
            },
            _tx: PhantomData,
        }
    }
}

/// Wrapping a batch's replies in one broker transaction: the `.transactional()` step.
///
/// Implemented for every reply wiring and for nothing else, so the call on a byte-for-byte reply
/// fails here. Whether the wiring still publishes directly is the separate question
/// [`PublishingDirectly`] answers, and that the policy's live publisher actually is a
/// [`TransactionalPublisher`](crate::TransactionalPublisher) is checked where the wiring pairs,
/// against the broker the mount site names.
#[doc(hidden)]
#[diagnostic::on_unimplemented(
    message = "`{Self}` cannot publish its replies inside a transaction",
    label = "this reply has no transaction to open",
    note = "`.transactional()` marks an encoded batch reply's wiring, right after \
            `.out(Reply, ..)`; the policy it marks must pair into a `TransactionalPublisher`"
)]
pub trait TransactionalReply {
    /// How the wiring publishes right now: [`Direct`] until a `.transactional()` marks it.
    type State;

    /// The wiring publishing inside a transaction.
    type Out;

    /// Marks it.
    fn into_transactional(self) -> Self::Out;
}

impl<Policy, Enc, PL, BL, Tx> TransactionalReply for ReplyWiring<Policy, Enc, PL, BL, Tx> {
    type State = Tx;
    type Out = ReplyWiring<Policy, Enc, PL, BL, InTransaction>;

    fn into_transactional(self) -> Self::Out {
        ReplyWiring {
            policy: self.policy,
            enc: self.enc,
            layers: self.layers,
            batch_layers: self.batch_layers,
            _tx: PhantomData,
        }
    }
}

/// A wiring still publishing each reply on its own: what a `.transactional()` step marks.
///
/// Stated about the publish state ([`TransactionalReply::State`]) rather than about the wiring,
/// so a second `.transactional()` reports the mark the first one already made.
#[doc(hidden)]
#[diagnostic::on_unimplemented(
    message = "this reply already publishes inside a transaction",
    label = "`.transactional()` marks the reply's publish state, and it is marked",
    note = "a batch's replies ride one transaction: drop one of the `.transactional()` calls"
)]
pub trait PublishingDirectly {}

impl PublishingDirectly for Direct {}

// A wiring is a policy over a policy: pairing swaps the leaf for its live form and resolves the
// codec position, while the transform stacks travel unchanged.
impl<CB, Policy, Enc, PL, BL> PublishPolicy<CB> for ReplyWiring<Policy, Enc, PL, BL, Direct>
where
    CB: ConnectedBroker,
    Policy: PublishPolicy<CB> + Send,
    Enc: PublishCodec<Codec: Clone> + Send,
    PL: Send,
    BL: Send,
{
    type Live = TypedPublisher<Policy::Live, Enc::Codec, PL, BL>;

    async fn pair(self, connected: &CB) -> Result<Self::Live, PairError> {
        let codec = self.enc.codec().clone();
        Ok(TypedPublisher::live(
            self.policy.pair(connected).await?,
            codec,
            self.layers,
            self.batch_layers,
        ))
    }

    #[cfg(feature = "asyncapi")]
    fn channel_bindings(&self, channel: &str) -> Bindings {
        self.policy.channel_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn operation_bindings(&self, channel: &str) -> Bindings {
        self.policy.operation_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn message_bindings(&self, channel: &str) -> Bindings {
        self.policy.message_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn reply_address_location(&self) -> Option<&'static str> {
        self.policy.reply_address_location()
    }
}

// The transactional wiring pairs into the transactional reply sink, which is where the leaf's
// live form has to carry broker transactions - so a broker without them fails at the mount that
// named `.transactional()`, not at the step.
impl<CB, Policy, Enc, PL, BL> PublishPolicy<CB> for ReplyWiring<Policy, Enc, PL, BL, InTransaction>
where
    CB: ConnectedBroker,
    Policy: PublishPolicy<CB> + Send,
    Policy::Live: TransactionalPublisher,
    Enc: PublishCodec<Codec: Clone> + Send,
    PL: Send,
    BL: Send,
{
    type Live = Transactional<Policy::Live, Enc::Codec, PL, BL>;

    async fn pair(self, connected: &CB) -> Result<Self::Live, PairError> {
        let codec = self.enc.codec().clone();
        Ok(Transactional::live(TypedPublisher::live(
            self.policy.pair(connected).await?,
            codec,
            self.layers,
            self.batch_layers,
        )))
    }

    #[cfg(feature = "asyncapi")]
    fn channel_bindings(&self, channel: &str) -> Bindings {
        self.policy.channel_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn operation_bindings(&self, channel: &str) -> Bindings {
        self.policy.operation_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn message_bindings(&self, channel: &str) -> Bindings {
        self.policy.message_bindings(channel)
    }

    #[cfg(feature = "asyncapi")]
    fn reply_address_location(&self) -> Option<&'static str> {
        self.policy.reply_address_location()
    }
}

#[cfg(all(test, feature = "memory", feature = "json", feature = "asyncapi"))]
mod tests {
    use std::collections::BTreeMap;
    use std::future::Future;

    use super::{RawReplyWiring, ReplyWiring, TransactionalReply};
    use crate::asyncapi::{Binding, Bindings};
    use crate::memory::{ConnectedMemoryBroker, MemoryPublish, MemoryPublisher};
    use crate::runtime::publish::{Names, NarrowToUse};
    use crate::{PairError, PublishPolicy};

    /// One binding under `protocol` whose body is the channel the hook was asked about, so a
    /// wrapper that drops a level or passes a name of its own is visible.
    fn one(protocol: &'static str, channel: &str) -> Bindings {
        let body = BTreeMap::from([("channel".to_owned(), channel.to_owned())]);
        Bindings::new().with(Binding::new(protocol, "0.1.0", &body).expect("a listed protocol"))
    }

    /// A policy that answers every document hook with a different protocol, so a wrapper that
    /// crosses two levels or drops one is visible.
    #[derive(Clone, Copy)]
    struct Described;

    impl PublishPolicy<ConnectedMemoryBroker> for Described {
        type Live = MemoryPublisher;

        fn pair(
            self,
            connected: &ConnectedMemoryBroker,
        ) -> impl Future<Output = Result<Self::Live, PairError>> {
            <MemoryPublish as PublishPolicy<ConnectedMemoryBroker>>::pair(MemoryPublish, connected)
        }

        fn channel_bindings(&self, channel: &str) -> Bindings {
            one("nats", channel)
        }

        fn operation_bindings(&self, channel: &str) -> Bindings {
            one("kafka", channel)
        }

        fn message_bindings(&self, channel: &str) -> Bindings {
            one("mqtt", channel)
        }

        fn reply_address_location(&self) -> Option<&'static str> {
            Some("$message.header#/reply-to")
        }
    }

    fn assert_forwards<P: PublishPolicy<ConnectedMemoryBroker>>(wrapper: &P) {
        assert_eq!(wrapper.channel_bindings("orders"), one("nats", "orders"));
        assert_eq!(wrapper.operation_bindings("orders"), one("kafka", "orders"));
        assert_eq!(wrapper.message_bindings("orders"), one("mqtt", "orders"));
        assert_eq!(
            wrapper.reply_address_location(),
            Some("$message.header#/reply-to"),
        );
    }

    /// Every step a mount chain adds wraps the policy, and none of them may swallow what the
    /// policy says about itself: the document is built from the wrapper the chain ended on. The
    /// resolved destination travels the same way, unchanged by any step.
    #[test]
    fn every_wrapper_forwards_what_the_leaf_policy_says() {
        assert_forwards(&RawReplyWiring::new(Described));
        assert_forwards(&ReplyWiring::new(Described));
        assert_forwards(&ReplyWiring::new(Described).into_transactional());
        // What a naming transform narrows the policy to, on a slot and on the retry position.
        assert_forwards(&<Names as NarrowToUse<Described>>::narrow(Described));
    }
}