use super::*;
macro_rules! define_codes {
(
$(
$(#[$meta:meta])*
$(fn $fn_name:ident ();)?
code $name:ident => $status:expr;
)*
) => {
$(
$(#[$meta])*
#[derive(Debug, Clone, Copy, Default)]
pub struct $name<T = ()>(pub T);
impl<T> WrapsResponse for $name<T> {
const STATUS_CODE: StatusCode = $status;
type Inner = T;
type Pure = $name;
fn into_inner(self) -> Self::Inner {
self.0
}
}
impl<T, S> From<$name<T>> for ApiResponse<S>
where
S: Contains<$name<T>>,
$name<T>: WrapsResponse<Inner: IntoResponse>,
{
fn from(err: $name<T>) -> Self {
ApiResponse::new(err)
}
}
impl<T> From<T> for $name<T> {
fn from(inner: T) -> Self {
Self(inner)
}
}
impl<T> std::ops::Deref for $name<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> std::ops::DerefMut for $name<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
)*
pub trait ApiResultExt<T, E> {
$(
$(
fn $fn_name<R>(self) -> Result<T, $name<R>>
where
E: Into<R>;
)?
)*
}
impl<T, E> ApiResultExt<T, E> for Result<T, E> {
$(
$(
fn $fn_name<R>(self) -> Result<T, $name<R>>
where
E: Into<R>,
{
match self {
Ok(val) => Ok(val),
Err(e) => Err($name(e.into())),
}
}
)?
)*
}
};
}
define_codes!(
fn bad_request();
code BadRequest => StatusCode::BAD_REQUEST;
fn unauthorized_err();
code Unauthorized => StatusCode::UNAUTHORIZED;
fn payment_required_err();
code PaymentRequired => StatusCode::PAYMENT_REQUIRED;
fn forbidden_err();
code Forbidden => StatusCode::FORBIDDEN;
fn not_found_err();
code NotFound => StatusCode::NOT_FOUND;
fn method_not_allowed_err();
code MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED;
fn not_acceptable_err();
code NotAcceptable => StatusCode::NOT_ACCEPTABLE;
fn proxy_authentication_required_err();
code ProxyAuthenticationRequired => StatusCode::PROXY_AUTHENTICATION_REQUIRED;
fn request_timeout_err();
code RequestTimeout => StatusCode::REQUEST_TIMEOUT;
fn conflict_err();
code Conflict => StatusCode::CONFLICT;
fn gone_err();
code Gone => StatusCode::GONE;
fn length_required_err();
code LengthRequired => StatusCode::LENGTH_REQUIRED;
fn precondition_failed_err();
code PreconditionFailed => StatusCode::PRECONDITION_FAILED;
fn uri_too_long_err();
code UriTooLong => StatusCode::URI_TOO_LONG;
fn unsupported_media_type_err();
code UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE;
fn range_not_satisfiable_err();
code RangeNotSatisfiable => StatusCode::RANGE_NOT_SATISFIABLE;
fn expectation_failed_err();
code ExpectationFailed => StatusCode::EXPECTATION_FAILED;
fn im_a_teapot_err();
code ImATeapot => StatusCode::IM_A_TEAPOT;
fn misdirected_request_err();
code MisdirectedRequest => StatusCode::MISDIRECTED_REQUEST;
fn unprocessable_entity_err();
code UnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY;
fn locked_err();
code Locked => StatusCode::LOCKED;
fn failed_dependency_err();
code FailedDependency => StatusCode::FAILED_DEPENDENCY;
fn upgrade_required_err();
code UpgradeRequired => StatusCode::UPGRADE_REQUIRED;
fn precondition_required_err();
code PreconditionRequired => StatusCode::PRECONDITION_REQUIRED;
fn too_many_requests_err();
code TooManyRequests => StatusCode::TOO_MANY_REQUESTS;
fn request_header_fields_too_large_err();
code RequestHeaderFieldsTooLarge => StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE;
fn unavailable_for_legal_reasons_err();
code UnavailableForLegalReasons => StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS;
fn internal_err();
code InternalServerError => StatusCode::INTERNAL_SERVER_ERROR;
fn not_implemented_err();
code NotImplemented => StatusCode::NOT_IMPLEMENTED;
fn bad_gateway_err();
code BadGateway => StatusCode::BAD_GATEWAY;
fn service_unavailable_err();
code ServiceUnavailable => StatusCode::SERVICE_UNAVAILABLE;
fn gateway_timeout_err();
code GatewayTimeout => StatusCode::GATEWAY_TIMEOUT;
fn http_version_not_supported_err();
code HttpVersionNotSupported => StatusCode::HTTP_VERSION_NOT_SUPPORTED;
fn variant_also_negotiates_err();
code VariantAlsoNegotiates => StatusCode::VARIANT_ALSO_NEGOTIATES;
fn insufficient_storage_err();
code InsufficientStorage => StatusCode::INSUFFICIENT_STORAGE;
fn loop_detected_err();
code LoopDetected => StatusCode::LOOP_DETECTED;
fn not_extended_err();
code NotExtended => StatusCode::NOT_EXTENDED;
fn network_authentication_required_err();
code NetworkAuthenticationRequired => StatusCode::NETWORK_AUTHENTICATION_REQUIRED;
);