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