use crate::regex_generator::generate_exact_match_regex;
use crate::types::RequestInfo;
use crate::Error;
use hyper::{body::HttpBody, Response};
use regex::Regex;
use std::fmt::{self, Debug, Formatter};
use std::future::Future;
use std::pin::Pin;
type HandlerWithoutInfo<B, E> = Box<dyn FnMut(Response<B>) -> HandlerWithoutInfoReturn<B, E> + Send + Sync + 'static>;
type HandlerWithoutInfoReturn<B, E> = Box<dyn Future<Output = Result<Response<B>, E>> + Send + 'static>;
type HandlerWithInfo<B, E> =
Box<dyn FnMut(Response<B>, RequestInfo) -> HandlerWithInfoReturn<B, E> + Send + Sync + 'static>;
type HandlerWithInfoReturn<B, E> = Box<dyn Future<Output = Result<Response<B>, E>> + Send + 'static>;
pub struct PostMiddleware<B, E> {
pub(crate) path: String,
pub(crate) regex: Regex,
pub(crate) handler: Option<Handler<B, E>>,
}
pub(crate) enum Handler<B, E> {
WithoutInfo(HandlerWithoutInfo<B, E>),
WithInfo(HandlerWithInfo<B, E>),
}
impl<
B: HttpBody + Send + Sync + Unpin + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync>> + Unpin + 'static,
> PostMiddleware<B, E>
{
pub(crate) fn new_with_boxed_handler<P: Into<String>>(
path: P,
handler: Handler<B, E>,
) -> crate::Result<PostMiddleware<B, E>> {
let path = path.into();
let (re, _) = generate_exact_match_regex(path.as_str())?;
Ok(PostMiddleware {
path,
regex: re,
handler: Some(handler),
})
}
pub fn new<P, H, R>(path: P, mut handler: H) -> crate::Result<PostMiddleware<B, E>>
where
P: Into<String>,
H: FnMut(Response<B>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
{
let handler: HandlerWithoutInfo<B, E> = Box::new(move |res: Response<B>| Box::new(handler(res)));
PostMiddleware::new_with_boxed_handler(path, Handler::WithoutInfo(handler))
}
pub fn new_with_info<P, H, R>(path: P, mut handler: H) -> crate::Result<PostMiddleware<B, E>>
where
P: Into<String>,
H: FnMut(Response<B>, RequestInfo) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
{
let handler: HandlerWithInfo<B, E> =
Box::new(move |res: Response<B>, req_info: RequestInfo| Box::new(handler(res, req_info)));
PostMiddleware::new_with_boxed_handler(path, Handler::WithInfo(handler))
}
pub(crate) fn should_require_req_meta(&self) -> bool {
if let Some(ref handler) = self.handler {
match handler {
Handler::WithInfo(_) => true,
Handler::WithoutInfo(_) => false,
}
} else {
false
}
}
pub(crate) async fn process(
&mut self,
res: Response<B>,
req_info: Option<RequestInfo>,
) -> crate::Result<Response<B>> {
let handler = self
.handler
.as_mut()
.expect("A router can not be used after mounting into another router");
match handler {
Handler::WithoutInfo(ref mut handler) => Pin::from(handler(res))
.await
.map_err(|e| Error::HandlePostMiddlewareWithoutInfoRequest(e.into())),
Handler::WithInfo(ref mut handler) => {
Pin::from(handler(res, req_info.expect("No RequestInfo is provided")))
.await
.map_err(|e| Error::HandlePostMiddlewareWithInfoRequest(e.into()))
}
}
}
}
impl<B, E> Debug for PostMiddleware<B, E> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{{ path: {:?}, regex: {:?} }}", self.path, self.regex)
}
}