use bytes::Bytes;
use super::{EventDescriptor, ServerError};
#[derive(Debug, Clone)]
pub struct EventPublisher {
client: async_nats::Client,
}
impl EventPublisher {
pub fn new(client: async_nats::Client) -> Self {
Self { client }
}
pub async fn publish<D>(&self, event: &D::Event) -> Result<(), ServerError>
where
D: EventDescriptor,
{
let payload = Bytes::from(serde_json::to_vec(event)?);
self.client
.publish(D::SUBJECT.to_string(), payload)
.await
.map_err(|error| ServerError::Nats(error.to_string()))?;
Ok(())
}
}