use std::sync::Arc;
use bytes::Bytes;
use ruststream::{OutgoingMessage, Publisher};
use super::broker::TestBrokerState;
use crate::error::KafkaError;
#[derive(Debug, Clone)]
pub struct KafkaTestPublisher {
state: Arc<TestBrokerState>,
}
impl KafkaTestPublisher {
pub(crate) fn new(state: Arc<TestBrokerState>) -> Self {
Self { state }
}
}
impl Publisher for KafkaTestPublisher {
type Error = KafkaError;
async fn publish(&self, msg: OutgoingMessage<'_>) -> Result<(), Self::Error> {
if msg.name().is_empty() {
return Err(KafkaError::InvalidOptions(
"topic name must not be empty; the outgoing message name is the destination \
topic"
.to_owned(),
));
}
self.state.router.publish(
msg.name(),
&Bytes::copy_from_slice(msg.payload()),
msg.headers(),
self.state.coordinator().as_ref(),
);
Ok(())
}
}