2018-09-04 12:07:13 -07:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2018-09-17 18:10:23 -07:00
|
|
|
use futures::{Async, Future, Poll};
|
2018-09-11 09:30:22 -07:00
|
|
|
|
2018-09-17 18:10:23 -07:00
|
|
|
use super::{NewService, Service};
|
2018-09-04 12:07:13 -07:00
|
|
|
|
2018-09-12 13:34:53 -07:00
|
|
|
/// Service for the `from_err` combinator, changing the error type of a service.
|
|
|
|
///
|
|
|
|
/// This is created by the `ServiceExt::from_err` method.
|
2018-11-29 16:56:15 -10:00
|
|
|
pub struct FromErr<A, E> {
|
2018-09-04 12:07:13 -07:00
|
|
|
service: A,
|
|
|
|
f: PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
impl<A, E> FromErr<A, E> {
|
|
|
|
pub(crate) fn new<Request>(service: A) -> Self
|
|
|
|
where
|
|
|
|
A: Service<Request>,
|
|
|
|
E: From<A::Error>,
|
|
|
|
{
|
2018-09-04 12:07:13 -07:00
|
|
|
FromErr {
|
|
|
|
service,
|
|
|
|
f: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:27:52 -07:00
|
|
|
impl<A, E> Clone for FromErr<A, E>
|
|
|
|
where
|
2018-11-29 16:56:15 -10:00
|
|
|
A: Clone,
|
2018-09-05 19:27:52 -07:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
FromErr {
|
|
|
|
service: self.service.clone(),
|
|
|
|
f: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
impl<A, E, Request> Service<Request> for FromErr<A, E>
|
2018-09-04 12:07:13 -07:00
|
|
|
where
|
2018-11-29 16:56:15 -10:00
|
|
|
A: Service<Request>,
|
2018-09-04 12:07:13 -07:00
|
|
|
E: From<A::Error>,
|
|
|
|
{
|
|
|
|
type Response = A::Response;
|
|
|
|
type Error = E;
|
2018-11-29 16:56:15 -10:00
|
|
|
type Future = FromErrFuture<A, E, Request>;
|
2018-09-04 12:07:13 -07:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), E> {
|
2019-02-01 14:48:09 -08:00
|
|
|
self.service.poll_ready().map_err(E::from)
|
2018-09-04 12:07:13 -07:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
2018-09-04 12:07:13 -07:00
|
|
|
FromErrFuture {
|
|
|
|
fut: self.service.call(req),
|
|
|
|
f: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
pub struct FromErrFuture<A: Service<Request>, E, Request> {
|
2018-09-04 12:07:13 -07:00
|
|
|
fut: A::Future,
|
|
|
|
f: PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
impl<A, E, Request> Future for FromErrFuture<A, E, Request>
|
2018-09-04 12:07:13 -07:00
|
|
|
where
|
2018-11-29 16:56:15 -10:00
|
|
|
A: Service<Request>,
|
2018-09-04 12:07:13 -07:00
|
|
|
E: From<A::Error>,
|
|
|
|
{
|
|
|
|
type Item = A::Response;
|
|
|
|
type Error = E;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
self.fut.poll().map_err(E::from)
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 13:34:53 -07:00
|
|
|
|
2018-09-17 18:10:23 -07:00
|
|
|
/// NewService for the `from_err` combinator, changing the type of a new
|
|
|
|
/// service's error.
|
|
|
|
///
|
|
|
|
/// This is created by the `NewServiceExt::from_err` method.
|
|
|
|
pub struct FromErrNewService<A, E> {
|
|
|
|
a: A,
|
|
|
|
e: PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
impl<A, E> FromErrNewService<A, E> {
|
2018-09-17 18:10:23 -07:00
|
|
|
/// Create new `FromErr` new service instance
|
2018-11-29 16:56:15 -10:00
|
|
|
pub fn new<Request>(a: A) -> Self
|
|
|
|
where
|
|
|
|
A: NewService<Request>,
|
|
|
|
E: From<A::Error>,
|
|
|
|
{
|
2018-09-17 18:10:23 -07:00
|
|
|
Self { a, e: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, E> Clone for FromErrNewService<A, E>
|
|
|
|
where
|
2018-11-29 16:56:15 -10:00
|
|
|
A: Clone,
|
2018-09-17 18:10:23 -07:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
impl<A, E, Request> NewService<Request> for FromErrNewService<A, E>
|
2018-09-17 18:10:23 -07:00
|
|
|
where
|
2018-11-29 16:56:15 -10:00
|
|
|
A: NewService<Request>,
|
2018-09-17 18:49:55 -07:00
|
|
|
E: From<A::Error>,
|
2018-09-17 18:10:23 -07:00
|
|
|
{
|
|
|
|
type Response = A::Response;
|
|
|
|
type Error = E;
|
|
|
|
type Service = FromErr<A::Service, E>;
|
|
|
|
|
2018-09-17 18:49:55 -07:00
|
|
|
type InitError = A::InitError;
|
2018-11-29 16:56:15 -10:00
|
|
|
type Future = FromErrNewServiceFuture<A, E, Request>;
|
2018-09-17 18:10:23 -07:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
FromErrNewServiceFuture {
|
|
|
|
fut: self.a.new_service(),
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
pub struct FromErrNewServiceFuture<A, E, Request>
|
2018-09-17 18:10:23 -07:00
|
|
|
where
|
2018-11-29 16:56:15 -10:00
|
|
|
A: NewService<Request>,
|
2018-09-17 18:49:55 -07:00
|
|
|
E: From<A::Error>,
|
2018-09-17 18:10:23 -07:00
|
|
|
{
|
|
|
|
fut: A::Future,
|
|
|
|
e: PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:56:15 -10:00
|
|
|
impl<A, E, Request> Future for FromErrNewServiceFuture<A, E, Request>
|
2018-09-17 18:10:23 -07:00
|
|
|
where
|
2018-11-29 16:56:15 -10:00
|
|
|
A: NewService<Request>,
|
2018-09-17 18:49:55 -07:00
|
|
|
E: From<A::Error>,
|
2018-09-17 18:10:23 -07:00
|
|
|
{
|
|
|
|
type Item = FromErr<A::Service, E>;
|
2018-09-17 18:49:55 -07:00
|
|
|
type Error = A::InitError;
|
2018-09-17 18:10:23 -07:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Async::Ready(service) = self.fut.poll()? {
|
|
|
|
Ok(Async::Ready(FromErr::new(service)))
|
|
|
|
} 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;
|
2018-11-29 17:17:02 -10:00
|
|
|
impl Service<()> for Srv {
|
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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
struct Error;
|
|
|
|
|
|
|
|
impl From<()> for Error {
|
|
|
|
fn from(_: ()) -> Self {
|
|
|
|
Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_poll_ready() {
|
|
|
|
let mut srv = Srv.from_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.from_err::<Error>();
|
|
|
|
let res = srv.call(()).poll();
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.err().unwrap(), Error);
|
|
|
|
}
|
2018-09-17 18:10:23 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_service() {
|
|
|
|
let blank = || Ok::<_, ()>(Srv);
|
|
|
|
let new_srv = blank.into_new_service().from_err::<Error>();
|
|
|
|
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
|
|
|
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
|
|
|
}
|