use std::convert::Infallible;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use axum::Router;
use axum::body::Body;
use axum::http::{Request, Response};
use tower::{Service, ServiceExt};
use crate::H3RequestBody;
pub fn router_into_h3_service(router: Router) -> AxumH3Service {
AxumH3Service { router }
}
#[derive(Clone, Debug)]
pub struct AxumH3Service {
router: Router,
}
impl Service<Request<H3RequestBody>> for AxumH3Service {
type Response = Response<Body>;
type Error = Infallible;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, request: Request<H3RequestBody>) -> Self::Future {
let router = self.router.clone();
Box::pin(async move {
let (parts, body) = request.into_parts();
let request = Request::from_parts(parts, Body::new(body));
router.oneshot(request).await.map_err(match_infallible)
})
}
}
fn match_infallible(error: Infallible) -> Infallible {
match error {}
}