2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
2018-11-30 03:56:15 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use pin_project::pin_project;
|
2018-09-11 18:30:22 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use super::{Service, ServiceFactory};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2018-09-12 22:34:53 +02:00
|
|
|
/// Service for the `map_err` combinator, changing the type of a service's
|
|
|
|
/// error.
|
|
|
|
///
|
|
|
|
/// This is created by the `ServiceExt::map_err` method.
|
2019-11-14 13:38:24 +01:00
|
|
|
pub(crate) struct MapErr<A, F, E> {
|
2018-09-12 22:34:53 +02:00
|
|
|
service: A,
|
2018-08-25 18:02:14 +02:00
|
|
|
f: F,
|
2018-11-30 03:56:15 +01:00
|
|
|
_t: PhantomData<E>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, E> MapErr<A, F, E> {
|
2018-08-25 18:02:14 +02:00
|
|
|
/// Create new `MapErr` combinator
|
2019-03-09 15:36:23 +01:00
|
|
|
pub fn new(service: A, f: F) -> Self
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2018-11-30 03:56:15 +01:00
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
service,
|
|
|
|
f,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:54:15 +02:00
|
|
|
impl<A, F, E> Clone for MapErr<A, F, E>
|
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Clone,
|
|
|
|
F: Clone,
|
2018-09-05 22:54:15 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
MapErr {
|
2018-09-12 22:34:53 +02:00
|
|
|
service: self.service.clone(),
|
2018-09-05 22:54:15 +02:00
|
|
|
f: self.f.clone(),
|
2018-11-30 03:56:15 +01:00
|
|
|
_t: PhantomData,
|
2018-09-05 22:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<A, F, E> Service for MapErr<A, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2018-11-30 03:56:15 +01:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = A::Request;
|
2018-08-25 18:02:14 +02:00
|
|
|
type Response = A::Response;
|
|
|
|
type Error = E;
|
2019-03-09 15:36:23 +01:00
|
|
|
type Future = MapErrFuture<A, F, E>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.service.poll_ready(ctx).map_err(&self.f)
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
|
|
|
MapErrFuture::new(self.service.call(req), self.f.clone())
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin_project]
|
|
|
|
pub(crate) struct MapErrFuture<A, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2018-08-25 18:02:14 +02:00
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
|
|
|
f: F,
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin]
|
2018-08-25 18:02:14 +02:00
|
|
|
fut: A::Future,
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<A, F, E> MapErrFuture<A, F, E>
|
|
|
|
where
|
|
|
|
A: Service,
|
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
|
|
MapErrFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, F, E> Future for MapErrFuture<A, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2018-08-25 18:02:14 +02:00
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<A::Response, E>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.project();
|
|
|
|
this.fut.poll(cx).map_err(this.f)
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Factory for the `map_err` combinator, changing the type of a new
|
2018-09-18 03:10:23 +02:00
|
|
|
/// service's error.
|
|
|
|
///
|
|
|
|
/// This is created by the `NewServiceExt::map_err` method.
|
2019-11-14 13:38:24 +01:00
|
|
|
pub(crate) struct MapErrNewService<A, F, E>
|
2019-03-12 21:45:05 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-03-12 21:45:05 +01:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
|
|
|
{
|
2018-08-25 18:02:14 +02:00
|
|
|
a: A,
|
|
|
|
f: F,
|
2019-05-12 15:03:50 +02:00
|
|
|
e: PhantomData<E>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<A, F, E> MapErrNewService<A, F, E>
|
2019-03-12 21:45:05 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-03-12 21:45:05 +01:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
|
|
|
{
|
2018-08-25 18:02:14 +02:00
|
|
|
/// Create new `MapErr` new service instance
|
2019-11-14 13:38:24 +01:00
|
|
|
pub(crate) fn new(a: A, f: F) -> Self {
|
2018-08-25 18:02:14 +02:00
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
f,
|
2018-11-30 03:56:15 +01:00
|
|
|
e: PhantomData,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<A, F, E> Clone for MapErrNewService<A, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory + Clone,
|
2019-03-12 21:45:05 +01:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
f: self.f.clone(),
|
2018-11-30 03:56:15 +01:00
|
|
|
e: PhantomData,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<A, F, E> ServiceFactory for MapErrNewService<A, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2018-08-25 18:02:14 +02:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = A::Request;
|
2018-08-25 18:02:14 +02:00
|
|
|
type Response = A::Response;
|
|
|
|
type Error = E;
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = A::Config;
|
|
|
|
type Service = MapErr<A::Service, F, E>;
|
2018-08-25 18:02:14 +02:00
|
|
|
type InitError = A::InitError;
|
2019-05-12 15:03:50 +02:00
|
|
|
type Future = MapErrNewServiceFuture<A, F, E>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
2019-03-05 06:35:47 +01:00
|
|
|
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin_project]
|
|
|
|
pub(crate) struct MapErrNewServiceFuture<A, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2018-08-25 18:02:14 +02:00
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin]
|
2019-03-05 06:35:47 +01:00
|
|
|
fut: A::Future,
|
2018-08-25 18:02:14 +02:00
|
|
|
f: F,
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<A, F, E> MapErrNewServiceFuture<A, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2018-08-25 18:02:14 +02:00
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
2019-03-05 06:35:47 +01:00
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
2018-08-25 18:02:14 +02:00
|
|
|
MapErrNewServiceFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2018-08-25 18:02:14 +02:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<MapErr<A::Service, F, E>, A::InitError>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.project();
|
|
|
|
if let Poll::Ready(svc) = this.fut.poll(cx)? {
|
|
|
|
Poll::Ready(Ok(MapErr::new(svc, this.f.clone())))
|
2018-08-25 18:02:14 +02:00
|
|
|
} else {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-11-14 13:38:24 +01:00
|
|
|
use futures::future::{err, lazy, ok, Ready};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
use super::*;
|
2019-11-14 13:38:24 +01:00
|
|
|
use crate::{into_factory, into_service, Service};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
struct Srv;
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl Service for Srv {
|
|
|
|
type Request = ();
|
2018-09-12 22:34:53 +02:00
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = Ready<Result<(), ()>>;
|
2018-09-12 22:34:53 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Err(()))
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, _: ()) -> Self::Future {
|
|
|
|
err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_poll_ready() {
|
|
|
|
let mut srv = into_service(Srv).map_err(|_| "error");
|
|
|
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
|
|
|
assert_eq!(res, Poll::Ready(Err("error")));
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_call() {
|
|
|
|
let mut srv = into_service(Srv).map_err(|_| "error");
|
|
|
|
let res = srv.call(()).await;
|
2018-09-12 22:34:53 +02:00
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.err().unwrap(), "error");
|
|
|
|
}
|
2018-09-18 04:21:24 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_new_service() {
|
|
|
|
let new_srv = into_factory(|| ok::<_, ()>(Srv)).map_err(|_| "error");
|
|
|
|
let mut srv = new_srv.new_service(&()).await.unwrap();
|
|
|
|
let res = srv.call(()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.err().unwrap(), "error");
|
2018-09-18 04:21:24 +02:00
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|