refactor(nats): strip NatsEventPublisher, add NatsTransport implementing Transport

This commit is contained in:
2026-05-14 12:22:11 +02:00
parent a684c922e0
commit cfc8c19175
2 changed files with 20 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
domain = { workspace = true } domain = { workspace = true }
event-payload = { workspace = true } event-payload = { workspace = true }
event-publisher = { workspace = true }
async-nats = { workspace = true } async-nats = { workspace = true }
async-stream = { workspace = true } async-stream = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }

View File

@@ -2,36 +2,33 @@ use async_trait::async_trait;
use domain::{ use domain::{
errors::DomainError, errors::DomainError,
events::{DomainEvent, EventEnvelope}, events::{DomainEvent, EventEnvelope},
ports::{EventConsumer, EventPublisher}, ports::EventConsumer,
}; };
use event_payload::EventPayload; use event_payload::EventPayload;
use event_publisher::Transport;
use futures::stream::BoxStream; use futures::stream::BoxStream;
// ── NatsEventPublisher ──────────────────────────────────────────────────── // ── NatsTransport — raw NATS publish backend ────────────────────────────────
pub struct NatsEventPublisher { pub struct NatsTransport {
client: async_nats::Client, client: async_nats::Client,
} }
impl NatsEventPublisher { impl NatsTransport {
pub fn new(client: async_nats::Client) -> Self { Self { client } } pub fn new(client: async_nats::Client) -> Self { Self { client } }
} }
#[async_trait] #[async_trait]
impl EventPublisher for NatsEventPublisher { impl Transport for NatsTransport {
async fn publish(&self, event: &DomainEvent) -> Result<(), DomainError> { async fn publish_bytes(&self, subject: &str, bytes: &[u8]) -> Result<(), DomainError> {
let payload = EventPayload::from(event);
let subject = payload.subject();
let bytes = serde_json::to_vec(&payload)
.map_err(|e| DomainError::Internal(e.to_string()))?;
self.client self.client
.publish(subject, bytes.into()) .publish(subject.to_string(), bytes.to_vec().into())
.await .await
.map_err(|e| DomainError::Internal(e.to_string())) .map_err(|e| DomainError::Internal(e.to_string()))
} }
} }
// ── NatsEventConsumer ───────────────────────────────────────────────────── // ── NatsEventConsumer — subscribes and yields EventEnvelopes ────────────────
pub struct NatsEventConsumer { pub struct NatsEventConsumer {
client: async_nats::Client, client: async_nats::Client,
@@ -66,7 +63,6 @@ impl EventConsumer for NatsEventConsumer {
continue; continue;
} }
}; };
// Basic NATS: no ack/nack (at-most-once delivery)
yield EventEnvelope { yield EventEnvelope {
event, event,
ack: Box::new(|| {}), ack: Box::new(|| {}),