1use super::*;
2
3macro_rules! define_codes {
4(
5 $(
6 $(#[$meta:meta])*
7 $(fn $fn_name:ident ();)?
8 code $name:ident => $status:expr;
9 )*
10) => {
11 $(
12 $(#[$meta])*
13 #[derive(Debug, Clone, Copy, Default)]
14 pub struct $name<T = ()>(pub T);
15
16 impl<T> WrapsResponse for $name<T> {
17 const STATUS_CODE: StatusCode = $status;
18 type Inner = T;
19 type Pure = $name;
20 fn into_inner(self) -> Self::Inner {
21 self.0
22 }
23 }
24
25 impl<T, S> From<$name<T>> for ApiResponse<S>
26 where
27 S: Contains<$name<T>>,
28 $name<T>: WrapsResponse<Inner: IntoResponse>,
29 {
30 fn from(err: $name<T>) -> Self {
31 ApiResponse::new(err)
32 }
33 }
34
35 impl<T> From<T> for $name<T> {
36 fn from(inner: T) -> Self {
37 Self(inner)
38 }
39 }
40
41 impl<T> std::ops::Deref for $name<T> {
42 type Target = T;
43
44 fn deref(&self) -> &Self::Target {
45 &self.0
46 }
47 }
48
49 impl<T> std::ops::DerefMut for $name<T> {
50 fn deref_mut(&mut self) -> &mut Self::Target {
51 &mut self.0
52 }
53 }
54 )*
55
56 pub trait ApiResultExt<T, E> {
57 $(
58 $(
59 fn $fn_name<R>(self) -> Result<T, $name<R>>
60 where
61 E: Into<R>;
62 )?
63 )*
64 }
65
66 impl<T, E> ApiResultExt<T, E> for Result<T, E> {
67 $(
68 $(
69 fn $fn_name<R>(self) -> Result<T, $name<R>>
70 where
71 E: Into<R>,
72 {
73 match self {
74 Ok(val) => Ok(val),
75 Err(e) => Err($name(e.into())),
76 }
77 }
78 )?
79 )*
80 }
81};
82}
83
84define_codes!(
85 fn bad_request();
87 code BadRequest => StatusCode::BAD_REQUEST;
88
89 fn unauthorized_err();
91 code Unauthorized => StatusCode::UNAUTHORIZED;
92
93 fn payment_required_err();
95 code PaymentRequired => StatusCode::PAYMENT_REQUIRED;
96
97 fn forbidden_err();
99 code Forbidden => StatusCode::FORBIDDEN;
100
101 fn not_found_err();
103 code NotFound => StatusCode::NOT_FOUND;
104
105 fn method_not_allowed_err();
107 code MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED;
108
109 fn not_acceptable_err();
111 code NotAcceptable => StatusCode::NOT_ACCEPTABLE;
112
113 fn proxy_authentication_required_err();
115 code ProxyAuthenticationRequired => StatusCode::PROXY_AUTHENTICATION_REQUIRED;
116
117 fn request_timeout_err();
119 code RequestTimeout => StatusCode::REQUEST_TIMEOUT;
120
121 fn conflict_err();
123 code Conflict => StatusCode::CONFLICT;
124
125 fn gone_err();
127 code Gone => StatusCode::GONE;
128
129 fn length_required_err();
131 code LengthRequired => StatusCode::LENGTH_REQUIRED;
132
133 fn precondition_failed_err();
135 code PreconditionFailed => StatusCode::PRECONDITION_FAILED;
136
137 fn uri_too_long_err();
142 code UriTooLong => StatusCode::URI_TOO_LONG;
143
144 fn unsupported_media_type_err();
146 code UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE;
147
148 fn range_not_satisfiable_err();
150 code RangeNotSatisfiable => StatusCode::RANGE_NOT_SATISFIABLE;
151
152 fn expectation_failed_err();
154 code ExpectationFailed => StatusCode::EXPECTATION_FAILED;
155
156 fn im_a_teapot_err();
158 code ImATeapot => StatusCode::IM_A_TEAPOT;
159
160 fn misdirected_request_err();
162 code MisdirectedRequest => StatusCode::MISDIRECTED_REQUEST;
163
164 fn unprocessable_entity_err();
166 code UnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY;
167
168 fn locked_err();
170 code Locked => StatusCode::LOCKED;
171
172 fn failed_dependency_err();
174 code FailedDependency => StatusCode::FAILED_DEPENDENCY;
175
176 fn upgrade_required_err();
181 code UpgradeRequired => StatusCode::UPGRADE_REQUIRED;
182
183 fn precondition_required_err();
185 code PreconditionRequired => StatusCode::PRECONDITION_REQUIRED;
186
187 fn too_many_requests_err();
189 code TooManyRequests => StatusCode::TOO_MANY_REQUESTS;
190
191 fn request_header_fields_too_large_err();
193 code RequestHeaderFieldsTooLarge => StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE;
194
195 fn unavailable_for_legal_reasons_err();
197 code UnavailableForLegalReasons => StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS;
198
199 fn internal_err();
201 code InternalServerError => StatusCode::INTERNAL_SERVER_ERROR;
202
203 fn not_implemented_err();
205 code NotImplemented => StatusCode::NOT_IMPLEMENTED;
206
207 fn bad_gateway_err();
209 code BadGateway => StatusCode::BAD_GATEWAY;
210
211 fn service_unavailable_err();
213 code ServiceUnavailable => StatusCode::SERVICE_UNAVAILABLE;
214
215 fn gateway_timeout_err();
217 code GatewayTimeout => StatusCode::GATEWAY_TIMEOUT;
218
219 fn http_version_not_supported_err();
221 code HttpVersionNotSupported => StatusCode::HTTP_VERSION_NOT_SUPPORTED;
222
223 fn variant_also_negotiates_err();
225 code VariantAlsoNegotiates => StatusCode::VARIANT_ALSO_NEGOTIATES;
226
227 fn insufficient_storage_err();
229 code InsufficientStorage => StatusCode::INSUFFICIENT_STORAGE;
230
231 fn loop_detected_err();
233 code LoopDetected => StatusCode::LOOP_DETECTED;
234
235 fn not_extended_err();
237 code NotExtended => StatusCode::NOT_EXTENDED;
238
239 fn network_authentication_required_err();
241 code NetworkAuthenticationRequired => StatusCode::NETWORK_AUTHENTICATION_REQUIRED;
242);