2020-12-27 15:15:42 +01:00
|
|
|
use core::marker::PhantomData;
|
2019-05-12 15:03:50 +02:00
|
|
|
|
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.
|
2020-12-29 22:20:24 +01:00
|
|
|
pub fn map_config<I, SF, Req, F, Cfg>(factory: I, f: F) -> MapConfig<SF, Req, F, Cfg>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
I: IntoServiceFactory<SF, Req>,
|
|
|
|
SF: ServiceFactory<Req>,
|
|
|
|
F: Fn(Cfg) -> SF::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
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
/// Replace config with unit.
|
|
|
|
pub fn unit_config<I, SF, Cfg, Req>(factory: I) -> UnitConfig<SF, Cfg, 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, Config = ()>,
|
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
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct MapConfig<SF, Req, F, Cfg> {
|
|
|
|
factory: SF,
|
|
|
|
cfg_mapper: F,
|
2021-12-04 23:30:04 +01:00
|
|
|
e: PhantomData<fn(Cfg, Req)>,
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Req, F, Cfg> MapConfig<SF, Req, F, Cfg> {
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Create new `MapConfig` combinator
|
2020-12-27 05:28:00 +01:00
|
|
|
pub(crate) fn new(factory: SF, cfg_mapper: F) -> Self
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req>,
|
|
|
|
F: Fn(Cfg) -> SF::Config,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
|
|
|
Self {
|
2020-12-27 05:28:00 +01:00
|
|
|
factory,
|
|
|
|
cfg_mapper,
|
2019-05-12 15:03:50 +02:00
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Req, F, Cfg> Clone for MapConfig<SF, Req, F, Cfg>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: Clone,
|
2019-05-12 15:03:50 +02:00
|
|
|
F: Clone,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
2020-12-27 05:28:00 +01:00
|
|
|
factory: self.factory.clone(),
|
|
|
|
cfg_mapper: self.cfg_mapper.clone(),
|
2019-05-12 15:03:50 +02:00
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Req, F, Cfg> ServiceFactory<Req> for MapConfig<SF, Req, F, Cfg>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req>,
|
|
|
|
F: Fn(Cfg) -> SF::Config,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Response = SF::Response;
|
|
|
|
type Error = SF::Error;
|
2019-05-12 15:03:50 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
type Config = Cfg;
|
|
|
|
type Service = SF::Service;
|
|
|
|
type InitError = SF::InitError;
|
|
|
|
type Future = SF::Future;
|
2019-05-12 15:03:50 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
fn new_service(&self, cfg: Self::Config) -> Self::Future {
|
|
|
|
let mapped_cfg = (self.cfg_mapper)(cfg);
|
|
|
|
self.factory.new_service(mapped_cfg)
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 13:28:54 +01:00
|
|
|
/// `unit_config()` config combinator
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct UnitConfig<SF, Cfg, Req> {
|
|
|
|
factory: SF,
|
2021-12-04 23:30:04 +01:00
|
|
|
_phantom: PhantomData<fn(Cfg, Req)>,
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Cfg, Req> UnitConfig<SF, Cfg, Req>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Create new `UnitConfig` combinator
|
2020-12-27 05:28:00 +01:00
|
|
|
pub(crate) fn new(factory: SF) -> Self {
|
|
|
|
Self {
|
|
|
|
factory,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Cfg, Req> Clone for UnitConfig<SF, Cfg, Req>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: Clone,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
2020-12-27 05:28:00 +01:00
|
|
|
factory: self.factory.clone(),
|
|
|
|
_phantom: PhantomData,
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Cfg, Req> ServiceFactory<Req> for UnitConfig<SF, Cfg, Req>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Response = SF::Response;
|
|
|
|
type Error = SF::Error;
|
2019-05-12 15:03:50 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
type Config = Cfg;
|
|
|
|
type Service = SF::Service;
|
|
|
|
type InitError = SF::InitError;
|
|
|
|
type Future = SF::Future;
|
2019-05-12 15:03:50 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
fn new_service(&self, _: Cfg) -> Self::Future {
|
|
|
|
self.factory.new_service(())
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|