use async_nats::Client;
use bytes::Bytes;
use prost::Message;
pub struct Publisher {
client: Client,
worker_id: String,
}
impl Publisher {
pub fn new(client: Client, worker_id: String) -> Self {
Self { client, worker_id }
}
pub async fn publish<T: Message>(&self, message: T) -> Result<(), String> {
let data = message.encode_to_vec();
self.client
.publish(format!("HDP.worker.{}", self.worker_id), data.into())
.await
.map_err(|e| e.to_string())?;
Ok(())
}
pub async fn publish_raw(&self, data: Bytes) -> Result<(), String> {
self.client
.publish(format!("HDP.worker.{}", self.worker_id), data)
.await
.map_err(|e| e.to_string())?;
Ok(())
}
pub async fn publish_to<T: Message>(&self, topic: String, message: T) -> Result<(), String> {
let data = message.encode_to_vec();
self.client
.publish(topic, data.into())
.await
.map_err(|e| e.to_string())?;
Ok(())
}
}