use crate::{ErrorSet, code::*};
use type_sets::SupersetOf;
pub trait StatusResultExt {
type Ok;
type Err;
fn into_bad_request(self) -> Result<Self::Ok, BadRequest<Self::Err>>;
fn into_unauthorized(self) -> Result<Self::Ok, Unauthorized<Self::Err>>;
fn into_forbidden(self) -> Result<Self::Ok, Forbidden<Self::Err>>;
fn into_not_found(self) -> Result<Self::Ok, NotFound<Self::Err>>;
fn into_conflict(self) -> Result<Self::Ok, Conflict<Self::Err>>;
fn into_unprocessable_entity(self) -> Result<Self::Ok, UnprocessableEntity<Self::Err>>;
fn into_too_many_requests(self) -> Result<Self::Ok, TooManyRequests<Self::Err>>;
fn into_internal(self) -> Result<Self::Ok, InternalServerError<Self::Err>>;
fn into_bad_gateway(self) -> Result<Self::Ok, BadGateway<Self::Err>>;
fn into_service_unavailable(self) -> Result<Self::Ok, ServiceUnavailable<Self::Err>>;
fn into_gateway_timeout(self) -> Result<Self::Ok, GatewayTimeout<Self::Err>>;
}
impl<T, E> StatusResultExt for Result<T, E> {
type Ok = T;
type Err = E;
fn into_bad_request(self) -> Result<T, BadRequest<E>> {
self.map_err(BadRequest)
}
fn into_unauthorized(self) -> Result<T, Unauthorized<E>> {
self.map_err(Unauthorized)
}
fn into_forbidden(self) -> Result<T, Forbidden<E>> {
self.map_err(Forbidden)
}
fn into_not_found(self) -> Result<T, NotFound<E>> {
self.map_err(NotFound)
}
fn into_conflict(self) -> Result<T, Conflict<E>> {
self.map_err(Conflict)
}
fn into_unprocessable_entity(self) -> Result<T, UnprocessableEntity<E>> {
self.map_err(UnprocessableEntity)
}
fn into_too_many_requests(self) -> Result<T, TooManyRequests<E>> {
self.map_err(TooManyRequests)
}
fn into_internal(self) -> Result<T, InternalServerError<E>> {
self.map_err(InternalServerError)
}
fn into_bad_gateway(self) -> Result<T, BadGateway<E>> {
self.map_err(BadGateway)
}
fn into_service_unavailable(self) -> Result<T, ServiceUnavailable<E>> {
self.map_err(ServiceUnavailable)
}
fn into_gateway_timeout(self) -> Result<T, GatewayTimeout<E>> {
self.map_err(GatewayTimeout)
}
}
pub trait ResultSetExt {
type Ok;
type Err;
type Value;
fn into_superset<E>(self) -> Result<Self::Ok, ErrorSet<Self::Value, E>>
where
E: SupersetOf<Self::Err>;
}
impl<T, E, V> ResultSetExt for Result<T, ErrorSet<V, E>> {
type Ok = T;
type Err = E;
type Value = V;
fn into_superset<E2>(self) -> Result<T, ErrorSet<V, E2>>
where
E2: SupersetOf<E>,
{
self.map_err(|e| e.into_superset())
}
}