use axum::extract::{FromRequest, Request}; use axum::response::IntoResponse; use serde::Serialize; use serde::de::DeserializeOwned; use crate::error::AppError; pub struct Json(pub T); impl FromRequest for Json where T: DeserializeOwned, S: Send + Sync, { type Rejection = AppError; async fn from_request(request: Request, state: &S) -> Result { axum::Json::::from_request(request, state) .await .map(|axum::Json(value)| Self(value)) .map_err(|rejection| AppError::BadRequest(rejection.body_text())) } } impl IntoResponse for Json where T: Serialize, { fn into_response(self) -> axum::response::Response { axum::Json(self.0).into_response() } }