Skip to main content

serenade_http_axum/
dispatch.rs

1//! Dispatch helpers that run Serenade kernels from Axum.
2
3use axum::http::request::Parts;
4use axum::response::Response as AxumResponse;
5use serenade_http::{AsyncHttpKernel, HttpKernel};
6
7use crate::{conversion_error, from_axum, to_axum};
8
9/// Converts the HTTP request, runs sync `kernel`, and returns an Axum response.
10#[must_use]
11pub fn dispatch(kernel: &HttpKernel, parts: &Parts, body: impl AsRef<[u8]>) -> AxumResponse {
12    match from_axum(parts, body) {
13        Ok(serenade) => to_axum(&kernel.handle(serenade)),
14        Err(error) => conversion_error(&error),
15    }
16}
17
18/// Converts the HTTP request, awaits async `kernel`, and returns an Axum response.
19#[must_use]
20pub async fn dispatch_async(
21    kernel: &AsyncHttpKernel,
22    parts: &Parts,
23    body: impl AsRef<[u8]>,
24) -> AxumResponse {
25    match from_axum(parts, body) {
26        Ok(serenade) => to_axum(&kernel.handle(serenade).await),
27        Err(error) => conversion_error(&error),
28    }
29}