use crate::body::{Body, HttpBody};
use crate::error::Error;
use futures::{Future, Poll};
use http::{Request, Response};
use tower_service::Service;
pub trait GrpcService<ReqBody> {
type ResponseBody: Body + HttpBody;
type Future: Future<Item = Response<Self::ResponseBody>, Error = Self::Error>;
type Error: Into<Error>;
fn poll_ready(&mut self) -> Poll<(), Self::Error>;
fn call(&mut self, request: Request<ReqBody>) -> Self::Future;
fn into_service(self) -> IntoService<Self>
where
Self: Sized,
{
IntoService(self)
}
fn as_service(&mut self) -> AsService<'_, Self>
where
Self: Sized,
{
AsService(self)
}
}
impl<T, ReqBody, ResBody> GrpcService<ReqBody> for T
where
T: Service<Request<ReqBody>, Response = Response<ResBody>>,
T::Error: Into<Error>,
ResBody: Body + HttpBody,
{
type ResponseBody = ResBody;
type Future = T::Future;
type Error = T::Error;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Service::poll_ready(self)
}
fn call(&mut self, request: Request<ReqBody>) -> Self::Future {
Service::call(self, request)
}
}
#[derive(Debug)]
pub struct AsService<'a, T>(&'a mut T);
impl<'a, T, ReqBody> Service<Request<ReqBody>> for AsService<'a, T>
where
T: GrpcService<ReqBody> + 'a,
{
type Response = Response<T::ResponseBody>;
type Future = T::Future;
type Error = T::Error;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
GrpcService::poll_ready(self.0)
}
fn call(&mut self, request: Request<ReqBody>) -> Self::Future {
GrpcService::call(self.0, request)
}
}
#[derive(Debug)]
pub struct IntoService<T>(pub(crate) T);
impl<T, ReqBody> Service<Request<ReqBody>> for IntoService<T>
where
T: GrpcService<ReqBody>,
{
type Response = Response<T::ResponseBody>;
type Future = T::Future;
type Error = T::Error;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
GrpcService::poll_ready(&mut self.0)
}
fn call(&mut self, request: Request<ReqBody>) -> Self::Future {
GrpcService::call(&mut self.0, request)
}
}