1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::error::exception_handler_trait::ExceptionHandler;
use crate::{Response, SilentError};
use async_trait::async_trait;
use std::future::Future;
use std::sync::Arc;

/// 处理器包装结构体
/// 包含
/// 请求类型: Option<Method>
/// 请求方法: Handler
/// 其中请求类型可为空,用来定义中间件
/// 请求方法不可为空,用来定义处理器
/// 处理器为返回值为 Into<Bytes> + From<Bytes>的异步函数或者闭包函数
pub struct ExceptionHandlerWrapper<F> {
    handler: F,
}

#[allow(dead_code)]
impl<F, T, Fut> ExceptionHandlerWrapper<F>
where
    Fut: Future<Output = T> + Send + 'static,
    F: Fn(SilentError) -> Fut,
    T: Into<Response>,
{
    pub fn new(handler: F) -> Self {
        Self { handler }
    }

    pub async fn handle(&self, err: SilentError) -> T {
        (self.handler)(err).await
    }

    pub fn arc(self) -> Arc<Self> {
        Arc::new(self)
    }
}

/// 为HandlerWrapper实现Handler
#[async_trait]
impl<F, T, Fut> ExceptionHandler for ExceptionHandlerWrapper<F>
where
    Fut: Future<Output = T> + Send + 'static,
    F: Fn(SilentError) -> Fut + Send + Sync + 'static,
    T: Into<Response>,
{
    async fn call(&self, err: SilentError) -> Response {
        self.handle(err).await.into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::StatusCode;

    async fn exception_handler(err: SilentError) -> SilentError {
        err
    }

    #[tokio::test]
    async fn handler_wrapper_match_req_works() {
        let handler_wrapper = ExceptionHandlerWrapper::new(exception_handler);
        assert_eq!(
            handler_wrapper
                .call(SilentError::business_error(StatusCode::OK, "".to_string()))
                .await
                .status_code,
            StatusCode::OK
        );
    }
}