use crate::{
Ctx, IntoService, IntoServiceFactory, ReadyBinding, Service, ServiceChain, ServiceChainFactory,
ServiceFactory,
};
pub fn ready<S, St, Req>(service: impl IntoService<S, St, Req>) -> ServiceChain<Ready<S>, St, Req>
where
S: Service<St, Req>,
{
crate::service(Ready {
service: ReadyBinding::new(service.into_service()),
})
}
pub fn ready_factory<Sf, St, Req>(
factory: impl IntoServiceFactory<Sf, St, Req>,
) -> ServiceChainFactory<ReadyFactory<Sf>, St, Req>
where
Sf: ServiceFactory<St, Req>,
{
crate::factory(ReadyFactory {
factory: factory.into_factory(),
})
}
#[derive(Debug, Clone)]
pub struct Ready<S> {
service: ReadyBinding<S>,
}
impl<S, St, Req> Service<St, Req> for Ready<S>
where
S: Service<St, Req>,
{
type Res = S::Res;
type Error = S::Error;
crate::forward_bind!(St, service);
#[inline]
async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), Self::Error> {
self.service.ready(ctx.cast()).await
}
#[inline]
async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<Self::Res, Self::Error> {
self.service.call(req, ctx.cast()).await
}
#[inline]
async fn shutdown(&self, ctx: Ctx<'_, Self, St>) {
self.service.shutdown(ctx.cast()).await;
}
}
#[derive(Debug, Clone)]
pub struct ReadyFactory<Sf> {
factory: Sf,
}
impl<Sf, St, Req> ServiceFactory<St, Req> for ReadyFactory<Sf>
where
Sf: ServiceFactory<St, Req>,
{
type Res = Sf::Res;
type Error = Sf::Error;
type InitError = Sf::InitError;
type Service = Ready<Sf::Service>;
#[inline]
async fn create(&self, st: &St) -> Result<Self::Service, Self::InitError> {
Ok(Ready {
service: ReadyBinding::new(self.factory.create(st).await?),
})
}
}