use crate::types::RequestInfo;
use http_body_util::Full;
use hyper::Response;
use hyper::body::Bytes;
use std::future::Future;
pub use self::post::PostMiddleware;
pub use self::pre::PreMiddleware;
mod post;
mod pre;
#[derive(Debug)]
pub enum Middleware<E> {
Pre(PreMiddleware<E>),
Post(PostMiddleware<E>),
}
impl<E: Into<Box<dyn std::error::Error + Send + Sync>> + 'static> Middleware<E> {
pub fn pre<H, R>(handler: H) -> Middleware<E>
where
H: Fn(hyper::Request<Full<Bytes>>) -> R + Send + Sync + 'static,
R: Future<Output = Result<hyper::Request<Full<Bytes>>, E>> + Send + 'static,
{
Middleware::pre_with_path("/*", handler).unwrap()
}
pub fn post<H, R>(handler: H) -> Middleware<E>
where
H: Fn(Response<Full<Bytes>>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Full<Bytes>>, E>> + Send + 'static,
{
Middleware::post_with_path("/*", handler).unwrap()
}
pub fn post_with_info<H, R>(handler: H) -> Middleware<E>
where
H: Fn(Response<Full<Bytes>>, RequestInfo) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Full<Bytes>>, E>> + Send + 'static,
{
Middleware::post_with_info_with_path("/*", handler).unwrap()
}
pub fn pre_with_path<P, H, R>(path: P, handler: H) -> crate::Result<Middleware<E>>
where
P: Into<String>,
H: Fn(hyper::Request<Full<Bytes>>) -> R + Send + Sync + 'static,
R: Future<Output = Result<hyper::Request<Full<Bytes>>, E>> + Send + 'static,
{
Ok(Middleware::Pre(PreMiddleware::new(path, handler)?))
}
pub fn post_with_path<P, H, R>(path: P, handler: H) -> crate::Result<Middleware<E>>
where
P: Into<String>,
H: Fn(Response<Full<Bytes>>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Full<Bytes>>, E>> + Send + 'static,
{
Ok(Middleware::Post(PostMiddleware::new(path, handler)?))
}
pub fn post_with_info_with_path<P, H, R>(path: P, handler: H) -> crate::Result<Middleware<E>>
where
P: Into<String>,
H: Fn(Response<Full<Bytes>>, RequestInfo) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Full<Bytes>>, E>> + Send + 'static,
{
Ok(Middleware::Post(PostMiddleware::new_with_info(path, handler)?))
}
}