ruststream 0.7.5

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
//! Pattern subscriptions: the [`MemoryPattern`] descriptor, the pattern it parses, and the
//! [`Routing`] rule that decides which subscriptions a publish reaches.
//!
//! A name is split into tokens at `.`. A pattern token `*` matches exactly one token, and `>` as
//! the last token matches one or more tokens: the NATS subject syntax, so a service that moves
//! between this broker and NATS keeps its subscriptions as written.

use std::{
    cmp::Ordering,
    future::{Future, ready},
};

use thiserror::Error;
use tokio::sync::mpsc;

use super::{ConnectedMemoryBroker, Discarding, LogMode, MemoryError, MemorySubscriber, Sender};
use crate::{AddressedCopies, RedeliveryAddress, RedeliveryAddressed, SubscriptionSource};

/// How the broker picks the subscriptions a publish reaches when patterns are subscribed.
///
/// Set once, on the unconnected broker, with [`MemoryBroker::routing`](super::MemoryBroker::routing).
/// Subscriptions of one exact name receive every publish to that name under both rules; the rule
/// decides what happens when patterns match as well.
///
/// # Examples
///
/// ```
/// # #[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.eu")]
/// async fn europe(order: &Order) -> HandlerOutcome {
///     tracing::info!(order.id, "europe");
///     HandlerOutcome::ack()
/// }
///
/// /// A fallback: it receives what no exact name, and no more specific pattern, took.
/// #[subscriber(MemoryPattern::new("orders.*"))]
/// async fn elsewhere(order: &Order) -> HandlerOutcome {
///     tracing::info!(order.id, "elsewhere");
///     HandlerOutcome::ack()
/// }
///
/// fn app() -> RustStream {
///     let broker = MemoryBroker::new().routing(Routing::MostSpecific);
///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(broker, |b| {
///         b.include(europe);
///         b.include(elsewhere);
///     })
/// }
/// # }
/// # fn main() {}
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Routing {
    /// Every subscription whose name or pattern matches receives the publish, as NATS delivers.
    /// The default.
    #[default]
    EveryMatch,
    /// Only the most specific match receives the publish.
    ///
    /// The subscriptions of the exact name come first. Where there are none, the most specific
    /// matching pattern wins: two patterns are compared token by token from the left, and at the
    /// first position where they differ a literal token beats `*`, and `*` beats `>`. So for a
    /// publish to `orders.eu.created`, `orders.eu.*` beats `orders.*.created`, which beats
    /// `orders.>`, which beats `*.eu.created`. Two different patterns that match the same name
    /// always differ somewhere, so there is exactly one winner; every subscription of the
    /// winning pattern receives the publish.
    MostSpecific,
}

/// Why a subscription pattern was refused.
///
/// Reported inside [`MemoryError::InvalidPattern`], which names the pattern.
///
/// # Examples
///
/// ```
/// # async fn demo() -> Result<(), Box<dyn std::error::Error>> {
/// use ruststream::memory::{MemoryBroker, MemoryError, MemoryPattern, PatternError};
/// use ruststream::{Broker, SubscriptionSource};
///
/// let connected = MemoryBroker::new().connect().await?;
/// let refused = MemoryPattern::new("orders.>.eu").subscribe(&connected).await;
/// assert!(matches!(
///     refused,
///     Err(MemoryError::InvalidPattern { reason: PatternError::TailNotLast { token: 1 }, .. })
/// ));
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PatternError {
    /// A token between two dots, or before the first or after the last one, is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn demo() -> Result<(), Box<dyn std::error::Error>> {
    /// use ruststream::memory::{MemoryBroker, MemoryError, MemoryPattern, PatternError};
    /// use ruststream::{Broker, SubscriptionSource};
    ///
    /// let connected = MemoryBroker::new().connect().await?;
    /// let refused = MemoryPattern::new("orders..eu").subscribe(&connected).await;
    /// assert!(matches!(
    ///     refused,
    ///     Err(MemoryError::InvalidPattern { reason: PatternError::EmptyToken { token: 1 }, .. })
    /// ));
    /// # Ok(())
    /// # }
    /// ```
    #[error("token {token} is empty; tokens are separated by single dots")]
    EmptyToken {
        /// The zero-based index of the empty token.
        token: usize,
    },
    /// `>` stands before the last token. It matches the rest of a name, so it ends a pattern.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn demo() -> Result<(), Box<dyn std::error::Error>> {
    /// use ruststream::memory::{MemoryBroker, MemoryError, MemoryPattern, PatternError};
    /// use ruststream::{Broker, SubscriptionSource};
    ///
    /// let connected = MemoryBroker::new().connect().await?;
    /// let refused = MemoryPattern::new("orders.>.eu").subscribe(&connected).await;
    /// assert!(matches!(
    ///     refused,
    ///     Err(MemoryError::InvalidPattern { reason: PatternError::TailNotLast { token: 1 }, .. })
    /// ));
    /// # Ok(())
    /// # }
    /// ```
    #[error("`>` at token {token} is not the last token; it matches the rest of a name")]
    TailNotLast {
        /// The zero-based index of the misplaced `>`.
        token: usize,
    },
    /// The pattern has no `*` or `>` token, so it matches one name only.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn demo() -> Result<(), Box<dyn std::error::Error>> {
    /// use ruststream::memory::{MemoryBroker, MemoryError, MemoryPattern, PatternError};
    /// use ruststream::{Broker, SubscriptionSource};
    ///
    /// let connected = MemoryBroker::new().connect().await?;
    /// let refused = MemoryPattern::new("orders.eu").subscribe(&connected).await;
    /// assert!(matches!(
    ///     refused,
    ///     Err(MemoryError::InvalidPattern { reason: PatternError::NoWildcard, .. })
    /// ));
    /// # Ok(())
    /// # }
    /// ```
    #[error("it has no `*` or `>` token; subscribe to one name with `MemorySource`")]
    NoWildcard,
}

/// Whether `name` carries a wildcard token, `*` or `>`, which makes it a pattern rather than a
/// name.
///
/// A wildcard is a whole token: `orders.eu*` is the name it reads as.
pub(super) fn has_wildcard(name: &str) -> bool {
    name.split('.').any(|token| token == "*" || token == ">")
}

/// One token of a parsed pattern.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Token {
    Literal(Box<str>),
    /// `*`: exactly one token.
    One,
    /// `>`: one or more tokens, and the end of the pattern.
    Tail,
}

impl Token {
    /// The token's place in the specificity order: a literal beats `*`, and `*` beats `>`.
    const fn rank(&self) -> u8 {
        match self {
            Self::Literal(_) => 2,
            Self::One => 1,
            Self::Tail => 0,
        }
    }
}

/// A validated subscription pattern: parsed once, when the subscription opens, and matched on
/// every publish without allocating.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Pattern {
    tokens: Box<[Token]>,
}

impl Pattern {
    /// Parses `pattern`.
    ///
    /// # Errors
    ///
    /// Returns the [`PatternError`] of the first fault from the left, or
    /// [`PatternError::NoWildcard`] for a pattern with no wildcard token.
    pub(super) fn parse(pattern: &str) -> Result<Self, PatternError> {
        let count = pattern.split('.').count();
        let mut wildcard = false;
        let tokens = pattern
            .split('.')
            .enumerate()
            .map(|(index, token)| match token {
                "" => Err(PatternError::EmptyToken { token: index }),
                ">" if index + 1 < count => Err(PatternError::TailNotLast { token: index }),
                ">" => {
                    wildcard = true;
                    Ok(Token::Tail)
                }
                "*" => {
                    wildcard = true;
                    Ok(Token::One)
                }
                literal => Ok(Token::Literal(literal.into())),
            })
            .collect::<Result<Box<[Token]>, PatternError>>()?;
        if !wildcard {
            return Err(PatternError::NoWildcard);
        }
        Ok(Self { tokens })
    }

    /// Whether a publish to `name` matches this pattern.
    ///
    /// A wildcard stands for a token, and an empty token (a leading, trailing or doubled dot) is
    /// none: the parser refuses one in a pattern, so it matches none in a name either.
    pub(super) fn matches(&self, name: &str) -> bool {
        let mut parts = name.split('.');
        for token in &*self.tokens {
            match token {
                Token::Tail => {
                    return parts.next().is_some_and(|part| !part.is_empty())
                        && parts.all(|part| !part.is_empty());
                }
                Token::One => {
                    if parts.next().is_none_or(str::is_empty) {
                        return false;
                    }
                }
                Token::Literal(literal) => {
                    if parts.next() != Some(&**literal) {
                        return false;
                    }
                }
            }
        }
        parts.next().is_none()
    }

    /// Orders two patterns by specificity: `Greater` when `self` is the more specific one.
    pub(super) fn specificity(&self, other: &Self) -> Ordering {
        self.tokens
            .iter()
            .map(Token::rank)
            .cmp(other.tokens.iter().map(Token::rank))
    }
}

/// The subscriptions of one pattern: every subscription opened under the same pattern text
/// joins one group, so the most specific match hands the publish to all of them.
pub(super) struct PatternGroup {
    pub(super) text: Box<str>,
    pub(super) pattern: Pattern,
    pub(super) senders: Vec<Sender>,
}

impl PatternGroup {
    /// Whether any subscription of this pattern still reads.
    pub(super) fn is_live(&self) -> bool {
        self.senders.iter().any(|sender| !sender.is_closed())
    }
}

/// Which pattern groups a publish reaches, found in one scan of the pattern table.
pub(super) enum PatternReach {
    /// None of them.
    Nobody,
    /// Every matching group, the first of which sits at this index.
    EveryFrom(usize),
    /// The group at this index alone.
    Only(usize),
}

impl Routing {
    /// Which of `groups` a publish to `name` reaches, given whether an exact name took it.
    pub(super) fn reach(self, groups: &[PatternGroup], name: &str, exact: bool) -> PatternReach {
        match self {
            Self::EveryMatch => groups
                .iter()
                .position(|group| group.pattern.matches(name))
                .map_or(PatternReach::Nobody, PatternReach::EveryFrom),
            Self::MostSpecific if exact => PatternReach::Nobody,
            // A group whose subscriptions have all gone takes nothing, so it cannot shadow a
            // less specific one that is still listening.
            Self::MostSpecific => groups
                .iter()
                .enumerate()
                .filter(|(_, group)| group.pattern.matches(name) && group.is_live())
                .max_by(|(_, left), (_, right)| left.pattern.specificity(&right.pattern))
                .map_or(PatternReach::Nobody, |(index, _)| PatternReach::Only(index)),
        }
    }

    /// Which of `subscriptions` (names and patterns, as an app subscribed them) a publish to
    /// `destination` reaches: the positions, in order.
    ///
    /// The same rule as the fanout's, over the names alone. A name with a wildcard token is a
    /// pattern, since a name subscription refuses one; a pattern that does not parse opened no
    /// subscription, so it reaches nothing.
    #[cfg(any(test, feature = "testing"))]
    pub(super) fn routes(self, destination: &str, subscriptions: &[&str]) -> Vec<usize> {
        let exact: Vec<usize> = subscriptions
            .iter()
            .enumerate()
            .filter(|(_, name)| !has_wildcard(name) && **name == destination)
            .map(|(position, _)| position)
            .collect();
        let patterns: Vec<(usize, Pattern)> = subscriptions
            .iter()
            .enumerate()
            .filter(|(_, name)| has_wildcard(name))
            .filter_map(|(position, name)| Some((position, Pattern::parse(name).ok()?)))
            .filter(|(_, pattern)| pattern.matches(destination))
            .collect();
        match self {
            Self::EveryMatch => {
                let mut every: Vec<usize> = exact
                    .into_iter()
                    .chain(patterns.into_iter().map(|(position, _)| position))
                    .collect();
                every.sort_unstable();
                every
            }
            Self::MostSpecific if !exact.is_empty() => exact,
            Self::MostSpecific => {
                let Some((_, best)) = patterns
                    .iter()
                    .max_by(|(_, left), (_, right)| left.specificity(right))
                else {
                    return Vec::new();
                };
                patterns
                    .iter()
                    .filter(|(_, pattern)| pattern == best)
                    .map(|(position, _)| *position)
                    .collect()
            }
        }
    }
}

/// A pattern subscription descriptor for [`MemoryBroker`](super::MemoryBroker): one subscription
/// reading every name the pattern matches.
///
/// `*` matches one token and `>` as the last token matches the rest of the name, as NATS
/// subjects do: `orders.*` reads `orders.eu` and `orders.us`, `orders.>` reads `orders.eu.created`
/// as well. The pattern is checked when the subscription opens, at startup, and a bad one
/// refuses to start with [`MemoryError::InvalidPattern`]. Which subscriptions a publish reaches
/// when several match is the broker's [`Routing`].
///
/// A `retry_after` outcome comes back to the same subscriber on the broker's own timer, as on
/// every subscription of this broker, so a pattern needs no destination for its retries. A
/// pattern subscription has no replay position: it reads many names, and a retaining broker keeps
/// a log per name, so its subscriber is
/// never [`Seekable`](crate::Seekable) and a handler on it cannot read a seek handle.
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "macros", feature = "json"))]
/// # mod demo {
/// use ruststream::memory::prelude::*;
/// # #[derive(serde::Deserialize)]
/// # struct Order { id: u64 }
///
/// #[subscriber(MemoryPattern::new("orders.*"))]
/// async fn audit(order: &Order) -> HandlerOutcome {
///     let _ = order.id;
///     HandlerOutcome::ack()
/// }
///
/// fn app() -> RustStream {
///     RustStream::new(AppInfo::new("audit", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
///         b.include(audit);
///     })
/// }
/// # }
/// # fn main() {}
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct MemoryPattern {
    pattern: String,
}

impl MemoryPattern {
    /// Creates a source reading every name `pattern` matches.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(all(feature = "macros", feature = "memory", feature = "json"))]
    /// # mod demo {
    /// use ruststream::memory::prelude::*;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct Order {
    ///     id: u64,
    /// }
    ///
    /// /// `orders.*` matches one token after `orders.`, `orders.>` every name below it.
    /// #[subscriber(MemoryPattern::new("orders.>"))]
    /// async fn audit(order: &Order) -> HandlerOutcome {
    ///     tracing::info!(order.id, "audited");
    ///     HandlerOutcome::ack()
    /// }
    ///
    /// fn app() -> RustStream {
    ///     RustStream::new(AppInfo::new("audit", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
    ///         b.include(audit);
    ///     })
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    pub fn new(pattern: impl Into<String>) -> Self {
        Self {
            pattern: pattern.into(),
        }
    }
}

impl<Log: LogMode> SubscriptionSource<ConnectedMemoryBroker<Log>> for MemoryPattern {
    // A pattern reads many names, and a retaining broker keeps a log per name, so there is no
    // one log a seek could reposition it in: the subscriber is the non-seekable form on either
    // broker.
    type Subscriber = MemorySubscriber<Discarding>;
    // This broker redelivers a `retry_after` on its own timer, so the runtime publishes no copy
    // to this address; what it reports is the pattern text, which every pattern reads as itself.
    type Copies = AddressedCopies;

    fn name(&self) -> &str {
        &self.pattern
    }

    fn subscribe(
        self,
        connected: &ConnectedMemoryBroker<Log>,
    ) -> impl Future<Output = Result<Self::Subscriber, MemoryError>> + Send {
        let subscribed = match Pattern::parse(&self.pattern) {
            Ok(pattern) => {
                let (tx, rx) = mpsc::unbounded_channel();
                connected
                    .state
                    .register_pattern(&self.pattern, pattern, tx.clone())
                    .map(|()| {
                        MemorySubscriber::new(
                            self.pattern,
                            rx,
                            tx,
                            &connected.state,
                            Some(connected.runtime.clone()),
                        )
                    })
            }
            Err(reason) => Err(MemoryError::InvalidPattern {
                pattern: self.pattern,
                reason,
            }),
        };
        ready(subscribed)
    }
}

impl<Log: LogMode> RedeliveryAddressed<ConnectedMemoryBroker<Log>> for MemoryPattern {
    /// The pattern text. A publish to it matches the pattern token for token (`*` matches the
    /// token `*`, `>` the token `>`), so under [`Routing::EveryMatch`] it reaches this
    /// subscription.
    fn redelivery_address(
        &self,
        _connected: &ConnectedMemoryBroker<Log>,
    ) -> impl Future<Output = Result<RedeliveryAddress, MemoryError>> + Send {
        ready(Ok(RedeliveryAddress::new(self.pattern.clone())))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(pattern: &str) -> Pattern {
        Pattern::parse(pattern).expect("a valid pattern")
    }

    #[test]
    fn one_matches_exactly_one_token() {
        let pattern = parse("orders.*");
        assert!(pattern.matches("orders.eu"));
        assert!(!pattern.matches("orders"));
        assert!(!pattern.matches("orders.eu.created"));
        assert!(!pattern.matches("invoices.eu"));
    }

    #[test]
    fn tail_matches_one_or_more_tokens() {
        let pattern = parse("orders.>");
        assert!(pattern.matches("orders.eu"));
        assert!(pattern.matches("orders.eu.created"));
        assert!(!pattern.matches("orders"));
        assert!(parse(">").matches("anything.at.all"));
    }

    #[test]
    fn an_empty_token_matches_no_wildcard() {
        assert!(!parse("orders.*").matches("orders."));
        assert!(!parse("orders.*").matches(".eu"));
        assert!(!parse("orders.>").matches("orders."));
        assert!(!parse("orders.>").matches("orders..eu"));
        assert!(!parse("orders.>").matches("orders.eu."));
        assert!(!parse("*.eu").matches(".eu"));
        assert!(parse("orders.>").matches("orders.eu.created"));
    }

    #[test]
    fn faults_are_reported_from_the_left() {
        assert_eq!(
            Pattern::parse("orders..*"),
            Err(PatternError::EmptyToken { token: 1 })
        );
        assert_eq!(
            Pattern::parse("orders.>.eu"),
            Err(PatternError::TailNotLast { token: 1 })
        );
        assert_eq!(
            Pattern::parse("*."),
            Err(PatternError::EmptyToken { token: 1 })
        );
        assert_eq!(Pattern::parse("orders.eu"), Err(PatternError::NoWildcard));
        // A wildcard is a whole token, so this is a name, not a pattern.
        assert_eq!(Pattern::parse("orders.eu*"), Err(PatternError::NoWildcard));
        assert!(!has_wildcard("orders.eu*"));
    }

    #[test]
    fn a_literal_beats_one_and_one_beats_tail_from_the_left() {
        let ordered = [
            "orders.eu.*",
            "orders.*.created",
            "orders.>",
            "*.eu.created",
        ];
        for pair in ordered.windows(2) {
            assert_eq!(
                parse(pair[0]).specificity(&parse(pair[1])),
                Ordering::Greater,
                "{} is more specific than {}",
                pair[0],
                pair[1]
            );
        }
    }

    #[test]
    fn routes_follow_the_rule() {
        let subscriptions = ["orders.eu", "orders.*", "orders.>", "orders.*"];
        assert_eq!(
            Routing::EveryMatch.routes("orders.eu", &subscriptions),
            [0, 1, 2, 3]
        );
        assert_eq!(
            Routing::MostSpecific.routes("orders.eu", &subscriptions),
            [0]
        );
        assert_eq!(
            Routing::MostSpecific.routes("orders.us", &subscriptions),
            [1, 3]
        );
        assert_eq!(
            Routing::MostSpecific.routes("orders.us.created", &subscriptions),
            [2]
        );
        assert_eq!(
            Routing::MostSpecific.routes("invoices", &subscriptions),
            Vec::<usize>::new()
        );
    }
}