| 1 | use std::error::Error as StdError; |
| 2 | use std::future::Future; |
| 3 | |
| 4 | use crate::body::Body; |
| 5 | use crate::service::service::Service; |
| 6 | use crate::{Request, Response}; |
| 7 | |
| 8 | /// An asynchronous function from `Request` to `Response`. |
| 9 | pub trait HttpService<ReqBody>: sealed::Sealed<ReqBody> { |
| 10 | /// The `Body` body of the `http::Response`. |
| 11 | type ResBody: Body; |
| 12 | |
| 13 | /// The error type that can occur within this `Service`. |
| 14 | /// |
| 15 | /// Note: Returning an `Error` to a hyper server will cause the connection |
| 16 | /// to be abruptly aborted. In most cases, it is better to return a `Response` |
| 17 | /// with a 4xx or 5xx status code. |
| 18 | type Error: Into<Box<dyn StdError + Send + Sync>>; |
| 19 | |
| 20 | /// The `Future` returned by this `Service`. |
| 21 | type Future: Future<Output = Result<Response<Self::ResBody>, Self::Error>>; |
| 22 | |
| 23 | #[doc (hidden)] |
| 24 | fn call(&mut self, req: Request<ReqBody>) -> Self::Future; |
| 25 | } |
| 26 | |
| 27 | impl<T, B1, B2> HttpService<B1> for T |
| 28 | where |
| 29 | T: Service<Request<B1>, Response = Response<B2>>, |
| 30 | B2: Body, |
| 31 | T::Error: Into<Box<dyn StdError + Send + Sync>>, |
| 32 | { |
| 33 | type ResBody = B2; |
| 34 | |
| 35 | type Error = T::Error; |
| 36 | type Future = T::Future; |
| 37 | |
| 38 | fn call(&mut self, req: Request<B1>) -> Self::Future { |
| 39 | Service::call(self, req) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl<T, B1, B2> sealed::Sealed<B1> for T |
| 44 | where |
| 45 | T: Service<Request<B1>, Response = Response<B2>>, |
| 46 | B2: Body, |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | mod sealed { |
| 51 | pub trait Sealed<T> {} |
| 52 | } |
| 53 | |