1use std::{error, fmt};
2
3use http::StatusCode;
4
5use crate::HttpError;
6
7#[derive(Debug)]
8pub struct StatusError<E> {
9 status: StatusCode,
10 inner: E,
11}
12
13macro_rules! trait_fn {
14 ($name:ident) => {
15 fn $name(self) -> Result<T, StatusError<E>>;
16 };
17}
18macro_rules! impl_fn {
19 ($name:ident, $status:ident) => {
20 fn $name(self) -> Result<T, StatusError<E>> {
21 self.map_err(|inner| StatusError {
22 status: StatusCode::$status,
23 inner,
24 })
25 }
26 };
27}
28
29pub trait ResultExt<T, E> {
30 trait_fn!(bad_request);
32 trait_fn!(unauthorized);
34 trait_fn!(payment_required);
36 trait_fn!(forbidden);
38 trait_fn!(not_found);
40 trait_fn!(method_not_allowed);
42 trait_fn!(not_acceptable);
44 trait_fn!(proxy_authentication_required);
46 trait_fn!(request_timeout);
48 trait_fn!(conflict);
50 trait_fn!(gone);
52 trait_fn!(length_required);
54 trait_fn!(precondition_failed);
56 trait_fn!(payload_too_large);
58 trait_fn!(uri_too_long);
60 trait_fn!(unsupported_media_type);
62 trait_fn!(range_not_satisfiable);
64 trait_fn!(expectation_failed);
66 trait_fn!(im_a_teapot);
68
69 trait_fn!(misdirected_request);
71 trait_fn!(unprocessable_entity);
73 trait_fn!(locked);
75 trait_fn!(failed_dependency);
77
78 trait_fn!(upgrade_required);
80
81 trait_fn!(precondition_required);
83 trait_fn!(too_many_requests);
85
86 trait_fn!(request_header_fields_too_large);
88
89 trait_fn!(unavailable_for_legal_reasons);
91
92 trait_fn!(internal_server_error);
94 trait_fn!(not_implemented);
96 trait_fn!(bad_gateway);
98 trait_fn!(service_unavailable);
100 trait_fn!(gateway_timeout);
102 trait_fn!(http_version_not_supported);
104 trait_fn!(variant_also_negotiates);
106 trait_fn!(insufficient_storage);
108 trait_fn!(loop_detected);
110
111 trait_fn!(not_extended);
113 trait_fn!(network_authentication_required);
115}
116impl<T, E> ResultExt<T, E> for Result<T, E> {
117 impl_fn!(bad_request, BAD_REQUEST);
118 impl_fn!(unauthorized, UNAUTHORIZED);
119 impl_fn!(payment_required, PAYMENT_REQUIRED);
120 impl_fn!(forbidden, FORBIDDEN);
121 impl_fn!(not_found, NOT_FOUND);
122 impl_fn!(method_not_allowed, METHOD_NOT_ALLOWED);
123 impl_fn!(not_acceptable, NOT_ACCEPTABLE);
124 impl_fn!(proxy_authentication_required, PROXY_AUTHENTICATION_REQUIRED);
125 impl_fn!(request_timeout, REQUEST_TIMEOUT);
126 impl_fn!(conflict, CONFLICT);
127 impl_fn!(gone, GONE);
128 impl_fn!(length_required, LENGTH_REQUIRED);
129 impl_fn!(precondition_failed, PRECONDITION_FAILED);
130 impl_fn!(payload_too_large, PAYLOAD_TOO_LARGE);
131 impl_fn!(uri_too_long, URI_TOO_LONG);
132 impl_fn!(unsupported_media_type, UNSUPPORTED_MEDIA_TYPE);
133 impl_fn!(range_not_satisfiable, RANGE_NOT_SATISFIABLE);
134 impl_fn!(expectation_failed, EXPECTATION_FAILED);
135 impl_fn!(im_a_teapot, IM_A_TEAPOT);
136
137 impl_fn!(misdirected_request, MISDIRECTED_REQUEST);
138 impl_fn!(unprocessable_entity, UNPROCESSABLE_ENTITY);
139 impl_fn!(locked, LOCKED);
140 impl_fn!(failed_dependency, FAILED_DEPENDENCY);
141
142 impl_fn!(upgrade_required, UPGRADE_REQUIRED);
143
144 impl_fn!(precondition_required, PRECONDITION_REQUIRED);
145 impl_fn!(too_many_requests, TOO_MANY_REQUESTS);
146
147 impl_fn!(
148 request_header_fields_too_large,
149 REQUEST_HEADER_FIELDS_TOO_LARGE
150 );
151
152 impl_fn!(unavailable_for_legal_reasons, UNAVAILABLE_FOR_LEGAL_REASONS);
153
154 impl_fn!(internal_server_error, INTERNAL_SERVER_ERROR);
155 impl_fn!(not_implemented, NOT_IMPLEMENTED);
156 impl_fn!(bad_gateway, BAD_GATEWAY);
157 impl_fn!(service_unavailable, SERVICE_UNAVAILABLE);
158 impl_fn!(gateway_timeout, GATEWAY_TIMEOUT);
159 impl_fn!(http_version_not_supported, HTTP_VERSION_NOT_SUPPORTED);
160 impl_fn!(variant_also_negotiates, VARIANT_ALSO_NEGOTIATES);
161 impl_fn!(insufficient_storage, INSUFFICIENT_STORAGE);
162 impl_fn!(loop_detected, LOOP_DETECTED);
163
164 impl_fn!(not_extended, NOT_EXTENDED);
165 impl_fn!(
166 network_authentication_required,
167 NETWORK_AUTHENTICATION_REQUIRED
168 );
169}
170
171impl<E> error::Error for StatusError<E>
172where
173 E: error::Error + 'static,
174{
175 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
176 Some(&self.inner)
177 }
178}
179
180impl<E> fmt::Display for StatusError<E>
181where
182 E: fmt::Display,
183{
184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185 self.inner.fmt(f)
186 }
187}
188
189impl<E> HttpError for StatusError<E>
190where
191 E: error::Error + 'static,
192{
193 fn status_code(&self) -> StatusCode {
194 self.status
195 }
196}