heart/
response.rs

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
use crate::maud::Markup;
use serde::Serialize;
use warp::http::{Response, StatusCode};
use warp::reply;
use warp::reply::WithStatus;

pub fn hx_with(
    status: StatusCode,
    res: Markup,
) -> Response<String> {
    Response::builder()
        .header("Content", "text/html")
        .status(status)
        .body(res.into_string())
        // TODO: verify that this is the case
        .expect("BUG: impossible")
}

macro_rules! generate_hx_status_functions {
    ($($fname:ident, $status:ident);* $(;)?) => {
        $(
            pub fn $fname(res: Markup) -> Response<String> {
                hx_with(StatusCode::$status, res)
            }
        )*
    };
}

generate_hx_status_functions! {
    // funny aliases
    hx, OK;
    hx_error, IM_A_TEAPOT;

    // 1XX
    hx_continue, CONTINUE;
    hx_switching_protocols, SWITCHING_PROTOCOLS;
    hx_processing, PROCESSING;

    // 2XX
    hx_ok, OK;
    hx_created, CREATED;
    hx_accepted, ACCEPTED;
    hx_non_authoritative_information, NON_AUTHORITATIVE_INFORMATION;
    hx_no_content, NO_CONTENT;
    hx_reset_content, RESET_CONTENT;
    hx_partial_content, PARTIAL_CONTENT;
    hx_multi_status, MULTI_STATUS;
    hx_already_reported, ALREADY_REPORTED;
    hx_im_used, IM_USED;

    // 3XX
    hx_multiple_choices, MULTIPLE_CHOICES;
    hx_moved_permanently, MOVED_PERMANENTLY;
    hx_found, FOUND;
    hx_see_other, SEE_OTHER;
    hx_not_modified, NOT_MODIFIED;
    hx_use_proxy, USE_PROXY;
    hx_temporary_redirect, TEMPORARY_REDIRECT;
    hx_permanent_redirect, PERMANENT_REDIRECT;

    // 4XX
    hx_bad_request, BAD_REQUEST;
    hx_unauthorized, UNAUTHORIZED;
    hx_payment_required, PAYMENT_REQUIRED;
    hx_forbidden, FORBIDDEN;
    hx_not_found, NOT_FOUND;
    hx_method_not_allowed, METHOD_NOT_ALLOWED;
    hx_not_acceptable, NOT_ACCEPTABLE;
    hx_proxy_authentication_required, PROXY_AUTHENTICATION_REQUIRED;
    hx_request_timeout, REQUEST_TIMEOUT;
    hx_conflict, CONFLICT;
    hx_gone, GONE;
    hx_length_required, LENGTH_REQUIRED;
    hx_precondition_failed, PRECONDITION_FAILED;
    hx_payload_too_large, PAYLOAD_TOO_LARGE;
    hx_uri_too_long, URI_TOO_LONG;
    hx_unsupported_media_type, UNSUPPORTED_MEDIA_TYPE;
    hx_range_not_satisfiable, RANGE_NOT_SATISFIABLE;
    hx_expectation_failed, EXPECTATION_FAILED;
    hx_im_a_teapot, IM_A_TEAPOT;
    hx_misdirected_request, MISDIRECTED_REQUEST;
    hx_unprocessable_entity, UNPROCESSABLE_ENTITY;
    hx_locked, LOCKED;
    hx_failed_dependency, FAILED_DEPENDENCY;
    hx_upgrade_required, UPGRADE_REQUIRED;
    hx_precondition_required, PRECONDITION_REQUIRED;
    hx_too_many_requests, TOO_MANY_REQUESTS;
    hx_request_header_fields_too_large, REQUEST_HEADER_FIELDS_TOO_LARGE;
    hx_unavailable_for_legal_reasons, UNAVAILABLE_FOR_LEGAL_REASONS;

    // 5XX
    hx_internal_server_error, INTERNAL_SERVER_ERROR;
    hx_not_implemented, NOT_IMPLEMENTED;
    hx_bad_gateway, BAD_GATEWAY;
    hx_service_unavailable, SERVICE_UNAVAILABLE;
    hx_gateway_timeout, GATEWAY_TIMEOUT;
    hx_http_version_not_supported, HTTP_VERSION_NOT_SUPPORTED;
    hx_variant_also_negotiates, VARIANT_ALSO_NEGOTIATES;
    hx_insufficient_storage, INSUFFICIENT_STORAGE;
    hx_loop_detected, LOOP_DETECTED;
    hx_not_extended, NOT_EXTENDED;
    hx_network_authentication_required, NETWORK_AUTHENTICATION_REQUIRED
}

pub fn js_with<T: Serialize>(
    data: &T,
    status_code: &StatusCode,
) -> WithStatus<String> {
    let json =
        serde_json::to_string(data).expect("BUG: Impossible");
    reply::with_status(json, *status_code)
}

macro_rules! generate_js_status_functions {
    ($($fname:ident, $status:ident);* $(;)?) => {
        $(
            pub fn $fname<T: Serialize>(
                data: &T,
            ) -> WithStatus<String> {
                js_with(data, &StatusCode::$status)
            }
        )*
    };
}

generate_js_status_functions! {
    // funny aliases
    js, OK;
    js_error, IM_A_TEAPOT;

    // 1XX
    js_continue, CONTINUE;
    js_switching_protocols, SWITCHING_PROTOCOLS;
    js_processing, PROCESSING;

    // 2XX
    js_ok, OK;
    js_created, CREATED;
    js_accepted, ACCEPTED;
    js_non_authoritative_information, NON_AUTHORITATIVE_INFORMATION;
    js_no_content, NO_CONTENT;
    js_reset_content, RESET_CONTENT;
    js_partial_content, PARTIAL_CONTENT;
    js_multi_status, MULTI_STATUS;
    js_already_reported, ALREADY_REPORTED;
    js_im_used, IM_USED;

    // 3XX
    js_multiple_choices, MULTIPLE_CHOICES;
    js_moved_permanently, MOVED_PERMANENTLY;
    js_found, FOUND;
    js_see_other, SEE_OTHER;
    js_not_modified, NOT_MODIFIED;
    js_use_proxy, USE_PROXY;
    js_temporary_redirect, TEMPORARY_REDIRECT;
    js_permanent_redirect, PERMANENT_REDIRECT;

    // 4XX
    js_bad_request, BAD_REQUEST;
    js_unauthorized, UNAUTHORIZED;
    js_payment_required, PAYMENT_REQUIRED;
    js_forbidden, FORBIDDEN;
    js_not_found, NOT_FOUND;
    js_method_not_allowed, METHOD_NOT_ALLOWED;
    js_not_acceptable, NOT_ACCEPTABLE;
    js_proxy_authentication_required, PROXY_AUTHENTICATION_REQUIRED;
    js_request_timeout, REQUEST_TIMEOUT;
    js_conflict, CONFLICT;
    js_gone, GONE;
    js_length_required, LENGTH_REQUIRED;
    js_precondition_failed, PRECONDITION_FAILED;
    js_payload_too_large, PAYLOAD_TOO_LARGE;
    js_uri_too_long, URI_TOO_LONG;
    js_unsupported_media_type, UNSUPPORTED_MEDIA_TYPE;
    js_range_not_satisfiable, RANGE_NOT_SATISFIABLE;
    js_expectation_failed, EXPECTATION_FAILED;
    js_im_a_teapot, IM_A_TEAPOT;
    js_misdirected_request, MISDIRECTED_REQUEST;
    js_unprocessable_entity, UNPROCESSABLE_ENTITY;
    js_locked, LOCKED;
    js_failed_dependency, FAILED_DEPENDENCY;
    js_upgrade_required, UPGRADE_REQUIRED;
    js_precondition_required, PRECONDITION_REQUIRED;
    js_too_many_requests, TOO_MANY_REQUESTS;
    js_request_header_fields_too_large, REQUEST_HEADER_FIELDS_TOO_LARGE;
    js_unavailable_for_legal_reasons, UNAVAILABLE_FOR_LEGAL_REASONS;

    // 5XX
    js_internal_server_error, INTERNAL_SERVER_ERROR;
    js_not_implemented, NOT_IMPLEMENTED;
    js_bad_gateway, BAD_GATEWAY;
    js_service_unavailable, SERVICE_UNAVAILABLE;
    js_gateway_timeout, GATEWAY_TIMEOUT;
    js_http_version_not_supported, HTTP_VERSION_NOT_SUPPORTED;
    js_variant_also_negotiates, VARIANT_ALSO_NEGOTIATES;
    js_insufficient_storage, INSUFFICIENT_STORAGE;
    js_loop_detected, LOOP_DETECTED;
    js_not_extended, NOT_EXTENDED;
    js_network_authentication_required, NETWORK_AUTHENTICATION_REQUIRED
}