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