2019-03-02 21:16:30 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-03-05 05:29:35 +01:00
|
|
|
use futures::IntoFuture;
|
2019-02-03 19:42:27 +01:00
|
|
|
|
2019-03-02 22:18:01 +01:00
|
|
|
use crate::transform_map_init_err::TransformMapInitErr;
|
2019-02-04 19:55:39 +01:00
|
|
|
use crate::Service;
|
2019-02-03 19:42:27 +01:00
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
/// `Transform` service factory.
|
2019-03-02 21:16:30 +01:00
|
|
|
///
|
2019-03-05 04:38:11 +01:00
|
|
|
/// Transform factory creates service that wraps other services.
|
2019-03-02 21:16:30 +01:00
|
|
|
/// `Config` is a service factory configuration type.
|
2019-03-05 04:38:11 +01:00
|
|
|
pub trait Transform<S> {
|
2019-02-03 19:42:27 +01:00
|
|
|
/// Requests handled by the service.
|
|
|
|
type Request;
|
|
|
|
|
|
|
|
/// Responses given by the service.
|
|
|
|
type Response;
|
|
|
|
|
|
|
|
/// Errors produced by the service.
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
/// The `TransformService` value created by this factory
|
2019-03-05 04:38:11 +01:00
|
|
|
type Transform: Service<
|
2019-02-03 19:42:27 +01:00
|
|
|
Request = Self::Request,
|
|
|
|
Response = Self::Response,
|
|
|
|
Error = Self::Error,
|
|
|
|
>;
|
|
|
|
|
|
|
|
/// Errors produced while building a service.
|
|
|
|
type InitError;
|
|
|
|
|
|
|
|
/// The future response value.
|
2019-03-05 05:29:35 +01:00
|
|
|
type Future: IntoFuture<Item = Self::Transform, Error = Self::InitError>;
|
2019-02-03 19:42:27 +01:00
|
|
|
|
|
|
|
/// Create and return a new service value asynchronously.
|
2019-03-05 04:38:11 +01:00
|
|
|
fn new_transform(&self, service: S) -> Self::Future;
|
2019-03-02 22:18:01 +01:00
|
|
|
|
|
|
|
/// Map this service's factory init error to a different error,
|
|
|
|
/// returning a new transform service factory.
|
2019-03-05 04:38:11 +01:00
|
|
|
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
|
2019-03-02 22:18:01 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::InitError) -> E,
|
|
|
|
{
|
|
|
|
TransformMapInitErr::new(self, f)
|
|
|
|
}
|
2019-02-03 19:42:27 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
impl<T, S> Transform<S> for Rc<T>
|
2019-02-03 19:42:27 +01:00
|
|
|
where
|
2019-03-05 04:38:11 +01:00
|
|
|
T: Transform<S>,
|
2019-03-02 21:16:30 +01:00
|
|
|
{
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type InitError = T::InitError;
|
2019-03-05 04:38:11 +01:00
|
|
|
type Transform = T::Transform;
|
2019-03-02 21:16:30 +01:00
|
|
|
type Future = T::Future;
|
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
fn new_transform(&self, service: S) -> T::Future {
|
|
|
|
self.as_ref().new_transform(service)
|
2019-03-02 21:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
impl<T, S> Transform<S> for Arc<T>
|
2019-03-02 21:16:30 +01:00
|
|
|
where
|
2019-03-05 04:38:11 +01:00
|
|
|
T: Transform<S>,
|
2019-03-02 21:16:30 +01:00
|
|
|
{
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type InitError = T::InitError;
|
2019-03-05 04:38:11 +01:00
|
|
|
type Transform = T::Transform;
|
2019-03-02 21:16:30 +01:00
|
|
|
type Future = T::Future;
|
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
fn new_transform(&self, service: S) -> T::Future {
|
|
|
|
self.as_ref().new_transform(service)
|
2019-03-02 21:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
/// Trait for types that can be converted to a *transform service*
|
2019-02-03 19:42:27 +01:00
|
|
|
pub trait IntoTransform<T, S>
|
|
|
|
where
|
|
|
|
T: Transform<S>,
|
|
|
|
{
|
|
|
|
/// Convert to a `TransformService`
|
|
|
|
fn into_transform(self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S> IntoTransform<T, S> for T
|
|
|
|
where
|
|
|
|
T: Transform<S>,
|
|
|
|
{
|
|
|
|
fn into_transform(self) -> T {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|