2019-11-14 13:38:24 +01:00
|
|
|
use std::task::{Context, Poll};
|
2020-12-27 05:28:00 +01:00
|
|
|
use std::{future::Future, marker::PhantomData};
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
use crate::and_then::{AndThenService, AndThenServiceFactory};
|
2019-12-03 14:59:28 +01:00
|
|
|
use crate::and_then_apply_fn::{AndThenApplyFn, AndThenApplyFnFactory};
|
2019-11-29 08:51:00 +01:00
|
|
|
use crate::map::{Map, MapServiceFactory};
|
|
|
|
use crate::map_err::{MapErr, MapErrServiceFactory};
|
|
|
|
use crate::map_init_err::MapInitErr;
|
2019-11-18 09:30:04 +01:00
|
|
|
use crate::then::{ThenService, ThenServiceFactory};
|
2019-11-14 13:38:24 +01:00
|
|
|
use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
|
|
|
|
2020-09-13 11:12:07 +02:00
|
|
|
/// Construct new pipeline with one service in pipeline chain.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn pipeline<I, S, Req>(service: I) -> Pipeline<S, Req>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
I: IntoService<S, Req>,
|
|
|
|
S: Service<Req>,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
|
|
|
Pipeline {
|
|
|
|
service: service.into_service(),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:12:07 +02:00
|
|
|
/// Construct new pipeline factory with one service factory.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn pipeline_factory<I, SF, Req>(factory: I) -> PipelineFactory<SF, Req>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
I: IntoServiceFactory<SF, Req>,
|
|
|
|
SF: ServiceFactory<Req>,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
|
|
|
PipelineFactory {
|
|
|
|
factory: factory.into_factory(),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 16:34:51 +01:00
|
|
|
/// Pipeline service - pipeline allows to compose multiple service into one service.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct Pipeline<S, Req> {
|
|
|
|
service: S,
|
|
|
|
_phantom: PhantomData<Req>,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<S, Req> Pipeline<S, Req>
|
|
|
|
where
|
|
|
|
S: Service<Req>,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Call another service after call to this one has resolved successfully.
|
|
|
|
///
|
|
|
|
/// This function can be used to chain two services together and ensure that
|
|
|
|
/// the second service isn't called until call to the fist service have
|
|
|
|
/// finished. Result of the call to the first service is used as an
|
|
|
|
/// input parameter for the second service's call.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn and_then<I, S1>(
|
2020-01-17 01:58:11 +01:00
|
|
|
self,
|
2020-12-27 05:28:00 +01:00
|
|
|
service: I,
|
|
|
|
) -> Pipeline<impl Service<Req, Response = S1::Response, Error = S::Error> + Clone, Req>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
I: IntoService<S1, S::Response>,
|
|
|
|
S1: Service<S::Response, Error = S::Error>,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
|
|
|
Pipeline {
|
2019-11-18 09:30:04 +01:00
|
|
|
service: AndThenService::new(self.service, service.into_service()),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
/// Apply function to specified service and use it as a next service in chain.
|
2019-12-03 14:59:28 +01:00
|
|
|
///
|
2020-12-27 05:28:00 +01:00
|
|
|
/// Short version of `pipeline_factory(...).and_then(apply_fn(...))`
|
|
|
|
pub fn and_then_apply_fn<I, S1, F, Fut, In, Res, Err>(
|
2019-12-03 14:59:28 +01:00
|
|
|
self,
|
|
|
|
service: I,
|
2020-12-27 05:28:00 +01:00
|
|
|
wrap_fn: F,
|
|
|
|
) -> Pipeline<impl Service<Req, Response = Res, Error = Err> + Clone, Req>
|
2019-12-03 14:59:28 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
I: IntoService<S1, In>,
|
|
|
|
S1: Service<In>,
|
|
|
|
F: FnMut(S::Response, &mut S1) -> Fut,
|
2019-12-03 14:59:28 +01:00
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2020-12-27 05:28:00 +01:00
|
|
|
Err: From<S::Error> + From<S1::Error>,
|
2019-12-03 14:59:28 +01:00
|
|
|
{
|
|
|
|
Pipeline {
|
2020-12-27 05:28:00 +01:00
|
|
|
service: AndThenApplyFn::new(self.service, service.into_service(), wrap_fn),
|
|
|
|
_phantom: PhantomData,
|
2019-12-03 14:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Chain on a computation for when a call to the service finished,
|
|
|
|
/// passing the result of the call to the next service `U`.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving pipeline and returns a
|
|
|
|
/// wrapped version of it.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn then<F, S1>(
|
2020-01-17 01:58:11 +01:00
|
|
|
self,
|
|
|
|
service: F,
|
2020-12-27 05:28:00 +01:00
|
|
|
) -> Pipeline<impl Service<Req, Response = S1::Response, Error = S::Error> + Clone, Req>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
F: IntoService<S1, Result<S::Response, S::Error>>,
|
|
|
|
S1: Service<Result<S::Response, S::Error>, Error = S::Error>,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
|
|
|
Pipeline {
|
2019-11-18 09:30:04 +01:00
|
|
|
service: ThenService::new(self.service, service.into_service()),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-29 08:51:00 +01:00
|
|
|
|
|
|
|
/// Map this service's output to a different type, returning a new service
|
|
|
|
/// of the resulting type.
|
|
|
|
///
|
|
|
|
/// This function is similar to the `Option::map` or `Iterator::map` where
|
|
|
|
/// it will change the type of the underlying service.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it, similar to the existing `map` methods in the
|
|
|
|
/// standard library.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn map<F, R>(self, f: F) -> Pipeline<Map<S, F, Req, R>, Req>
|
2019-11-29 08:51:00 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
F: FnMut(S::Response) -> R,
|
2019-11-29 08:51:00 +01:00
|
|
|
{
|
|
|
|
Pipeline {
|
|
|
|
service: Map::new(self.service, f),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-29 08:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map this service's error to a different error, returning a new service.
|
|
|
|
///
|
|
|
|
/// This function is similar to the `Result::map_err` where it will change
|
|
|
|
/// the error type of the underlying service. This is useful for example to
|
|
|
|
/// ensure that services have the same error type.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn map_err<F, E>(self, f: F) -> Pipeline<MapErr<S, Req, F, E>, Req>
|
2019-11-29 08:51:00 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
F: Fn(S::Error) -> E,
|
2019-11-29 08:51:00 +01:00
|
|
|
{
|
|
|
|
Pipeline {
|
|
|
|
service: MapErr::new(self.service, f),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-29 08:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<T, Req> Clone for Pipeline<T, Req>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
T: Clone,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Pipeline {
|
|
|
|
service: self.service.clone(),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<S: Service<Req>, Req> Service<Req> for Pipeline<S, Req> {
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
2019-11-14 13:38:24 +01:00
|
|
|
|
|
|
|
#[inline]
|
2020-12-27 05:28:00 +01:00
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
|
2019-11-14 13:38:24 +01:00
|
|
|
self.service.poll_ready(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-12-27 05:28:00 +01:00
|
|
|
fn call(&mut self, req: Req) -> Self::Future {
|
2019-11-14 13:38:24 +01:00
|
|
|
self.service.call(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 16:34:51 +01:00
|
|
|
/// Pipeline factory
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct PipelineFactory<SF, Req> {
|
|
|
|
factory: SF,
|
|
|
|
_phantom: PhantomData<Req>,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Req> PipelineFactory<SF, Req>
|
|
|
|
where
|
|
|
|
SF: ServiceFactory<Req>,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Call another service after call to this one has resolved successfully.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn and_then<I, SF1>(
|
2020-01-17 01:58:11 +01:00
|
|
|
self,
|
2020-12-27 05:28:00 +01:00
|
|
|
factory: I,
|
2020-01-17 01:58:11 +01:00
|
|
|
) -> PipelineFactory<
|
|
|
|
impl ServiceFactory<
|
2020-12-27 05:28:00 +01:00
|
|
|
Req,
|
|
|
|
Response = SF1::Response,
|
|
|
|
Error = SF::Error,
|
|
|
|
Config = SF::Config,
|
|
|
|
InitError = SF::InitError,
|
|
|
|
Service = impl Service<Req, Response = SF1::Response, Error = SF::Error> + Clone,
|
2020-01-17 01:58:11 +01:00
|
|
|
> + Clone,
|
2020-12-27 05:28:00 +01:00
|
|
|
Req,
|
2020-01-17 01:58:11 +01:00
|
|
|
>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
SF::Config: Clone,
|
|
|
|
I: IntoServiceFactory<SF1, SF::Response>,
|
|
|
|
SF1: ServiceFactory<
|
|
|
|
SF::Response,
|
|
|
|
Config = SF::Config,
|
|
|
|
Error = SF::Error,
|
|
|
|
InitError = SF::InitError,
|
2019-11-14 13:38:24 +01:00
|
|
|
>,
|
|
|
|
{
|
|
|
|
PipelineFactory {
|
2019-11-18 09:30:04 +01:00
|
|
|
factory: AndThenServiceFactory::new(self.factory, factory.into_factory()),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
/// Apply function to specified service and use it as a next service in chain.
|
2019-12-03 14:59:28 +01:00
|
|
|
///
|
|
|
|
/// Short version of `pipeline_factory(...).and_then(apply_fn_factory(...))`
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn and_then_apply_fn<I, SF1, Fut, F, In, Res, Err>(
|
2019-12-03 14:59:28 +01:00
|
|
|
self,
|
|
|
|
factory: I,
|
2020-12-27 05:28:00 +01:00
|
|
|
wrap_fn: F,
|
2020-01-17 01:58:11 +01:00
|
|
|
) -> PipelineFactory<
|
|
|
|
impl ServiceFactory<
|
2020-12-27 05:28:00 +01:00
|
|
|
Req,
|
2020-01-17 01:58:11 +01:00
|
|
|
Response = Res,
|
|
|
|
Error = Err,
|
2020-12-27 05:28:00 +01:00
|
|
|
Config = SF::Config,
|
|
|
|
InitError = SF::InitError,
|
|
|
|
Service = impl Service<Req, Response = Res, Error = Err> + Clone,
|
2020-01-17 01:58:11 +01:00
|
|
|
> + Clone,
|
2020-12-27 05:28:00 +01:00
|
|
|
Req,
|
2020-01-17 01:58:11 +01:00
|
|
|
>
|
2019-12-03 14:59:28 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
SF::Config: Clone,
|
|
|
|
I: IntoServiceFactory<SF1, In>,
|
|
|
|
SF1: ServiceFactory<In, Config = SF::Config, InitError = SF::InitError>,
|
|
|
|
F: FnMut(SF::Response, &mut SF1::Service) -> Fut + Clone,
|
2019-12-03 14:59:28 +01:00
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2020-12-27 05:28:00 +01:00
|
|
|
Err: From<SF::Error> + From<SF1::Error>,
|
2019-12-03 14:59:28 +01:00
|
|
|
{
|
|
|
|
PipelineFactory {
|
2020-12-27 05:28:00 +01:00
|
|
|
factory: AndThenApplyFnFactory::new(self.factory, factory.into_factory(), wrap_fn),
|
|
|
|
_phantom: PhantomData,
|
2019-12-03 14:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Create `NewService` to chain on a computation for when a call to the
|
|
|
|
/// service finished, passing the result of the call to the next
|
|
|
|
/// service `U`.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving pipeline and returns a
|
|
|
|
/// wrapped version of it.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn then<I, SF1>(
|
2020-01-17 01:58:11 +01:00
|
|
|
self,
|
2020-12-27 05:28:00 +01:00
|
|
|
factory: I,
|
2020-01-17 01:58:11 +01:00
|
|
|
) -> PipelineFactory<
|
|
|
|
impl ServiceFactory<
|
2020-12-27 05:28:00 +01:00
|
|
|
Req,
|
|
|
|
Response = SF1::Response,
|
|
|
|
Error = SF::Error,
|
|
|
|
Config = SF::Config,
|
|
|
|
InitError = SF::InitError,
|
|
|
|
Service = impl Service<Req, Response = SF1::Response, Error = SF::Error> + Clone,
|
2020-01-17 01:58:11 +01:00
|
|
|
> + Clone,
|
2020-12-27 05:28:00 +01:00
|
|
|
Req,
|
2020-01-17 01:58:11 +01:00
|
|
|
>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
SF::Config: Clone,
|
|
|
|
I: IntoServiceFactory<SF1, Result<SF::Response, SF::Error>>,
|
|
|
|
SF1: ServiceFactory<
|
|
|
|
Result<SF::Response, SF::Error>,
|
|
|
|
Config = SF::Config,
|
|
|
|
Error = SF::Error,
|
|
|
|
InitError = SF::InitError,
|
2019-11-14 13:38:24 +01:00
|
|
|
>,
|
|
|
|
{
|
|
|
|
PipelineFactory {
|
2019-11-18 09:30:04 +01:00
|
|
|
factory: ThenServiceFactory::new(self.factory, factory.into_factory()),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-29 08:51:00 +01:00
|
|
|
|
|
|
|
/// Map this service's output to a different type, returning a new service
|
|
|
|
/// of the resulting type.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn map<F, R>(self, f: F) -> PipelineFactory<MapServiceFactory<SF, F, Req, R>, Req>
|
2019-11-29 08:51:00 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
F: FnMut(SF::Response) -> R + Clone,
|
2019-11-29 08:51:00 +01:00
|
|
|
{
|
|
|
|
PipelineFactory {
|
|
|
|
factory: MapServiceFactory::new(self.factory, f),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-29 08:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map this service's error to a different error, returning a new service.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn map_err<F, E>(
|
|
|
|
self,
|
|
|
|
f: F,
|
|
|
|
) -> PipelineFactory<MapErrServiceFactory<SF, Req, F, E>, Req>
|
2019-11-29 08:51:00 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
F: Fn(SF::Error) -> E + Clone,
|
2019-11-29 08:51:00 +01:00
|
|
|
{
|
|
|
|
PipelineFactory {
|
|
|
|
factory: MapErrServiceFactory::new(self.factory, f),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-29 08:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map this factory's init error to a different error, returning a new service.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn map_init_err<F, E>(self, f: F) -> PipelineFactory<MapInitErr<SF, F, Req, E>, Req>
|
2019-11-29 08:51:00 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2020-12-27 05:28:00 +01:00
|
|
|
F: Fn(SF::InitError) -> E + Clone,
|
2019-11-29 08:51:00 +01:00
|
|
|
{
|
|
|
|
PipelineFactory {
|
|
|
|
factory: MapInitErr::new(self.factory, f),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-29 08:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<T, Req> Clone for PipelineFactory<T, Req>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
T: Clone,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
PipelineFactory {
|
|
|
|
factory: self.factory.clone(),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Req> ServiceFactory<Req> for PipelineFactory<SF, Req>
|
|
|
|
where
|
|
|
|
SF: ServiceFactory<Req>,
|
|
|
|
{
|
|
|
|
type Config = SF::Config;
|
|
|
|
type Response = SF::Response;
|
|
|
|
type Error = SF::Error;
|
|
|
|
type Service = SF::Service;
|
|
|
|
type InitError = SF::InitError;
|
|
|
|
type Future = SF::Future;
|
2019-11-14 13:38:24 +01:00
|
|
|
|
|
|
|
#[inline]
|
2020-12-27 05:28:00 +01:00
|
|
|
fn new_service(&self, cfg: SF::Config) -> Self::Future {
|
2019-11-14 13:38:24 +01:00
|
|
|
self.factory.new_service(cfg)
|
|
|
|
}
|
|
|
|
}
|