Skip to main content

kvbm_engine/pubsub/
mod.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! PubSub abstraction for distributed messaging.
5//!
6//! This module provides traits for publish/subscribe messaging patterns,
7//! with implementations for NATS and an in-memory stub for testing.
8
9use anyhow::Result;
10use bytes::Bytes;
11use futures::future::BoxFuture;
12use futures::stream::BoxStream;
13
14#[cfg(feature = "nats")]
15mod nats;
16mod stub;
17
18#[cfg(feature = "nats")]
19pub use self::nats::{NatsConfig, NatsPublisher, NatsSubscriber};
20pub use stub::{StubBus, StubPublisher, StubSubscriber};
21
22/// Message received from a subscription.
23#[derive(Debug, Clone)]
24pub struct Message {
25    /// The subject the message was published to.
26    pub subject: String,
27    /// The message payload.
28    pub payload: Bytes,
29}
30
31/// A subscription stream that yields messages.
32pub type Subscription = BoxStream<'static, Message>;
33
34pub use kvbm_logical::pubsub::Publisher;
35
36/// Subscriber trait for receiving messages from subjects.
37///
38/// Subscribers receive messages published to matching subjects.
39/// Subject patterns support wildcards:
40/// - `*` matches a single token (e.g., `foo.*.bar`)
41/// - `>` matches one or more tokens at the tail (e.g., `foo.>`)
42pub trait Subscriber: Send + Sync {
43    /// Subscribe to a subject pattern, returning a message stream.
44    ///
45    /// The returned stream yields messages as they arrive. The subscription
46    /// remains active until the stream is dropped.
47    fn subscribe(&self, subject: &str) -> BoxFuture<'static, Result<Subscription>>;
48}