use futures_util::{future, TryFutureExt}; use std::fmt; use std::task::{Context, Poll}; use tower_layer::Layer; use tower_service::Service; /// Service returned by the [`map_err`] combinator. /// /// [`map_err`]: crate::util::ServiceExt::map_err #[derive(Clone)] pub struct MapErr { inner: S, f: F, } impl fmt::Debug for MapErr where S: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("MapErr") .field("inner", &self.inner) .field("f", &format_args!("{}", std::any::type_name::())) .finish() } } /// A [`Layer`] that produces [`MapErr`] services. /// /// [`Layer`]: tower_layer::Layer #[derive(Clone, Debug)] pub struct MapErrLayer { f: F, } opaque_future! { /// Response future from [`MapErr`] services. /// /// [`MapErr`]: crate::util::MapErr pub type MapErrFuture = future::MapErr; } impl MapErr { /// Creates a new [`MapErr`] service. pub fn new(inner: S, f: F) -> Self { MapErr { f, inner } } /// Returns a new [`Layer`] that produces [`MapErr`] services. /// /// This is a convenience function that simply calls [`MapErrLayer::new`]. /// /// [`Layer`]: tower_layer::Layer pub fn layer(f: F) -> MapErrLayer { MapErrLayer { f } } } impl Service for MapErr where S: Service, F: FnOnce(S::Error) -> Error + Clone, { type Response = S::Response; type Error = Error; type Future = MapErrFuture; #[inline] fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.inner.poll_ready(cx).map_err(self.f.clone()) } #[inline] fn call(&mut self, request: Request) -> Self::Future { MapErrFuture::new(self.inner.call(request).map_err(self.f.clone())) } } impl MapErrLayer { /// Creates a new [`MapErrLayer`]. pub fn new(f: F) -> Self { MapErrLayer { f } } } impl Layer for MapErrLayer where F: Clone, { type Service = MapErr; fn layer(&self, inner: S) -> Self::Service { MapErr { f: self.f.clone(), inner, } } }