use super::record::{KafkaPublish, KafkaRecord};
use crate::{
dead_letter::DEAD_LETTER_HEADER_PREFIX,
error::Error,
forward::{SamePlatform, ValueRecord},
handler::{HandlerError, Result},
middleware::Middleware,
};
#[derive(Clone, Copy, Debug)]
pub struct KafkaInherit {
key: bool,
headers: bool,
}
impl KafkaInherit {
pub fn new() -> Self {
Self {
key: true,
headers: true,
}
}
pub fn without_key(mut self) -> Self {
self.key = false;
self
}
pub fn without_headers(mut self) -> Self {
self.headers = false;
self
}
}
impl Default for KafkaInherit {
fn default() -> Self {
Self::new()
}
}
impl<I, O> Middleware<KafkaRecord<I>, KafkaPublish<O>> for KafkaInherit
where
I: Send + Sync + 'static,
O: Send + 'static,
{
async fn post_handler(
&self,
input: &KafkaRecord<I>,
mut output: KafkaPublish<O>,
) -> Result<KafkaPublish<O>> {
if self.key && output.key.is_none() {
output.key.clone_from(&input.key);
}
if self.headers {
let mut headers: Vec<_> = input
.headers
.iter()
.filter(|(name, _)| {
!name.starts_with(DEAD_LETTER_HEADER_PREFIX)
&& output.headers.iter().all(|(explicit, _)| explicit != name)
})
.cloned()
.collect();
headers.append(&mut output.headers);
output.headers = headers;
}
Ok(output)
}
}
impl<T: Clone + Send + Sync + 'static> ValueRecord for KafkaRecord<T> {
type Value = T;
fn value(&self) -> Result<T> {
self.value.clone().ok_or_else(|| {
HandlerError::Reject(Error::invalid_record("Kafka record has a null value").into())
})
}
}
impl<T: Clone + Send + Sync + 'static, U: Send + Sync + 'static> SamePlatform<U>
for KafkaRecord<T>
{
type Publish = KafkaPublish<U>;
type Inherit = KafkaInherit;
fn publish(value: U) -> KafkaPublish<U> {
KafkaPublish::new(value)
}
}