ruststream 0.7.3

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
//! The publishing forms from the Publishing guide, written without the `macros` feature. The
//! attribute and the derives are sugar over public value constructors and traits, so every
//! declaration they mint is written out here: the reply bodies, the slot markers and their
//! dictionaries, and what a message type says about being sent. Everything a body does is an axis
//! of its own `impl Handle`, and the mount site then reads exactly as it does with the attribute -
//! `include`, `.out_reply(..)`, `.out(marker, ..)`, `.build()`.
//!
//! ```text
//! cargo run --example manual_publishing --no-default-features --features memory,json
//! ```

use std::error::Error;
use std::fmt::Display;
use std::future::{Future, ready};

use ruststream::memory::prelude::*;
use ruststream::runtime::{
    BoundSegment, ContextKind, MissingSegment, PublishAt, PublishError, PublishLayer, PublishNext,
    PublishPipeline, PublishTransform, Reads, TemplateAddress,
};
// The derive and the pipeline's message type share the name in different namespaces: the derive
// is the macro `ruststream::Outgoing`, the value flowing through a publish transform is the type
// `ruststream::runtime::Outgoing`.
use ruststream::runtime::Outgoing;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct Request {
    id: u64,
}

#[derive(Debug, Serialize, schemars::JsonSchema)]
struct Response {
    ok: bool,
}

// A reply the mount site sends where it says: the destination form is the one that leaves the
// name to the chain.
impl OutgoingDestination for Response {
    type Form = CallerName;
}

impl MessageHeaders for Response {
    type Contract = NoHeaders;
}

/// An event this service sends wherever the call site says.
#[derive(Debug, Deserialize, Serialize, schemars::JsonSchema)]
struct Event {
    id: u64,
}

// `#[derive(Outgoing)]` with no `name`, by hand: the destination form is the one that leaves the
// name to the call, so the publish builder offers `to(..)` and nothing is declared as leaving.
impl OutgoingDestination for Event {
    type Form = CallerName;
}

impl MessageHeaders for Event {
    type Contract = NoHeaders;
}

impl<M: OutSlot> OutMessages<M> for Event {
    fn outgoing() -> Vec<OutgoingMessageMetadata> {
        Vec::new()
    }
}

// --8<-- [start:reply_declared]
// The reply type fixes its destination, so the chain names none.
#[derive(Debug, Serialize, schemars::JsonSchema)]
struct Receipt {
    id: u64,
}

impl OutgoingDestination for Receipt {
    type Form = FixedName;

    const DESTINATION: &'static str = "receipts";
}

impl MessageHeaders for Receipt {
    type Contract = NoHeaders;
}

struct IssueReceipt;

impl Handle<Request, Receipt> for IssueReceipt {
    fn handle(
        &self,
        req: &Request,
        _outs: &(),
        _ctx: &mut Context<'_>,
    ) -> impl Future<Output = Result<Receipt, HandlerOutcome>> {
        ready(Ok(Receipt { id: req.id }))
    }
}
// --8<-- [end:reply_declared]

// --8<-- [start:reply]
// A replying handler is a body producing a reply: the reply type is the second axis of
// `Handle`, and the chain names the subscription, the destination and the publisher the reply
// leaves through.
struct Respond;

impl Handle<Request, Response> for Respond {
    fn handle(
        &self,
        req: &Request,
        _outs: &(),
        _ctx: &mut Context<'_>,
    ) -> impl Future<Output = Result<Response, HandlerOutcome>> {
        println!("responding to request {}", req.id);
        ready(Ok(Response { ok: true }))
    }
}
// --8<-- [end:reply]

// --8<-- [start:reply_result]
// `Ok` publishes the reply and acks; `Err` publishes nothing and the dispatcher acts on the
// returned HandlerOutcome (here: drop the malformed request instead of replying).
struct Validate;

impl Handle<Request, Response> for Validate {
    fn handle(
        &self,
        req: &Request,
        _outs: &(),
        _ctx: &mut Context<'_>,
    ) -> impl Future<Output = Result<Response, HandlerOutcome>> {
        if req.id == 0 {
            return ready(Err(HandlerOutcome::drop()));
        }
        ready(Ok(Response { ok: true }))
    }
}
// --8<-- [end:reply_result]

// --8<-- [start:forward]
// The publisher arrives as an injection: the policy is attached at the include site, the runtime
// pairs it with the connected broker at startup, and the body always holds a live publisher - no
// registry, no erased lookup, no state plumbing. Nothing the mount site chose is named here: one
// parameter stands for the slot's entry, `OutEntry` ties it to the marker, and the body states
// only the capability it needs of the wired publisher - so the same body mounts on a production
// broker and its in-process test transport unchanged, and under a slot transform or an app-wide
// publish layer alike. `Event` declares no destination of its own, so the call site names one.
struct Forward;

impl<Egress> Handle<Event, (), Outs<(Egress,)>> for Forward
where
    Egress: OutEntry<DefaultSlot, Wire: Publisher>,
{
    async fn handle(
        &self,
        event: &Event,
        outs: &Outs<(Egress,)>,
        _ctx: &mut Context<'_>,
    ) -> Result<(), HandlerOutcome> {
        if outs
            .get(DefaultSlot)
            .message(event)
            .to("egress")
            .publish()
            .await
            .is_err()
        {
            return Err(HandlerOutcome::retry());
        }
        Ok(())
    }
}
// --8<-- [end:forward]

// --8<-- [start:slots]
// A body with several injected publishers names a slot marker per parameter; the include site
// binds each marker to its own policy, in any order. Each marker lists what may leave through
// it, which is both what the generated document reports and what the publish builder admits.
struct Primary;

impl OutSlot for Primary {
    const NAME: &'static str = "Primary";
    type Destination = Reads;

    fn outgoing() -> Vec<OutgoingMessageMetadata> {
        <Event as OutMessages<Self>>::outgoing()
    }
}

impl PublishedThrough<Primary> for Event {}

struct Shadow;

impl OutSlot for Shadow {
    const NAME: &'static str = "Shadow";
    type Destination = Reads;

    fn outgoing() -> Vec<OutgoingMessageMetadata> {
        <Event as OutMessages<Self>>::outgoing()
    }
}

impl PublishedThrough<Shadow> for Event {}

struct Mirror;

impl<P, S> Handle<Event, (), Outs<(P, S)>> for Mirror
where
    P: OutEntry<Primary, Wire: Publisher>,
    S: OutEntry<Shadow, Wire: Publisher>,
{
    async fn handle(
        &self,
        event: &Event,
        outs: &Outs<(P, S)>,
        _ctx: &mut Context<'_>,
    ) -> Result<(), HandlerOutcome> {
        if outs
            .get(Primary)
            .message(event)
            .to("mirror-primary")
            .publish()
            .await
            .is_err()
            || outs
                .get(Shadow)
                .message(event)
                .to("mirror-shadow")
                .publish()
                .await
                .is_err()
        {
            return Err(HandlerOutcome::retry());
        }
        Ok(())
    }
}
// --8<-- [end:slots]

// --8<-- [start:publish_out]
// A reply and an injected publisher in one body: the reply answers on the chain's destination
// while an audit copy fans out through the slot. Both are axes of the one trait - the reply type
// and the arena - and the mount site fills both.
struct Gateway;

impl<Audit> Handle<Request, Response, Outs<(Audit,)>> for Gateway
where
    Audit: OutEntry<DefaultSlot, Wire: Publisher>,
{
    async fn handle(
        &self,
        req: &Request,
        outs: &Outs<(Audit,)>,
        _ctx: &mut Context<'_>,
    ) -> Result<Response, HandlerOutcome> {
        if outs
            .get(DefaultSlot)
            .message(&Event { id: req.id })
            .to("gateway-audit")
            .publish()
            .await
            .is_err()
        {
            return Err(HandlerOutcome::retry());
        }
        Ok(Response { ok: true })
    }
}
// --8<-- [end:publish_out]

// --8<-- [start:declared]
// What a message says about being sent lives on the type. A fixed name resolves the destination
// for every call site; a name template turns each `{placeholder}` into a setter, so a service
// routing per tenant still declares where the type goes; and a declaration with no name (like
// `Event` above) leaves the name to the call.
#[derive(Debug, Serialize)]
struct OrderConfirmed {
    id: u64,
}

impl OutgoingDestination for OrderConfirmed {
    type Form = FixedName;
    const DESTINATION: &'static str = "orders.confirmed";
}

impl MessageHeaders for OrderConfirmed {
    type Contract = NoHeaders;
}

impl<M: OutSlot> OutMessages<M> for OrderConfirmed {
    fn outgoing() -> Vec<OutgoingMessageMetadata> {
        vec![OutgoingMessageMetadata::new(
            Self::DESTINATION,
            std::any::type_name::<Self>(),
        )]
    }
}

#[derive(Debug, Serialize)]
struct OrderPlaced {
    id: u64,
}

impl OutgoingDestination for OrderPlaced {
    type Form = NameTemplate;
    const DESTINATION: &'static str = "orders.{tenant}.placed";
    const PARAMETERS: &'static [&'static str] = &["tenant"];
}

impl MessageHeaders for OrderPlaced {
    type Contract = NoHeaders;
}

impl<M: OutSlot> OutMessages<M> for OrderPlaced {
    fn outgoing() -> Vec<OutgoingMessageMetadata> {
        vec![
            OutgoingMessageMetadata::new(Self::DESTINATION, std::any::type_name::<Self>())
                .with_parameters(Self::PARAMETERS),
        ]
    }
}

/// The `tenant` segment of the declared name, named in the compile error of a publish that never
/// bound it. The derive hides it (and the builder below) in a module named after the type; by
/// hand the only requirement is that the names do not collide.
#[derive(Debug, Clone, Copy)]
struct Tenant;

/// One publish whose templated address is being bound, segment by segment. The publish terminal
/// is callable once every placeholder is bound; until then the unbound ones ride in this type, so
/// the compile error names them.
#[must_use = "an address builder does nothing until publish() is awaited"]
struct PlacedAt<Cont, S0> {
    cont: Cont,
    segment_0: S0,
}

impl<Cont> PlacedAt<Cont, MissingSegment<Tenant>> {
    fn start(cont: Cont) -> Self {
        Self {
            cont,
            segment_0: MissingSegment::new(),
        }
    }

    /// Binds the `tenant` segment of the declared name.
    fn tenant(self, value: impl Display) -> PlacedAt<Cont, String> {
        PlacedAt {
            cont: self.cont,
            segment_0: value.to_string(),
        }
    }
}

impl<Cont, S0> PlacedAt<Cont, S0> {
    // Every bound, the segment witness included, sits on the method: on the impl block a publish
    // that forgot a segment reads as "method not found" and loses the guidance the bound carries.
    async fn publish(self) -> Result<(), PublishError<<Cont as PublishAt>::Error>>
    where
        Cont: PublishAt,
        S0: BoundSegment,
    {
        // A templated address is rendered per publish; the fixed form is not.
        let address = format!("orders.{}.placed", self.segment_0);
        PublishAt::publish_at(self.cont, &address).await
    }
}

impl<Cont> TemplateAddress<Cont> for OrderPlaced {
    type Builder = PlacedAt<Cont, MissingSegment<Tenant>>;

    fn begin(cont: Cont) -> Self::Builder {
        PlacedAt::start(cont)
    }
}

struct Orders;

impl OutSlot for Orders {
    const NAME: &'static str = "Orders";
    type Destination = Reads;

    fn outgoing() -> Vec<OutgoingMessageMetadata> {
        let mut declared = <OrderConfirmed as OutMessages<Self>>::outgoing();
        declared.extend(<OrderPlaced as OutMessages<Self>>::outgoing());
        declared
    }
}

impl PublishedThrough<Orders> for OrderConfirmed {}
impl PublishedThrough<Orders> for OrderPlaced {}

struct Route;

impl<O> Handle<Event, (), Outs<(O,)>> for Route
where
    O: OutEntry<Orders, Wire: Publisher>,
{
    async fn handle(
        &self,
        event: &Event,
        outs: &Outs<(O,)>,
        _ctx: &mut Context<'_>,
    ) -> Result<(), HandlerOutcome> {
        let orders = outs.get(Orders);
        // Bound to one name: the destination is already resolved.
        if orders
            .message(&OrderConfirmed { id: event.id })
            .publish()
            .await
            .is_err()
        {
            return Err(HandlerOutcome::retry());
        }
        // Bound to a space of names: one setter per placeholder, and no publish until the last one
        // is bound.
        if orders
            .message(&OrderPlaced { id: event.id })
            .to()
            .tenant("acme")
            .publish()
            .await
            .is_err()
        {
            return Err(HandlerOutcome::retry());
        }
        Ok(())
    }
}
// --8<-- [end:declared]

// --8<-- [start:static_transform]
/// A static, per-publisher transform: stamps an envelope header on every outgoing message. It
/// reads no context, so one impl serves every position - a reply and an `Out` slot alike.
struct EnvelopeTransform;

impl<K: ContextKind, Options> PublishTransform<K, Options> for EnvelopeTransform {
    type Destination = Reads;

    fn apply(&self, out: &mut Outgoing<'_>, _options: &mut Option<Options>, _cx: &K::View<'_>) {
        out.headers_mut().insert("x-envelope", b"1".to_vec());
    }
}
// --8<-- [end:static_transform]

// --8<-- [start:slot_transform]
/// A static, per-slot transform: it stamps what leaves one `Out` slot. A slot position hands its
/// transforms a `SlotContext`, which names the slot and nothing else: the body issues a slot
/// publish itself, so the delivery is the body's own to read and put on the message.
struct OutboxEnvelope;

impl<K: ContextKind, Options> PublishTransform<K, Options> for OutboxEnvelope {
    type Destination = Reads;

    fn apply(&self, out: &mut Outgoing<'_>, _options: &mut Option<Options>, _cx: &K::View<'_>) {
        out.headers_mut().insert("x-outbox", b"1".to_vec());
    }
}
// --8<-- [end:slot_transform]

// --8<-- [start:app_layer]
/// A static, app-wide publish layer: observes every publish, then passes it on.
#[derive(Clone)]
struct AuditPublish;

impl PublishLayer for AuditPublish {
    async fn on_publish<'a, N: PublishPipeline, P: Publisher>(
        &'a self,
        out: &'a mut Outgoing<'a>,
        next: PublishNext<'a, N, P>,
    ) -> Result<(), Box<dyn Error + Send + Sync>> {
        println!("publishing to {}", out.name());
        next.run(out).await
    }
}
// --8<-- [end:app_layer]

// --8<-- [start:batch_publishing]
/// Confirms a whole batch of orders; the replies become visible atomically on commit. The batch
/// input and the reply type are two axes of the one trait: one `Vec` of replies per batch, each
/// published to the destination the chain names, and an `Err` settles the batch element-wise
/// (one outcome per element) without publishing anything.
struct Confirm;

impl Handle<[Event], Vec<Event>> for Confirm {
    fn handle(
        &self,
        orders: &[Event],
        _outs: &(),
        _ctx: &mut Context<'_>,
    ) -> impl Future<Output = Result<Vec<Event>, Vec<HandlerOutcome>>> {
        if orders.iter().any(|o| o.id == 0) {
            // nothing published, every element of the batch settled on its own
            return ready(Err(orders.iter().map(|_| HandlerOutcome::drop()).collect()));
        }
        ready(Ok(orders.iter().map(|o| Event { id: o.id }).collect()))
    }
}
// --8<-- [end:batch_publishing]

// --8<-- [start:manual_transaction]
/// Seeds the reference events inside one broker transaction: both records become visible
/// together on commit, or not at all. The scope owns the transaction, so a commit without a
/// begin, a second commit, or a publish after settling do not compile. The publisher arrives
/// already paired (the scope's `after_startup` hands it over live), so seeding cannot race the
/// broker connect; the bound names the capability the seeding needs, not the broker's publisher.
async fn seed_events<P>(seeder: P) -> Result<(), Box<dyn Error + Send + Sync>>
where
    P: TransactionalPublisher,
{
    let scope = seeder.begin().await?;
    scope
        .message(&Event { id: 1 })
        .to("events")
        .publish()
        .await?;
    scope
        .message(&Event { id: 2 })
        .to("events")
        .publish()
        .await?;
    scope.commit().await?;
    Ok(())
}
// --8<-- [end:manual_transaction]

// `impl App` hides the composed pipeline type: the app-wide `publish_layer` would otherwise surface
// in the return type as `RustStream<_, (), PublishStack<AuditPublish, PublishIdentity>>`.
fn app() -> impl App {
    let broker = MemoryBroker::new();
    // --8<-- [start:pipeline]
    RustStream::new(AppInfo::new("publishing", "0.1.0"))
        // app-wide layer: wraps every publish a handler makes, replies and Out slots alike
        .publish_layer(AuditPublish)
        .with_broker(broker, |b| {
            // the first publish: runs once connected and subscribed, with the transactional
            // wiring already paired
            b.after_startup(TransactionalPublish, async move |seeder| {
                seed_events(seeder).await.map_err(std::io::Error::other)
            });
            // --8<-- [start:reply_mount]
            // the reply type carries the destination, so the chain adds nothing to it
            b.include(subscriber("receipt-requests", IssueReceipt).reply().build());
            // static, per-reply: the chain names the policy and composes the transform at
            // compile time; the runtime pairs it with the connected broker at startup
            b.include(
                subscriber("requests", Respond)
                    .reply()
                    .to("responses")
                    .build(),
            )
            .out_reply(Publish)
            .transform(EnvelopeTransform);
            // the default reply wiring: the broker's default policy under the default codec
            b.include(
                subscriber("validated-requests", Validate)
                    .reply()
                    .to("responses")
                    .build(),
            );
            // --8<-- [end:reply_mount]
            // --8<-- [start:forward_mount]
            b.include(subscriber("ingress", Forward).build())
                .out(DefaultSlot, Publish)
                .build();
            // --8<-- [end:forward_mount]
            // --8<-- [start:slots_mount]
            // each named slot binds by marker; the call order does not matter, and a
            // .transform(..) rides the slot the .out(..) before it bound
            b.include(subscriber("mirror", Mirror).build())
                .out(Shadow, Publish)
                .transform(OutboxEnvelope)
                .out(Primary, Publish)
                .build();
            // --8<-- [end:slots_mount]
            // --8<-- [start:publish_out_mount]
            // one verb for both positions: .out_reply(..) names who publishes the returned
            // value (or leave it out for the default), and .out(<marker>, ..) binds an Out
            // slot - DefaultSlot for a single unnamed slot
            b.include(
                subscriber("gateway-requests", Gateway)
                    .reply()
                    .to("gateway-responses")
                    .build(),
            )
            .out(DefaultSlot, Publish)
            .build();
            // --8<-- [end:publish_out_mount]
            // --8<-- [start:declared_mount]
            // the slot lists what it may publish; where each message goes is its own declaration
            b.include(subscriber("orders.incoming", Route).build())
                .out(Orders, Publish)
                .build();
            // --8<-- [end:declared_mount]
            // --8<-- [start:batch_publishing_mount]
            // .batch(n) is the batch size the subscription opens with, which every batch mount
            // owes. .transactional() marks the wiring; the pairing checks that the policy's live
            // publisher is transactional. Without it, each reply publishes independently.
            b.include(
                subscriber("orders", Confirm)
                    .reply()
                    .to("confirmations")
                    .batch(nonzero!(64))
                    .build(),
            )
            .out_reply(TransactionalPublish)
            .transactional();
            // --8<-- [end:batch_publishing_mount]
        })
    // --8<-- [end:pipeline]
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    app().run().await?;
    Ok(())
}