mod.rs 1.58 KB
Newer Older
Ryan Olson's avatar
Ryan Olson committed
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
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! PubSub abstraction for distributed messaging.
//!
//! This module provides traits for publish/subscribe messaging patterns,
//! with implementations for NATS and an in-memory stub for testing.

use anyhow::Result;
use bytes::Bytes;
use futures::future::BoxFuture;
use futures::stream::BoxStream;

#[cfg(feature = "nats")]
mod nats;
mod stub;

#[cfg(feature = "nats")]
pub use self::nats::{NatsConfig, NatsPublisher, NatsSubscriber};
pub use stub::{StubBus, StubPublisher, StubSubscriber};

/// Message received from a subscription.
#[derive(Debug, Clone)]
pub struct Message {
    /// The subject the message was published to.
    pub subject: String,
    /// The message payload.
    pub payload: Bytes,
}

/// A subscription stream that yields messages.
pub type Subscription = BoxStream<'static, Message>;

pub use kvbm_logical::pubsub::Publisher;

/// Subscriber trait for receiving messages from subjects.
///
/// Subscribers receive messages published to matching subjects.
/// Subject patterns support wildcards:
/// - `*` matches a single token (e.g., `foo.*.bar`)
/// - `>` matches one or more tokens at the tail (e.g., `foo.>`)
pub trait Subscriber: Send + Sync {
    /// Subscribe to a subject pattern, returning a message stream.
    ///
    /// The returned stream yields messages as they arrive. The subscription
    /// remains active until the stream is dropped.
    fn subscribe(&self, subject: &str) -> BoxFuture<'static, Result<Subscription>>;
}