use futures_util::{future::Map, FutureExt};
use std::fmt;
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;
#[derive(Clone)]
pub struct MapResult<S, F> {
inner: S,
f: F,
}
impl<S, F> fmt::Debug for MapResult<S, F>
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::<F>()))
.finish()
}
}
#[derive(Debug, Clone)]
pub struct MapResultLayer<F> {
f: F,
}
opaque_future! {
pub type MapResultFuture<F, N> = Map<F, N>;
}
impl<S, F> MapResult<S, F> {
pub const fn new(inner: S, f: F) -> Self {
MapResult { f, inner }
}
pub fn layer(f: F) -> MapResultLayer<F> {
MapResultLayer { f }
}
}
impl<S, F, Request, Response, Error> Service<Request> for MapResult<S, F>
where
S: Service<Request>,
Error: From<S::Error>,
F: FnOnce(Result<S::Response, S::Error>) -> Result<Response, Error> + Clone,
{
type Response = Response;
type Error = Error;
type Future = MapResultFuture<S::Future, F>;
#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
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<F> MapResultLayer<F> {
pub const fn new(f: F) -> Self {
MapResultLayer { f }
}
}
impl<S, F> Layer<S> for MapResultLayer<F>
where
F: Clone,
{
type Service = MapResult<S, F>;
fn layer(&self, inner: S) -> Self::Service {
MapResult {
f: self.f.clone(),
inner,
}
}
}