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
pub fn handle<T, H: WrappedHandler<T>>(h: H) {
    let req = match ft_sdk::from_request::handler::current_request() {
        Ok(v) => v,
        Err(e) => {
            ft_sdk::println!("Error parsing request: {:?}", e);
            ft_sdk::error::handle_error(e);
            return;
        }
    };
    let resp = h.call(&req).and_then(Into::into).unwrap_or_else(|e| {
        ft_sdk::println!("Error: {:?}", e);
        ft_sdk::error::handle_error(e)
    });
    ft_sdk::http::send_response(resp);
}

pub trait WrappedHandler<T>: Sized {
    fn call(self, req: &http::Request<serde_json::Value>) -> ft_sdk::processor::Result;
}

fn wrap<T: ft_sdk::WrappedFromRequest>(
    t: T,
    o: ft_sdk::chr::CHR<ft_sdk::processor::Output>,
) -> ft_sdk::processor::Result {
    let ft_sdk::chr::CHR {
        cookies,
        headers,
        response,
    } = o;
    let response = match response {
        ft_sdk::processor::Output::Json(j) => ft_sdk::processor::Output::Json(t.wrap(j)?),
        _ => response,
    };
    Ok(ft_sdk::chr::CHR {
        cookies,
        headers,
        response,
    })
}

// why is the first element in all these, e.g. WrappedHandler<((), T), O> a ()? If we remove
// () from it, we start getting compilation error.
impl<F, T> WrappedHandler<((), T)> for F
where
    F: Fn(&mut T) -> ft_sdk::processor::Result,
    T: ft_sdk::WrappedFromRequest,
{
    fn call(self, req: &http::Request<serde_json::Value>) -> ft_sdk::processor::Result {
        let mut t = T::from_request(req)?;
        let o = (self)(&mut t)?;
        wrap(t, o)
    }
}

impl<F, T1, T2> WrappedHandler<((), T1, T2)> for F
where
    F: Fn(&mut T1, T2) -> ft_sdk::processor::Result,
    T1: ft_sdk::WrappedFromRequest,
    T2: ft_sdk::FromRequest,
{
    fn call(self, req: &http::Request<serde_json::Value>) -> ft_sdk::processor::Result {
        // TODO: try to parse both t1 and t2 and return result for both together to clients
        let mut t = T1::from_request(req)?;
        let o = (self)(&mut t, T2::from_request(req)?)?;
        wrap(t, o)
    }
}

impl<F, T1, T2, T3> WrappedHandler<((), T1, T2, T3)> for F
where
    F: Fn(&mut T1, T2, T3) -> ft_sdk::processor::Result,
    T1: ft_sdk::WrappedFromRequest,
    T2: ft_sdk::FromRequest,
    T3: ft_sdk::FromRequest,
{
    fn call(self, req: &http::Request<serde_json::Value>) -> ft_sdk::processor::Result {
        // TODO: try to parse both t1 and t2 and return result for both together to clients
        let mut t = T1::from_request(req)?;
        let o = (self)(&mut t, T2::from_request(req)?, T3::from_request(req)?)?;
        wrap(t, o)
    }
}

impl<F, T1, T2, T3, T4> WrappedHandler<((), T1, T2, T3, T4)> for F
where
    F: Fn(&mut T1, T2, T3, T4) -> ft_sdk::processor::Result,
    T1: ft_sdk::WrappedFromRequest,
    T2: ft_sdk::FromRequest,
    T3: ft_sdk::FromRequest,
    T4: ft_sdk::FromRequest,
{
    fn call(self, req: &http::Request<serde_json::Value>) -> ft_sdk::processor::Result {
        // TODO: try to parse both t1 and t2 and return result for both together to clients
        let mut t = T1::from_request(req)?;
        let o = (self)(
            &mut t,
            T2::from_request(req)?,
            T3::from_request(req)?,
            T4::from_request(req)?,
        )?;
        wrap(t, o)
    }
}

impl<F, T1, T2, T3, T4, T5> WrappedHandler<((), T1, T2, T3, T4, T5)> for F
where
    F: Fn(&mut T1, T2, T3, T4, T5) -> ft_sdk::processor::Result,
    T1: ft_sdk::WrappedFromRequest,
    T2: ft_sdk::FromRequest,
    T3: ft_sdk::FromRequest,
    T4: ft_sdk::FromRequest,
    T5: ft_sdk::FromRequest,
{
    fn call(self, req: &http::Request<serde_json::Value>) -> ft_sdk::processor::Result {
        // TODO: try to parse both t1 and t2 and return result for both together to clients
        let mut t = T1::from_request(req)?;
        let o = (self)(
            &mut t,
            T2::from_request(req)?,
            T3::from_request(req)?,
            T4::from_request(req)?,
            T5::from_request(req)?,
        )?;
        wrap(t, o)
    }
}

impl<F, T1, T2, T3, T4, T5, T6> WrappedHandler<((), T1, T2, T3, T4, T5, T6)> for F
where
    F: Fn(&mut T1, T2, T3, T4, T5, T6) -> ft_sdk::processor::Result,
    T1: ft_sdk::WrappedFromRequest,
    T2: ft_sdk::FromRequest,
    T3: ft_sdk::FromRequest,
    T4: ft_sdk::FromRequest,
    T5: ft_sdk::FromRequest,
    T6: ft_sdk::FromRequest,
{
    fn call(self, req: &http::Request<serde_json::Value>) -> ft_sdk::processor::Result {
        // TODO: try to parse both t1 and t2 and return result for both together to clients
        let mut t = T1::from_request(req)?;
        let o = (self)(
            &mut t,
            T2::from_request(req)?,
            T3::from_request(req)?,
            T4::from_request(req)?,
            T5::from_request(req)?,
            T6::from_request(req)?,
        )?;
        wrap(t, o)
    }
}