use super::record::{PulsarPublish, PulsarRecord};
use crate::{
dead_letter::DEAD_LETTER_HEADER_PREFIX,
error::Error,
forward::{SamePlatform, ValueRecord},
handler::{HandlerError, Result},
middleware::Middleware,
};
#[derive(Clone, Copy, Debug)]
pub struct PulsarInherit {
key: bool,
properties: bool,
event_time: bool,
}
impl PulsarInherit {
pub fn new() -> Self {
Self {
key: true,
properties: true,
event_time: true,
}
}
pub fn without_key(mut self) -> Self {
self.key = false;
self
}
pub fn without_properties(mut self) -> Self {
self.properties = false;
self
}
pub fn without_event_time(mut self) -> Self {
self.event_time = false;
self
}
}
impl Default for PulsarInherit {
fn default() -> Self {
Self::new()
}
}
impl<I, O> Middleware<PulsarRecord<I>, PulsarPublish<O>> for PulsarInherit
where
I: Send + Sync + 'static,
O: Send + 'static,
{
async fn post_handler(
&self,
input: &PulsarRecord<I>,
mut output: PulsarPublish<O>,
) -> Result<PulsarPublish<O>> {
if self.key && output.key.is_none() {
output.key.clone_from(&input.key);
}
if self.properties {
let inherited = input
.properties
.iter()
.filter(|(name, _)| !name.starts_with(DEAD_LETTER_HEADER_PREFIX));
for (name, value) in inherited {
output
.properties
.entry(name.clone())
.or_insert_with(|| value.clone());
}
}
if self.event_time && output.event_time.is_none() {
output.event_time = input.event_time;
}
Ok(output)
}
}
impl<T: Clone + Send + Sync + 'static> ValueRecord for PulsarRecord<T> {
type Value = T;
fn value(&self) -> Result<T> {
self.value.clone().ok_or_else(|| {
HandlerError::Reject(Error::invalid_record("Pulsar record has a null value").into())
})
}
}
impl<T: Clone + Send + Sync + 'static, U: Send + Sync + 'static> SamePlatform<U>
for PulsarRecord<T>
{
type Publish = PulsarPublish<U>;
type Inherit = PulsarInherit;
fn publish(value: U) -> PulsarPublish<U> {
PulsarPublish::new(value)
}
}