Skip to main content

siderite_core/
service.rs

1//! Opaque tower service produced from an [`App`](crate::App).
2
3use crate::body::Body;
4use std::convert::Infallible;
5use std::future::Future;
6use std::pin::Pin;
7use std::task::{Context, Poll};
8use tower::Service;
9
10/// A cloneable `tower::Service` handling `http::Request<Body>`; used for
11/// in-process testing without opening a socket.
12#[derive(Debug, Clone)]
13pub struct RouterService {
14    inner: axum::Router,
15}
16
17impl RouterService {
18    pub(crate) fn new(inner: axum::Router) -> Self {
19        Self { inner }
20    }
21}
22
23impl Service<http::Request<Body>> for RouterService {
24    type Response = http::Response<Body>;
25    type Error = Infallible;
26    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Infallible>> + Send>>;
27
28    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Infallible>> {
29        Service::<http::Request<axum::body::Body>>::poll_ready(&mut self.inner, cx)
30    }
31
32    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
33        let fut = self.inner.call(req.map(Body::into_inner));
34        Box::pin(async move {
35            match fut.await {
36                Ok(response) => Ok(response.map(Body::from_inner)),
37                Err(never) => match never {},
38            }
39        })
40    }
41}