api: Add map and transpose methods for FromHttpResponseError and ServerError

This commit is contained in:
Jonas Platte 2022-03-21 19:57:00 +01:00 committed by Jonas Platte
parent ffd7625a17
commit eb515046d7

View File

@ -134,6 +134,30 @@ pub enum FromHttpResponseError<E> {
Server(ServerError<E>), Server(ServerError<E>),
} }
impl<E> FromHttpResponseError<E> {
/// Map `FromHttpResponseError<E>` to `FromHttpResponseError<F>` by applying a function to a
/// contained `Server` value, leaving a `Deserialization` value untouched.
pub fn map<F>(
self,
f: impl FnOnce(ServerError<E>) -> ServerError<F>,
) -> FromHttpResponseError<F> {
match self {
Self::Deserialization(d) => FromHttpResponseError::Deserialization(d),
Self::Server(s) => FromHttpResponseError::Server(f(s)),
}
}
}
impl<E, F> FromHttpResponseError<Result<E, F>> {
/// Transpose `FromHttpResponseError<Result<E, F>>` to `Result<FromHttpResponseError<E>, F>`.
pub fn transpose(self) -> Result<FromHttpResponseError<E>, F> {
match self {
Self::Deserialization(d) => Ok(FromHttpResponseError::Deserialization(d)),
Self::Server(s) => s.transpose().map(FromHttpResponseError::Server),
}
}
}
impl<E: fmt::Display> fmt::Display for FromHttpResponseError<E> { impl<E: fmt::Display> fmt::Display for FromHttpResponseError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
@ -172,6 +196,28 @@ pub enum ServerError<E> {
Unknown(DeserializationError), Unknown(DeserializationError),
} }
impl<E> ServerError<E> {
/// Map `ServerError<E>` to `ServerError<F>` by applying a function to a contained `Known`
/// value, leaving an `Unknown` value untouched.
pub fn map<F>(self, f: impl FnOnce(E) -> F) -> ServerError<F> {
match self {
Self::Known(k) => ServerError::Known(f(k)),
Self::Unknown(u) => ServerError::Unknown(u),
}
}
}
impl<E, F> ServerError<Result<E, F>> {
/// Transpose `ServerError<Result<E, F>>` to `Result<ServerError<E>, F>`.
pub fn transpose(self) -> Result<ServerError<E>, F> {
match self {
Self::Known(Ok(k)) => Ok(ServerError::Known(k)),
Self::Known(Err(e)) => Err(e),
Self::Unknown(u) => Ok(ServerError::Unknown(u)),
}
}
}
impl<E: fmt::Display> fmt::Display for ServerError<E> { impl<E: fmt::Display> fmt::Display for ServerError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {