2018-11-29 16:56:15 -10:00
|
|
|
use std::marker::PhantomData;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
2019-03-04 21:35:47 -08:00
|
|
|
use futures::{Async, Future, Poll};
|
2018-09-11 09:30:22 -07:00
|
|
|
|
|
|
|
use super::{NewService, Service};
|
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.
|
2018-11-29 16:56:15 -10: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-03-09 06:36:23 -08:00
|
|
|
pub 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,
|
2018-11-29 16:56:15 -10: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
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-09-12 13:34:53 -07:00
|
|
|
self.service.poll_ready().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-03-09 06:36:23 -08: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,
|
2018-08-25 09:02:14 -07:00
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
|
|
|
f: F,
|
|
|
|
fut: A::Future,
|
|
|
|
}
|
|
|
|
|
2019-03-09 06:36:23 -08: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 09:02:14 -07:00
|
|
|
where
|
2019-03-09 06:36:23 -08:00
|
|
|
A: Service,
|
2018-08-25 09:02:14 -07:00
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
|
|
|
type Item = A::Response;
|
|
|
|
type Error = E;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
self.fut.poll().map_err(&self.f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 18:10:23 -07:00
|
|
|
/// NewService for the `map_err` combinator, changing the type of a new
|
|
|
|
/// service's error.
|
|
|
|
///
|
|
|
|
/// This is created by the `NewServiceExt::map_err` method.
|
2019-05-12 06:03:50 -07:00
|
|
|
pub struct MapErrNewService<A, F, E>
|
2019-03-12 13:45:05 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
A: NewService,
|
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-05-12 06:03:50 -07:00
|
|
|
impl<A, F, E> MapErrNewService<A, F, E>
|
2019-03-12 13:45:05 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
A: NewService,
|
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-03-12 13:45:05 -07:00
|
|
|
pub 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-05-12 06:03:50 -07:00
|
|
|
impl<A, F, E> Clone for MapErrNewService<A, F, E>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
A: NewService + 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-05-12 06:03:50 -07:00
|
|
|
impl<A, F, E> NewService for MapErrNewService<A, F, E>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
A: NewService,
|
2018-08-25 09:02:14 -07:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
|
|
|
{
|
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-05-12 06:03:50 -07:00
|
|
|
type Future = MapErrNewServiceFuture<A, F, E>;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
2019-03-04 21:35:47 -08:00
|
|
|
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
pub struct MapErrNewServiceFuture<A, F, E>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
A: NewService,
|
2018-08-25 09:02:14 -07:00
|
|
|
F: Fn(A::Error) -> E,
|
|
|
|
{
|
2019-03-04 21:35:47 -08:00
|
|
|
fut: A::Future,
|
2018-08-25 09:02:14 -07:00
|
|
|
f: F,
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
impl<A, F, E> MapErrNewServiceFuture<A, F, E>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
A: NewService,
|
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 {
|
2018-08-25 09:02:14 -07:00
|
|
|
MapErrNewServiceFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
A: NewService,
|
2018-08-25 09:02:14 -07:00
|
|
|
F: Fn(A::Error) -> E + Clone,
|
|
|
|
{
|
|
|
|
type Item = MapErr<A::Service, F, E>;
|
|
|
|
type Error = A::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Async::Ready(service) = self.fut.poll()? {
|
|
|
|
Ok(Async::Ready(MapErr::new(service, self.f.clone())))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 13:34:53 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use futures::future::{err, FutureResult};
|
|
|
|
|
|
|
|
use super::*;
|
2018-12-12 18:56:39 -08:00
|
|
|
use crate::{IntoNewService, NewService, Service, ServiceExt};
|
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 = ();
|
|
|
|
type Future = FutureResult<(), ()>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, _: ()) -> Self::Future {
|
|
|
|
err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_poll_ready() {
|
|
|
|
let mut srv = Srv.map_err(|_| "error");
|
|
|
|
let res = srv.poll_ready();
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.err().unwrap(), "error");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call() {
|
|
|
|
let mut srv = Srv.map_err(|_| "error");
|
|
|
|
let res = srv.call(()).poll();
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.err().unwrap(), "error");
|
|
|
|
}
|
2018-09-17 19:21:24 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_service() {
|
2019-02-22 14:19:43 -08:00
|
|
|
let blank = || Ok::<_, ()>(Srv);
|
2018-09-17 19:21:24 -07:00
|
|
|
let new_srv = blank.into_new_service().map_err(|_| "error");
|
2019-02-22 12:44:37 -08:00
|
|
|
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
2018-09-17 19:21:24 -07:00
|
|
|
let res = srv.call(()).poll();
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.err().unwrap(), "error");
|
|
|
|
} else {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 13:34:53 -07:00
|
|
|
}
|