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
pub use axum::{
  self, async_trait,
  extract::FromRequestParts,
  http::{request::Parts, StatusCode},
  response::{IntoResponse, Response},
};

pub fn middleware(r: anyhow::Result<Response>) -> Response {
  match r {
    Ok(r) => r,
    Err(err) => {
      tracing::error!("{:?}", err);
      (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response()
    }
  }
}

#[macro_export]
macro_rules! from_request_parts {
  ($cls:ty, $func:expr) => {
    #[$crate::async_trait]
    impl<S> $crate::FromRequestParts<S> for $cls
    where
      S: Send + Sync,
    {
      type Rejection = $crate::Response;

      async fn from_request_parts(
        parts: &mut $crate::Parts,
        _state: &S,
      ) -> Result<Self, Self::Rejection> {
        let func = $func;
        let r: anyhow::Result<Self> = func(parts).await;
        match r {
          Ok(r) => Ok(r),
          Err(err) => {
            tracing::error!("{}", err);
            use $crate::IntoResponse;
            Err(($crate::StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())
          }
        }
      }
    }
  };
}