2019-05-12 06:03:50 -07:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
use super::ServiceFactory;
|
2019-05-12 06:03:50 -07:00
|
|
|
|
|
|
|
pub enum MappedConfig<'a, T> {
|
|
|
|
Ref(&'a T),
|
|
|
|
Owned(T),
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
/// Adapt external config to a config for provided new service
|
|
|
|
pub fn map_config<T, F, C>(
|
|
|
|
factory: T,
|
|
|
|
f: F,
|
|
|
|
) -> impl ServiceFactory<
|
|
|
|
Config = C,
|
|
|
|
Request = T::Request,
|
|
|
|
Response = T::Response,
|
|
|
|
Error = T::Error,
|
|
|
|
InitError = T::InitError,
|
|
|
|
>
|
|
|
|
where
|
|
|
|
T: ServiceFactory,
|
|
|
|
F: Fn(&C) -> MappedConfig<T::Config>,
|
|
|
|
{
|
|
|
|
MapConfig::new(factory, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replace config with unit
|
|
|
|
pub fn unit_config<T, C>(
|
|
|
|
new_service: T,
|
|
|
|
) -> impl ServiceFactory<
|
|
|
|
Config = C,
|
|
|
|
Request = T::Request,
|
|
|
|
Response = T::Response,
|
|
|
|
Error = T::Error,
|
|
|
|
InitError = T::InitError,
|
|
|
|
>
|
|
|
|
where
|
|
|
|
T: ServiceFactory<Config = ()>,
|
|
|
|
{
|
|
|
|
UnitConfig::new(new_service)
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
/// `MapInitErr` service combinator
|
2019-11-14 18:38:24 +06:00
|
|
|
pub(crate) struct MapConfig<A, F, C> {
|
2019-05-12 06:03:50 -07:00
|
|
|
a: A,
|
|
|
|
f: F,
|
|
|
|
e: PhantomData<C>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, F, C> MapConfig<A, F, C> {
|
|
|
|
/// Create new `MapConfig` combinator
|
|
|
|
pub fn new(a: A, f: F) -> Self
|
|
|
|
where
|
2019-11-14 18:38:24 +06:00
|
|
|
A: ServiceFactory,
|
2019-05-12 06:03:50 -07:00
|
|
|
F: Fn(&C) -> MappedConfig<A::Config>,
|
|
|
|
{
|
|
|
|
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 18:38:24 +06:00
|
|
|
impl<A, F, C> ServiceFactory for MapConfig<A, F, C>
|
2019-05-12 06:03:50 -07:00
|
|
|
where
|
2019-11-14 18:38:24 +06:00
|
|
|
A: ServiceFactory,
|
2019-05-12 06:03:50 -07:00
|
|
|
F: Fn(&C) -> MappedConfig<A::Config>,
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
|
|
|
match (self.f)(cfg) {
|
|
|
|
MappedConfig::Ref(cfg) => self.a.new_service(cfg),
|
|
|
|
MappedConfig::Owned(cfg) => self.a.new_service(&cfg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `MapInitErr` service combinator
|
2019-11-14 18:38:24 +06:00
|
|
|
pub(crate) struct UnitConfig<A, C> {
|
2019-05-12 06:03:50 -07:00
|
|
|
a: A,
|
|
|
|
e: PhantomData<C>,
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<A, C> UnitConfig<A, C>
|
|
|
|
where
|
|
|
|
A: ServiceFactory<Config = ()>,
|
|
|
|
{
|
2019-05-12 06:03:50 -07:00
|
|
|
/// Create new `UnitConfig` combinator
|
2019-11-14 18:38:24 +06:00
|
|
|
pub(crate) fn new(a: A) -> Self {
|
2019-05-12 06:03:50 -07: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 18:38:24 +06:00
|
|
|
impl<A, C> ServiceFactory for UnitConfig<A, C>
|
2019-05-12 06:03:50 -07:00
|
|
|
where
|
2019-11-14 18:38:24 +06:00
|
|
|
A: ServiceFactory<Config = ()>,
|
2019-05-12 06:03:50 -07: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;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &C) -> Self::Future {
|
|
|
|
self.a.new_service(&())
|
|
|
|
}
|
|
|
|
}
|