use std::{future::Future, sync::Arc};
use crate::IncomingMessage;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum HandlerResult {
Ack,
Nack {
requeue: bool,
},
}
impl HandlerResult {
#[must_use]
pub const fn retry() -> Self {
Self::Nack { requeue: true }
}
#[must_use]
pub const fn drop() -> Self {
Self::Nack { requeue: false }
}
}
pub trait Handler<M>: Send + Sync
where
M: IncomingMessage,
{
fn handle(&self, msg: &M) -> impl Future<Output = HandlerResult> + Send;
}
impl<M, F, Fut> Handler<M> for F
where
M: IncomingMessage,
F: Fn(&M) -> Fut + Send + Sync,
Fut: Future<Output = HandlerResult> + Send,
{
fn handle(&self, msg: &M) -> impl Future<Output = HandlerResult> + Send {
(self)(msg)
}
}
impl<M, H> Handler<M> for Arc<H>
where
M: IncomingMessage,
H: Handler<M>,
{
fn handle(&self, msg: &M) -> impl Future<Output = HandlerResult> + Send {
(**self).handle(msg)
}
}