Skip to main content

kamu_snap_response_axum/
lib.rs

1//! `axum::response::IntoResponse` adapter for
2//! [`kamu_snap_response::SnapResponse`].
3//!
4//! Wraps `SnapResponse<T>` in a newtype that implements `IntoResponse` for
5//! axum 0.8. Defensive fallback to `INTERNAL_SERVER_ERROR` if the
6//! `responseCode` cannot be parsed back into an HTTP status.
7
8#![forbid(unsafe_code)]
9
10use axum::{Json, response::IntoResponse};
11use kamu_snap_response::SnapResponse;
12use serde::Serialize;
13
14/// Newtype wrapping `SnapResponse<T>` for axum's `IntoResponse` impl
15/// (orphan-rule shim).
16pub struct AxumResponder<T>(pub SnapResponse<T>);
17
18impl<T: Serialize> IntoResponse for AxumResponder<T> {
19    fn into_response(self) -> axum::response::Response {
20        let status = self.0.envelope.response_code.http().unwrap_or(http::StatusCode::INTERNAL_SERVER_ERROR);
21        (status, Json(self.0)).into_response()
22    }
23}
24
25/// Extension trait: `.into_axum()` on `SnapResponse<T>`.
26pub trait SnapResponderExt<T> {
27    /// Wrap into [`AxumResponder`].
28    fn into_axum(self) -> AxumResponder<T>;
29}
30
31impl<T> SnapResponderExt<T> for SnapResponse<T> {
32    fn into_axum(self) -> AxumResponder<T> {
33        AxumResponder(self)
34    }
35}