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
203
204
205
206
use super::Method;
use serde_json::Value;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;

pub use matchit::Router;

type Handler = Box<
    dyn Fn(
        Vec<(String, String)>,
        HashMap<String, Value>,
        Vec<u8>,
    ) -> Pin<Box<dyn Future<Output = ()>>>,
>;

/// Helper for wrapping function to a Handler, and then binding to 'GET' method.
pub fn get<T>(
    f: fn(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> T,
) -> (Method, Handler)
where
    T: Future<Output = ()> + 'static,
{
    (Method::GET, Box::new(move |a, b, c| Box::pin(f(a, b, c))))
}

/// Helper for wrapping function to a Handler, and then binding to 'OPTIONS' method.
pub fn options<T>(
    f: fn(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> T,
) -> (Method, Handler)
where
    T: Future<Output = ()> + 'static,
{
    (
        Method::OPTIONS,
        Box::new(move |a, b, c| Box::pin(f(a, b, c))),
    )
}

/// Helper for wrapping function to a Handler, and then binding to 'POST' method.
pub fn post<T>(
    f: fn(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> T,
) -> (Method, Handler)
where
    T: Future<Output = ()> + 'static,
{
    (Method::POST, Box::new(move |a, b, c| Box::pin(f(a, b, c))))
}

/// Helper for wrapping function to a Handler, and then binding to 'PUT' method.
pub fn put<T>(
    f: fn(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> T,
) -> (Method, Handler)
where
    T: Future<Output = ()> + 'static,
{
    (Method::PUT, Box::new(move |a, b, c| Box::pin(f(a, b, c))))
}

/// Helper for wrapping function to a Handler, and then binding to 'DELETE' method.
pub fn delete<T>(
    f: fn(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> T,
) -> (Method, Handler)
where
    T: Future<Output = ()> + 'static,
{
    (
        Method::DELETE,
        Box::new(move |a, b, c| Box::pin(f(a, b, c))),
    )
}

/// Helper for wrapping function to a Handler, and then binding to 'HEAD' method.
pub fn head<T>(
    f: fn(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> T,
) -> (Method, Handler)
where
    T: Future<Output = ()> + 'static,
{
    (Method::HEAD, Box::new(move |a, b, c| Box::pin(f(a, b, c))))
}

/// Helper for wrapping function to a Handler, and then binding to 'TRACE' method.
pub fn trace<T>(
    f: fn(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> T,
) -> (Method, Handler)
where
    T: Future<Output = ()> + 'static,
{
    (Method::TRACE, Box::new(move |a, b, c| Box::pin(f(a, b, c))))
}

/// Helper for wrapping function to a Handler, and then binding to 'PATCH' method.
pub fn patch<T>(
    f: fn(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> T,
) -> (Method, Handler)
where
    T: Future<Output = ()> + 'static,
{
    (Method::PATCH, Box::new(move |a, b, c| Box::pin(f(a, b, c))))
}

extern "C" {
    fn get_event_method_length() -> i32;
    fn get_event_method(p: *mut u8) -> i32;
    fn get_event_headers_length() -> i32;
    fn get_event_headers(p: *mut u8) -> i32;
    fn get_event_query_length() -> i32;
    fn get_event_query(p: *mut u8) -> i32;
    fn get_event_subpath_length() -> i32;
    fn get_event_subpath(p: *mut u8) -> i32;
    fn get_event_body_length() -> i32;
    fn get_event_body(p: *mut u8) -> i32;
}

fn get_request() -> (
    Method,
    Vec<(String, String)>,
    String,
    HashMap<String, Value>,
    Vec<u8>,
) {
    unsafe {
        let l = get_event_method_length();
        let mut event_method = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_method(event_method.as_mut_ptr());
        assert!(c == l);
        event_method.set_len(c as usize);
        let event_method = Method::from_bytes(&event_method).unwrap();

        let l = get_event_headers_length();
        let mut event_headers = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_headers(event_headers.as_mut_ptr());
        assert!(c == l);
        event_headers.set_len(c as usize);
        let event_headers = serde_json::from_slice(&event_headers).unwrap();

        let l = get_event_query_length();
        let mut event_query = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_query(event_query.as_mut_ptr());
        assert!(c == l);
        event_query.set_len(c as usize);
        let event_query = serde_json::from_slice(&event_query).unwrap();

        let l = get_event_subpath_length();
        let mut event_subpath = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_subpath(event_subpath.as_mut_ptr());
        assert!(c == l);
        event_subpath.set_len(c as usize);
        let event_subpath = String::from_utf8_lossy(&event_subpath).into_owned();

        let l = get_event_body_length();
        let mut event_body = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_body(event_body.as_mut_ptr());
        assert!(c == l);
        event_body.set_len(c as usize);

        (
            event_method,
            event_headers,
            event_subpath,
            event_query,
            event_body,
        )
    }
}

/// Route error types
pub enum RouteError {
    NotFound,
    MethodNotAllowed,
}

/// Route path to handler.
/// For calling the exact handler, [construct the router](Router) then pass it to this function.
/// ```rust
/// let mut router = Router::new();
/// router
///     .insert("/options", vec![options(opt)])
///     .unwrap();
/// router
///     .insert(
///         "/get/:city",
///         vec![options(opt), get(query)],
///     )
///     .unwrap();
/// if let Err(e) = route(router).await {
///     send_response(404, vec![], b"No route matched".to_vec())
/// }
/// ```
pub async fn route(router: Router<Vec<(Method, Handler)>>) -> Result<(), RouteError> {
    let (method, headers, subpath, mut qry, body) = get_request();
    let matched = router.at(subpath.as_str()).or(Err(RouteError::NotFound))?;
    for p in matched.params.iter() {
        qry.insert(String::from(p.0), Value::from(p.1));
    }
    let mh = matched.value;
    for (m, h) in mh.iter() {
        if m.eq(&method) {
            h(headers, qry, body).await;
            return Ok(());
        }
    }

    Err(RouteError::MethodNotAllowed)
}