use super::{layer::FactoryLayer, MakeService, MapTargetService, Service};
pub struct FactoryStack<C, S> {
config: C,
inner: S,
}
impl<C> FactoryStack<C, ()> {
pub fn new(config: C) -> Self {
FactoryStack { config, inner: () }
}
}
impl<C, F> FactoryStack<C, F> {
pub fn replace<NF>(self, factory: NF) -> FactoryStack<C, NF> {
FactoryStack {
config: self.config,
inner: factory,
}
}
pub fn push<L>(self, layer: L) -> FactoryStack<C, L::Factory>
where
L: FactoryLayer<C, F>,
{
let inner = layer.layer(&self.config, self.inner);
FactoryStack {
config: self.config,
inner,
}
}
pub fn push_map_target<M: Clone>(self, f: M) -> FactoryStack<C, MapTargetService<F, M>> {
FactoryStack {
config: self.config,
inner: MapTargetService {
f,
inner: self.inner,
},
}
}
pub fn check_make_svc<R>(self) -> Self
where
F: MakeService,
F::Service: Service<R>,
{
self
}
pub fn into_inner(self) -> F {
self.inner
}
pub fn into_parts(self) -> (C, F) {
(self.config, self.inner)
}
}
impl<C, F> FactoryStack<C, F>
where
F: MakeService,
{
pub fn make(&self) -> Result<F::Service, F::Error> {
self.inner.make()
}
}