1use super::*;
4
5macro_rules! define_codes {
6(
7 $(
8 $(#[$meta:meta])*
9 $(fn $fn_name:ident ();)?
10 code $name:ident => $status:expr;
11 )*
12) => {
13 $(
14 $(#[$meta])*
15 #[derive(Debug, Clone, Copy, Default)]
16 pub struct $name<T = ()>(pub T);
17
18 impl<T> StatusProvider for $name<T> {
19 const STATUS_CODE: StatusCode = $status;
20 type Inner = T;
21 type WithInner<R> = $name<R>;
22 fn into_inner(self) -> Self::Inner {
23 self.0
24 }
25 }
26
27 impl<T, S> From<$name<T>> for ApiResponse<S>
28 where
29 S: Contains<$name<T>>,
30 $name<T>: StatusProvider<Inner: IntoResponse>,
31 {
32 fn from(err: $name<T>) -> Self {
33 ApiResponse::new(err)
34 }
35 }
36
37 impl<T> From<T> for $name<T> {
38 fn from(inner: T) -> Self {
39 Self(inner)
40 }
41 }
42
43 impl<T> std::ops::Deref for $name<T> {
44 type Target = T;
45
46 fn deref(&self) -> &Self::Target {
47 &self.0
48 }
49 }
50
51 impl<T> std::ops::DerefMut for $name<T> {
52 fn deref_mut(&mut self) -> &mut Self::Target {
53 &mut self.0
54 }
55 }
56 )*
57};
58}
59
60define_codes!(
61 fn map_bad_request();
63 code BadRequest => StatusCode::BAD_REQUEST;
64
65 fn map_unauthorized();
67 code Unauthorized => StatusCode::UNAUTHORIZED;
68
69 fn map_payment_required();
71 code PaymentRequired => StatusCode::PAYMENT_REQUIRED;
72
73 fn map_forbidden();
75 code Forbidden => StatusCode::FORBIDDEN;
76
77 fn map_not_found();
79 code NotFound => StatusCode::NOT_FOUND;
80
81 fn map_method_not_allowed();
83 code MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED;
84
85 fn map_not_acceptable();
87 code NotAcceptable => StatusCode::NOT_ACCEPTABLE;
88
89 fn map_proxy_authentication_required();
91 code ProxyAuthenticationRequired => StatusCode::PROXY_AUTHENTICATION_REQUIRED;
92
93 fn map_request_timeout();
95 code RequestTimeout => StatusCode::REQUEST_TIMEOUT;
96
97 fn map_conflict();
99 code Conflict => StatusCode::CONFLICT;
100
101 fn map_gone();
103 code Gone => StatusCode::GONE;
104
105 fn map_length_required();
107 code LengthRequired => StatusCode::LENGTH_REQUIRED;
108
109 fn map_precondition_failed();
111 code PreconditionFailed => StatusCode::PRECONDITION_FAILED;
112
113 fn map_payload_too_large();
115 code PayloadTooLarge => StatusCode::PAYLOAD_TOO_LARGE;
116
117 fn map_uri_too_long();
119 code UriTooLong => StatusCode::URI_TOO_LONG;
120
121 fn map_unsupported_media_type();
123 code UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE;
124
125 fn map_range_not_satisfiable();
127 code RangeNotSatisfiable => StatusCode::RANGE_NOT_SATISFIABLE;
128
129 fn map_expectation_failed();
131 code ExpectationFailed => StatusCode::EXPECTATION_FAILED;
132
133 fn map_im_a_teapot();
135 code ImATeapot => StatusCode::IM_A_TEAPOT;
136
137 fn map_misdirected_request();
139 code MisdirectedRequest => StatusCode::MISDIRECTED_REQUEST;
140
141 fn map_unprocessable_entity();
143 code UnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY;
144
145 fn map_locked();
147 code Locked => StatusCode::LOCKED;
148
149 fn map_failed_dependency();
151 code FailedDependency => StatusCode::FAILED_DEPENDENCY;
152
153 fn map_too_early();
155 code TooEarly => StatusCode::TOO_EARLY;
156
157 fn map_upgrade_required();
159 code UpgradeRequired => StatusCode::UPGRADE_REQUIRED;
160
161 fn map_precondition_required();
163 code PreconditionRequired => StatusCode::PRECONDITION_REQUIRED;
164
165 fn map_too_many_requests();
167 code TooManyRequests => StatusCode::TOO_MANY_REQUESTS;
168
169 fn map_request_header_fields_too_large();
171 code RequestHeaderFieldsTooLarge => StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE;
172
173 fn map_unavailable_for_legal_reasons();
175 code UnavailableForLegalReasons => StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS;
176
177 fn map_internal();
179 code Internal => StatusCode::INTERNAL_SERVER_ERROR;
180
181 fn map_not_implemented();
183 code NotImplemented => StatusCode::NOT_IMPLEMENTED;
184
185 fn map_bad_gateway();
187 code BadGateway => StatusCode::BAD_GATEWAY;
188
189 fn map_service_unavailable();
191 code ServiceUnavailable => StatusCode::SERVICE_UNAVAILABLE;
192
193 fn map_gateway_timeout();
195 code GatewayTimeout => StatusCode::GATEWAY_TIMEOUT;
196
197 fn map_http_version_not_supported();
199 code HttpVersionNotSupported => StatusCode::HTTP_VERSION_NOT_SUPPORTED;
200
201 fn map_variant_also_negotiates();
203 code VariantAlsoNegotiates => StatusCode::VARIANT_ALSO_NEGOTIATES;
204
205 fn map_insufficient_storage();
207 code InsufficientStorage => StatusCode::INSUFFICIENT_STORAGE;
208
209 fn map_loop_detected();
211 code LoopDetected => StatusCode::LOOP_DETECTED;
212
213 fn map_not_extended();
215 code NotExtended => StatusCode::NOT_EXTENDED;
216
217 fn map_network_authentication_required();
219 code NetworkAuthenticationRequired => StatusCode::NETWORK_AUTHENTICATION_REQUIRED;
220);