2020-12-27 15:15:42 +01:00
|
|
|
use core::{
|
|
|
|
future::Future,
|
|
|
|
marker::PhantomData,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
|
|
|
use pin_project_lite::pin_project;
|
2018-08-25 18:02:14 +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.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct MapErr<S, Req, F, E> {
|
|
|
|
service: S,
|
2018-08-25 18:02:14 +02:00
|
|
|
f: F,
|
2020-12-27 05:28:00 +01:00
|
|
|
_t: PhantomData<(E, Req)>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<S, Req, F, E> MapErr<S, Req, F, E> {
|
2018-08-25 18:02:14 +02:00
|
|
|
/// Create new `MapErr` combinator
|
2020-12-27 05:28:00 +01:00
|
|
|
pub(crate) fn new(service: S, f: F) -> Self
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
S: Service<Req>,
|
|
|
|
F: Fn(S::Error) -> E,
|
2018-11-30 03:56:15 +01:00
|
|
|
{
|
|
|
|
Self {
|
|
|
|
service,
|
|
|
|
f,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<S, Req, F, E> Clone for MapErr<S, Req, F, E>
|
2018-09-05 22:54:15 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
S: Clone,
|
2018-11-30 03:56:15 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, F, E> Service<Req> for MapErr<A, Req, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
type Response = A::Response;
|
|
|
|
type Error = E;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Future = MapErrFuture<A, Req, F, E>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-14 13:38:24 +01:00
|
|
|
self.service.poll_ready(ctx).map_err(&self.f)
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
fn call(&self, req: Req) -> Self::Future {
|
2019-03-09 15:36:23 +01:00
|
|
|
MapErrFuture::new(self.service.call(req), self.f.clone())
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
pin_project! {
|
|
|
|
pub struct MapErrFuture<A, Req, F, E>
|
|
|
|
where
|
|
|
|
A: Service<Req>,
|
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
|
|
|
f: F,
|
|
|
|
#[pin]
|
|
|
|
fut: A::Future,
|
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, F, E> MapErrFuture<A, Req, F, E>
|
2019-03-09 15:36:23 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: Fn(A::Error) -> E,
|
2019-03-09 15:36:23 +01:00
|
|
|
{
|
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
|
|
MapErrFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, F, E> Future for MapErrFuture<A, Req, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: Fn(A::Error) -> E,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
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> {
|
2019-11-19 09:51:40 +01:00
|
|
|
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.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct MapErrServiceFactory<A, Req, F, E>
|
2019-03-12 21:45:05 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
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,
|
2020-12-27 05:28:00 +01:00
|
|
|
e: PhantomData<(E, Req)>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, F, E> MapErrServiceFactory<A, Req, F, E>
|
2019-03-12 21:45:05 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, F, E> Clone for MapErrServiceFactory<A, Req, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req> + 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, F, E> ServiceFactory<Req> for MapErrServiceFactory<A, Req, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
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;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Service = MapErr<A::Service, Req, F, E>;
|
2018-08-25 18:02:14 +02:00
|
|
|
type InitError = A::InitError;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Future = MapErrServiceFuture<A, Req, F, E>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
2019-11-18 09:30:04 +01:00
|
|
|
MapErrServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
pin_project! {
|
|
|
|
pub struct MapErrServiceFuture<A, Req, F, E>
|
|
|
|
where
|
|
|
|
A: ServiceFactory<Req>,
|
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
|
|
|
#[pin]
|
|
|
|
fut: A::Future,
|
|
|
|
f: F,
|
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, F, E> MapErrServiceFuture<A, Req, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
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 {
|
2021-03-27 01:20:17 +01:00
|
|
|
MapErrServiceFuture { fut, f }
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, F, E> Future for MapErrServiceFuture<A, Req, F, E>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Output = Result<MapErr<A::Service, Req, 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> {
|
2019-11-19 09:51:40 +01:00
|
|
|
let this = self.project();
|
|
|
|
if let Poll::Ready(svc) = this.fut.poll(cx)? {
|
2019-11-14 13:38:24 +01:00
|
|
|
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 {
|
2020-12-27 19:24:57 +01:00
|
|
|
use futures_util::future::lazy;
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
use super::*;
|
2020-12-27 19:24:57 +01:00
|
|
|
use crate::{
|
|
|
|
err, ok, IntoServiceFactory, Ready, Service, ServiceExt, ServiceFactory,
|
|
|
|
ServiceFactoryExt,
|
|
|
|
};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
struct Srv;
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl Service<()> for Srv {
|
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
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Err(()))
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
fn call(&self, _: ()) -> Self::Future {
|
2018-09-12 22:34:53 +02:00
|
|
|
err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_poll_ready() {
|
2021-01-23 04:06:22 +01:00
|
|
|
let srv = Srv.map_err(|_| "error");
|
2019-11-14 13:38:24 +01:00
|
|
|
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-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_call() {
|
2021-01-23 04:06:22 +01:00
|
|
|
let srv = Srv.map_err(|_| "error");
|
2019-11-14 13:38:24 +01:00
|
|
|
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-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_new_service() {
|
2019-11-18 09:30:04 +01:00
|
|
|
let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map_err(|_| "error");
|
2021-01-23 04:06:22 +01:00
|
|
|
let srv = new_srv.new_service(&()).await.unwrap();
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
|
|
}
|