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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::{future::Future, pin::Pin};

use axum::{
  extract::{FromRequest, FromRequestParts, Request},
  handler::Handler,
  response::{IntoResponse, Response},
};
pub use bytes::Bytes;
pub use prost::Message;

pub trait ToResponse {
  fn to_response(self, req: &Request) -> Response;
}

pub struct Msg(pub Bytes);

impl<T: prost::Message> From<T> for Msg {
  default fn from(t: T) -> Self {
    let msg = t.encode_to_vec();
    Msg(if msg.is_empty() {
      [0][..].into()
    } else {
      msg.into()
    })
  }
}

macro_rules! impl_from {
($($ty:ty),*) => {
$(
    impl From<$ty> for Msg {
        fn from(t: $ty) -> Self {
            Msg(t.into())
        }
    }
)*
};
}

impl From<()> for Msg {
  fn from(_: ()) -> Self {
    Msg(Default::default())
  }
}

impl_from!(Bytes, Vec<u8>, String);

impl IntoResponse for Msg {
  fn into_response(self) -> Response {
    self.0.into_response()
  }
}

#[macro_export]
macro_rules! ok {
  ($expr:expr) => {{
    let t: $crate::Msg = $expr.into();
    Ok(t)
  }};
}

#[macro_export]
macro_rules! msg {
()=>{
$crate::Result<impl Into<$crate::Msg>>
}
}

#[derive(Clone)]
pub struct FnAny<F>(pub F);

pub type Result<T> = crate::Result<T, crate::Err>;

// pub fn into_response(result: Result<impl Into<Any>>) -> Response {
//   match result {
//     Ok(r) => r.into().into_response(),
//     Err(err) => err.into_response(),
//   }
// }

pub async fn await_into_response(
  result: impl Future<Output = Result<impl Into<Msg>>> + Send,
) -> Response {
  match result.await {
    Ok(r) => r.into().into_response(),
    Err(err) => err.into_response(),
  }
}

impl<F, Fut, S, T: Into<Msg>> Handler<((),), S> for FnAny<F>
where
  F: FnOnce() -> Fut + Clone + Send + 'static,
  Fut: Future<Output = Result<T>> + Send,
{
  type Future = Pin<Box<dyn Future<Output = Response> + Send>>;

  fn call(self, _req: Request, _state: S) -> Self::Future {
    Box::pin(async move { await_into_response(self.0()).await })
  }
}

macro_rules! impl_handler {
(
[$($ty:ident),*], $last:ident
) => {

#[allow(non_snake_case, unused_mut)]
impl<F, Fut, S, M, T: Into<Msg>, $($ty,)* $last> Handler<(M, $($ty,)* $last,), S> for FnAny<F>
where
F: FnOnce($($ty,)* $last,) -> Fut + Clone + Send + 'static,
Fut: Future<Output = Result<T>> + Send,
S: Send + Sync + 'static,
$( $ty: FromRequestParts<S> + Send, )*
$last: FromRequest<S, M> + Send,
{
type Future = Pin<Box<dyn Future<Output = Response> + Send>>;

fn call(self, req: Request, state: S) -> Self::Future {
Box::pin(async move {
let (mut parts, body) = req.into_parts();
let state = &state;

$(
    let $ty = match $ty::from_request_parts(
        &mut parts,
        state
    ).await {
        Ok(value) => value,
        Err(rejection) => return rejection.into_response(),
    };
)*

    let req = Request::from_parts(parts, body);

    let $last = match $last::from_request(req, state).await {
        Ok(value) => value,
        Err(rejection) => return rejection.into_response(),
    };

    await_into_response(self.0($($ty,)* $last,)).await

})
}
}
};
}

macro_rules! all_the_tuples {
  ($name:ident) => {
    $name!([], T1);
    $name!([T1], T2);
    $name!([T1, T2], T3);
    $name!([T1, T2, T3], T4);
    $name!([T1, T2, T3, T4], T5);
    $name!([T1, T2, T3, T4, T5], T6);
    $name!([T1, T2, T3, T4, T5, T6], T7);
    $name!([T1, T2, T3, T4, T5, T6, T7], T8);
    $name!([T1, T2, T3, T4, T5, T6, T7, T8], T9);
    $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9], T10);
    $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], T11);
    $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11], T12);
    $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12], T13);
    $name!(
      [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13],
      T14
    );
    $name!(
      [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14],
      T15
    );
    $name!(
      [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15],
      T16
    );
  };
}

all_the_tuples!(impl_handler);