use std::marker::PhantomData;
use std::sync::Arc;
use crate::{
EventBusError,
EventEnvelope,
IntoEventBusResult,
PublishOptions,
RetryOptions,
};
use super::publish_options::PublishErrorHandlerFn;
pub struct PublishOptionsBuilder<T: 'static> {
retry_options: Option<RetryOptions>,
error_handlers: Vec<Arc<PublishErrorHandlerFn<T>>>,
marker: PhantomData<fn() -> T>,
}
impl<T: 'static> PublishOptionsBuilder<T> {
pub(crate) fn new() -> Self {
Self {
retry_options: None,
error_handlers: Vec::new(),
marker: PhantomData,
}
}
pub fn retry_options(mut self, retry_options: RetryOptions) -> Self {
self.retry_options = Some(retry_options);
self
}
pub fn error_handler<F, R>(mut self, handler: F) -> Self
where
F: Fn(&EventEnvelope<T>, &EventBusError) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
{
self.error_handlers.push(Arc::new(move |envelope, error| {
handler(envelope, error).into_event_bus_result()
}));
self
}
pub fn build(self) -> PublishOptions<T> {
PublishOptions {
retry_options: self.retry_options,
error_handlers: self.error_handlers,
}
}
}