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