2019-05-12 15:03:50 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2019-12-22 13:30:49 +01:00
|
|
|
use super::{IntoServiceFactory, ServiceFactory};
|
2019-05-12 15:03:50 +02:00
|
|
|
|
2019-12-10 16:11:39 +01:00
|
|
|
/// Adapt external config argument to a config for provided service factory
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service factory and returns
|
|
|
|
/// a wrapped version of it.
|
2019-12-22 13:30:49 +01:00
|
|
|
pub fn map_config<T, U, F, C>(factory: U, f: F) -> MapConfig<T, F, C>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
T: ServiceFactory,
|
2019-12-22 13:30:49 +01:00
|
|
|
U: IntoServiceFactory<T>,
|
2019-12-02 16:27:48 +01:00
|
|
|
F: Fn(C) -> T::Config,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
2019-12-22 13:30:49 +01:00
|
|
|
MapConfig::new(factory.into_factory(), f)
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Replace config with unit
|
2019-12-22 13:30:49 +01:00
|
|
|
pub fn unit_config<T, U, C>(factory: U) -> UnitConfig<T, C>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-12-22 13:30:49 +01:00
|
|
|
U: IntoServiceFactory<T>,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
2019-12-22 13:30:49 +01:00
|
|
|
UnitConfig::new(factory.into_factory())
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 13:30:49 +01:00
|
|
|
/// `map_config()` adapter service factory
|
2019-11-18 13:28:54 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|