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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::{future::Future, marker::PhantomData};

use crate::actix_web::{
    dev::Handler as ActixHandler,
    web::{Data, Json, Query},
    *,
};
use ajars_core::{HttpMethod, Rest};
use serde::{de::DeserializeOwned, Serialize};

pub mod actix_web {
    pub use actix_web::*;
}

pub trait HandleActix<I: Serialize + DeserializeOwned + 'static, O: Serialize + DeserializeOwned + 'static> {
    fn handle<H, D, R, E>(&self, handler: H) -> Resource
    where
        H: Handler<D, R, E, I, O>,
        D: 'static,
        R: Future<Output = Result<Json<O>, E>> + 'static,
        E: ResponseError + 'static;
}

impl <I: Serialize + DeserializeOwned + 'static, O: Serialize + DeserializeOwned + 'static, REST: Rest<I,O>> HandleActix<I, O>
    for REST
{
    fn handle<H, D, R, E>(&self, handler: H) -> Resource
    where
        H: Handler<D, R, E, I, O>,
        D: 'static,
        R: Future<Output = Result<Json<O>, E>> + 'static,
        E: ResponseError + 'static,
    {
        let resource = web::resource::<&str>(self.path());

        match self.method() {
            HttpMethod::DELETE => {
                resource.route(web::delete().to(QueryHandlerWrapper::new(handler)))
            }
            HttpMethod::GET => {
                resource.route(web::get().to(QueryHandlerWrapper::new(handler)))
            }
            HttpMethod::POST => {
                resource.route(web::post().to(JsonHandlerWrapper::new(handler)))
            }
            HttpMethod::PUT => {
                resource.route(web::put().to(JsonHandlerWrapper::new(handler)))
            }
        }
    }
}

pub trait Handler<D, R, E, I, O>: Clone + 'static
where
    R: Future<Output = Result<Json<O>, E>>,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    fn call(&self, param: (HttpRequest, Data<D>, I)) -> R;
}

impl<F, D, R, E, I, O> Handler<D, R, E, I, O> for F
where
    F: 'static + Clone + Fn(HttpRequest, Data<D>, I) -> R,
    R: Future<Output = Result<Json<O>, E>>,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    fn call(&self, param: (HttpRequest, Data<D>, I)) -> R {
        self(param.0, param.1, param.2)
    }
}

pub struct JsonHandlerWrapper<H, D, R, E, I, O>
where
    H: Handler<D, R, E, I, O> + Clone + 'static,
    R: Future<Output = Result<Json<O>, E>>,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    handler: H,
    phantom_d: PhantomData<D>,
    phantom_r: PhantomData<R>,
    phantom_e: PhantomData<E>,
    phantom_i: PhantomData<I>,
    phantom_o: PhantomData<O>,
}

impl<H, D, R, E, I, O> JsonHandlerWrapper<H, D, R, E, I, O>
where
    H: Handler<D, R, E, I, O> + Clone + 'static,
    R: Future<Output = Result<Json<O>, E>>,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    pub fn new(handler: H) -> Self {
        Self {
            handler,
            phantom_d: PhantomData,
            phantom_r: PhantomData,
            phantom_e: PhantomData,
            phantom_i: PhantomData,
            phantom_o: PhantomData,
        }
    }
}

impl<H, D, R, E, I, O> Clone for JsonHandlerWrapper<H, D, R, E, I, O>
where
    H: Handler<D, R, E, I, O> + Clone + 'static,
    R: Future<Output = Result<Json<O>, E>>,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    fn clone(&self) -> Self {
        Self::new(self.handler.clone())
    }
}

impl<H, D, R, E, I, O> ActixHandler<(HttpRequest, Data<D>, Json<I>), R>
    for JsonHandlerWrapper<H, D, R, E, I, O>
where
    H: Handler<D, R, E, I, O> + Clone + 'static,
    D: 'static,
    R: Future<Output = Result<Json<O>, E>> + 'static,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    fn call(&self, param: (HttpRequest, Data<D>, Json<I>)) -> R {
        self.handler.call((param.0, param.1, param.2.into_inner()))
    }
}

pub struct QueryHandlerWrapper<H, D, R, E, I, O>
where
    H: Handler<D, R, E, I, O> + Clone + 'static,
    R: Future<Output = Result<Json<O>, E>>,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    handler: H,
    phantom_d: PhantomData<D>,
    phantom_r: PhantomData<R>,
    phantom_e: PhantomData<E>,
    phantom_i: PhantomData<I>,
    phantom_o: PhantomData<O>,
}

impl<H, D, R, E, I, O> QueryHandlerWrapper<H, D, R, E, I, O>
where
    H: Handler<D, R, E, I, O> + Clone + 'static,
    R: Future<Output = Result<Json<O>, E>>,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    pub fn new(handler: H) -> Self {
        Self {
            handler,
            phantom_d: PhantomData,
            phantom_r: PhantomData,
            phantom_e: PhantomData,
            phantom_i: PhantomData,
            phantom_o: PhantomData,
        }
    }
}

impl<H, D, R, E, I, O> Clone for QueryHandlerWrapper<H, D, R, E, I, O>
where
    H: Handler<D, R, E, I, O> + Clone + 'static,
    R: Future<Output = Result<Json<O>, E>>,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    fn clone(&self) -> Self {
        Self::new(self.handler.clone())
    }
}

impl<H, D, R, E, I, O> ActixHandler<(HttpRequest, Data<D>, Query<I>), R>
    for QueryHandlerWrapper<H, D, R, E, I, O>
where
    H: Handler<D, R, E, I, O> + Clone + 'static,
    D: 'static,
    R: Future<Output = Result<Json<O>, E>> + 'static,
    E: ResponseError + 'static,
    I: Serialize + DeserializeOwned + 'static,
    O: Serialize + DeserializeOwned + 'static,
{
    fn call(&self, param: (HttpRequest, Data<D>, Query<I>)) -> R {
        self.handler.call((param.0, param.1, param.2.into_inner()))
    }
}