use async_trait::async_trait;
use std::marker::PhantomData;
use crate::error::EventError;
pub struct NoOpPublisher<E> {
_phantom: PhantomData<E>,
}
impl<E> NoOpPublisher<E> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<E> Default for NoOpPublisher<E> {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
pub trait Publisher<E: Send + Sync + 'static>: Send + Sync {
async fn publish(&self, event: E) -> Result<(), EventError>;
async fn publish_many(&self, events: Vec<E>) -> Result<(), EventError> {
for event in events {
self.publish(event).await?;
}
Ok(())
}
}
#[async_trait]
impl<E: Send + Sync + 'static> Publisher<E> for NoOpPublisher<E> {
async fn publish(&self, _event: E) -> Result<(), EventError> {
Ok(())
}
async fn publish_many(&self, _events: Vec<E>) -> Result<(), EventError> {
Ok(())
}
}
pub struct NoOpEventBus;
impl NoOpEventBus {
pub fn new() -> Self {
Self
}
pub async fn publish<E: Send + 'static>(&self, _event: E) -> Result<(), EventError> {
Ok(())
}
}
impl Default for NoOpEventBus {
fn default() -> Self {
Self::new()
}
}