use crate::{Request, Response, Status, Streaming};
use std::future::Future;
use tokio_stream::Stream;
use tower_service::Service;
pub trait UnaryService<R> {
    type Response;
    type Future: Future<Output = Result<Response<Self::Response>, Status>>;
    fn call(&mut self, request: Request<R>) -> Self::Future;
}
impl<T, M1, M2> UnaryService<M1> for T
where
    T: Service<Request<M1>, Response = Response<M2>, Error = crate::Status>,
{
    type Response = M2;
    type Future = T::Future;
    fn call(&mut self, request: Request<M1>) -> Self::Future {
        Service::call(self, request)
    }
}
pub trait ServerStreamingService<R> {
    type Response;
    type ResponseStream: Stream<Item = Result<Self::Response, Status>>;
    type Future: Future<Output = Result<Response<Self::ResponseStream>, Status>>;
    fn call(&mut self, request: Request<R>) -> Self::Future;
}
impl<T, S, M1, M2> ServerStreamingService<M1> for T
where
    T: Service<Request<M1>, Response = Response<S>, Error = crate::Status>,
    S: Stream<Item = Result<M2, crate::Status>>,
{
    type Response = M2;
    type ResponseStream = S;
    type Future = T::Future;
    fn call(&mut self, request: Request<M1>) -> Self::Future {
        Service::call(self, request)
    }
}
pub trait ClientStreamingService<R> {
    type Response;
    type Future: Future<Output = Result<Response<Self::Response>, Status>>;
    fn call(&mut self, request: Request<Streaming<R>>) -> Self::Future;
}
impl<T, M1, M2> ClientStreamingService<M1> for T
where
    T: Service<Request<Streaming<M1>>, Response = Response<M2>, Error = crate::Status>,
{
    type Response = M2;
    type Future = T::Future;
    fn call(&mut self, request: Request<Streaming<M1>>) -> Self::Future {
        Service::call(self, request)
    }
}
pub trait StreamingService<R> {
    type Response;
    type ResponseStream: Stream<Item = Result<Self::Response, Status>>;
    type Future: Future<Output = Result<Response<Self::ResponseStream>, Status>>;
    fn call(&mut self, request: Request<Streaming<R>>) -> Self::Future;
}
impl<T, S, M1, M2> StreamingService<M1> for T
where
    T: Service<Request<Streaming<M1>>, Response = Response<S>, Error = crate::Status>,
    S: Stream<Item = Result<M2, crate::Status>>,
{
    type Response = M2;
    type ResponseStream = S;
    type Future = T::Future;
    fn call(&mut self, request: Request<Streaming<M1>>) -> Self::Future {
        Service::call(self, request)
    }
}