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