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