2019-05-12 15:03:50 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use super::ServiceFactory;
|
2019-05-12 15:03:50 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Adapt external config to a config for provided new service
|
2019-11-18 13:28:54 +01:00
|
|
|
pub fn map_config<T, F, C>(factory: T, f: F) -> MapConfig<T, F, C>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
T: ServiceFactory,
|
2019-12-02 16:27:48 +01:00
|
|
|
F: Fn(C) -> T::Config,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
|
|
|
MapConfig::new(factory, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replace config with unit
|
2019-11-18 13:28:54 +01:00
|
|
|
pub fn unit_config<T, C>(new_service: T) -> UnitConfig<T, C>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
T: ServiceFactory<Config = ()>,
|
|
|
|
{
|
|
|
|
UnitConfig::new(new_service)
|
|
|
|
}
|
|
|
|
|
2019-11-18 13:28:54 +01:00
|
|
|
/// `.map_config()` service combinator
|
|
|
|
pub struct MapConfig<A, F, C> {
|
2019-05-12 15:03:50 +02:00
|
|
|
a: A,
|
|
|
|
f: F,
|
|
|
|
e: PhantomData<C>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, F, C> MapConfig<A, F, C> {
|
|
|
|
/// Create new `MapConfig` combinator
|
2019-11-18 13:28:54 +01:00
|
|
|
pub(crate) fn new(a: A, f: F) -> Self
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-12-02 16:27:48 +01:00
|
|
|
F: Fn(C) -> A::Config,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
f,
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, F, C> Clone for MapConfig<A, F, C>
|
|
|
|
where
|
|
|
|
A: Clone,
|
|
|
|
F: Clone,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<A, F, C> ServiceFactory for MapConfig<A, F, C>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-12-02 16:27:48 +01:00
|
|
|
F: Fn(C) -> A::Config,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
|
|
|
type Request = A::Request;
|
|
|
|
type Response = A::Response;
|
|
|
|
type Error = A::Error;
|
|
|
|
|
|
|
|
type Config = C;
|
|
|
|
type Service = A::Service;
|
|
|
|
type InitError = A::InitError;
|
|
|
|
type Future = A::Future;
|
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, cfg: C) -> Self::Future {
|
|
|
|
self.a.new_service((self.f)(cfg))
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 13:28:54 +01:00
|
|
|
/// `unit_config()` config combinator
|
|
|
|
pub struct UnitConfig<A, C> {
|
2019-05-12 15:03:50 +02:00
|
|
|
a: A,
|
|
|
|
e: PhantomData<C>,
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<A, C> UnitConfig<A, C>
|
|
|
|
where
|
|
|
|
A: ServiceFactory<Config = ()>,
|
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Create new `UnitConfig` combinator
|
2019-11-14 13:38:24 +01:00
|
|
|
pub(crate) fn new(a: A) -> Self {
|
2019-05-12 15:03:50 +02:00
|
|
|
Self { a, e: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, C> Clone for UnitConfig<A, C>
|
|
|
|
where
|
|
|
|
A: Clone,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<A, C> ServiceFactory for UnitConfig<A, C>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory<Config = ()>,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
|
|
|
type Request = A::Request;
|
|
|
|
type Response = A::Response;
|
|
|
|
type Error = A::Error;
|
|
|
|
|
|
|
|
type Config = C;
|
|
|
|
type Service = A::Service;
|
|
|
|
type InitError = A::InitError;
|
|
|
|
type Future = A::Future;
|
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, _: C) -> Self::Future {
|
|
|
|
self.a.new_service(())
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|