1use futures_util::{future::Map, FutureExt};
2use std::fmt;
3use std::task::{Context, Poll};
4use tower_layer::Layer;
5use tower_service::Service;
6
7/// Service returned by the [`map_result`] combinator.
8///
9/// [`map_result`]: crate::util::ServiceExt::map_result
10#[derive(Clone)]
11pub struct MapResult<S, F> {
12 inner: S,
13 f: F,
14}
15
16impl<S, F> fmt::Debug for MapResult<S, F>
17where
18 S: fmt::Debug,
19{
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 f&mut DebugStruct<'_, '_>.debug_struct("MapResult")
22 .field("inner", &self.inner)
23 .field(name:"f", &format_args!("{}", std::any::type_name::<F>()))
24 .finish()
25 }
26}
27
28/// A [`Layer`] that produces a [`MapResult`] service.
29///
30/// [`Layer`]: tower_layer::Layer
31#[derive(Debug, Clone)]
32pub struct MapResultLayer<F> {
33 f: F,
34}
35
36opaque_future! {
37 /// Response future from [`MapResult`] services.
38 ///
39 /// [`MapResult`]: crate::util::MapResult
40 pub type MapResultFuture<F, N> = Map<F, N>;
41}
42
43impl<S, F> MapResult<S, F> {
44 /// Creates a new [`MapResult`] service.
45 pub const fn new(inner: S, f: F) -> Self {
46 MapResult { f, inner }
47 }
48
49 /// Returns a new [`Layer`] that produces [`MapResult`] services.
50 ///
51 /// This is a convenience function that simply calls [`MapResultLayer::new`].
52 ///
53 /// [`Layer`]: tower_layer::Layer
54 pub fn layer(f: F) -> MapResultLayer<F> {
55 MapResultLayer { f }
56 }
57}
58
59impl<S, F, Request, Response, Error> Service<Request> for MapResult<S, F>
60where
61 S: Service<Request>,
62 Error: From<S::Error>,
63 F: FnOnce(Result<S::Response, S::Error>) -> Result<Response, Error> + Clone,
64{
65 type Response = Response;
66 type Error = Error;
67 type Future = MapResultFuture<S::Future, F>;
68
69 #[inline]
70 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
71 self.inner.poll_ready(cx).map_err(Into::into)
72 }
73
74 #[inline]
75 fn call(&mut self, request: Request) -> Self::Future {
76 MapResultFuture::new(self.inner.call(req:request).map(self.f.clone()))
77 }
78}
79
80impl<F> MapResultLayer<F> {
81 /// Creates a new [`MapResultLayer`] layer.
82 pub const fn new(f: F) -> Self {
83 MapResultLayer { f }
84 }
85}
86
87impl<S, F> Layer<S> for MapResultLayer<F>
88where
89 F: Clone,
90{
91 type Service = MapResult<S, F>;
92
93 fn layer(&self, inner: S) -> Self::Service {
94 MapResult {
95 f: self.f.clone(),
96 inner,
97 }
98 }
99}
100