use std::{future::Future, sync::Arc};
use super::ResponseFuture;
#[derive(Clone)]
pub(super) enum Handler {
Sync(Arc<dyn Fn(String) -> String + Send + Sync + 'static>),
Async(Arc<dyn Fn(String) -> ResponseFuture + Send + Sync + 'static>),
}
impl Handler {
pub(super) fn sync(f: impl Fn(String) -> String + Send + Sync + 'static) -> Self {
Self::Sync(Arc::new(f))
}
pub(super) fn async_handler<F, Fut>(f: F) -> Self
where
F: Fn(String) -> Fut + Send + Sync + 'static,
Fut: Future<Output = String> + Send + 'static,
{
Self::Async(Arc::new(move |payload| Box::pin(f(payload))))
}
pub(super) fn call(self, payload: String) -> ResponseFuture {
match self {
Self::Sync(f) => Box::pin(async move { f(payload) }),
Self::Async(f) => Box::pin(async move { f(payload).await }),
}
}
}