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
use crate::{
    extractor::Extractor,
    handler::{Handler, HandlerService},
    http::Method,
    service::BoxedService,
    Error, HttpRequest, HttpResponse, Responder,
};

pub fn to<F, T, R>(handler: F) -> Route<'static>
where
    F: Handler<T, R> + Send + Sync + 'static,
    T: Extractor<Error = Error> + Send + Sync + 'static,
    R: Responder + Send + Sync + 'static,
{
    Route {
        method: Method::Get,
        path: "/<to>",
        service: BoxedService::new(HandlerService::new(handler)),
    }
}

macro_rules! route {
    ($($fn:ident[$method:expr],)*) => {
        $(
            pub fn $fn(path: &str) -> Route<'_> {
                Route::new($method, path)
            }
        )*
    };
}

route![
    get[Method::Get],
    head[Method::Head],
    post[Method::Post],
    put[Method::Put],
    delete[Method::Delete],
    connect[Method::Connect],
    options[Method::Options],
    trace[Method::Trace],
    patch[Method::Patch],
];

pub(crate) fn not_found() -> HttpResponse {
    HttpResponse::not_found().finish()
}

pub struct Route<'s> {
    pub(crate) method: Method,
    pub(crate) path: &'s str,
    pub(crate) service: BoxedService<HttpRequest, HttpResponse, Error>,
}

impl<'s> Route<'s> {
    #[inline]
    pub(crate) fn new(method: Method, path: &'s str) -> Self {
        Self {
            method,
            path,
            service: BoxedService::new(HandlerService::new(not_found)),
        }
    }

    pub fn to<F, T, R>(mut self, handler: F) -> Self
    where
        F: Handler<T, R> + Send + Sync + 'static,
        T: Extractor<Error = Error> + Send + Sync + 'static,
        R: Responder + Send + Sync + 'static,
    {
        self.service = BoxedService::new(HandlerService::new(handler));

        self
    }
}