1use axum::{
2 extract::FromRequest,
3 http::{header, HeaderValue, StatusCode},
4 response::IntoResponse,
5};
6use bytes::{BufMut, BytesMut};
7use serde::Serialize;
8
9use crate::error::Error;
10
11#[derive(FromRequest)]
12#[from_request(via(axum::Json), rejection(Error))]
13pub struct Json<T>(pub T);
14
15impl<T: Serialize> IntoResponse for Json<T> {
17 fn into_response(self) -> axum::response::Response {
18 let mut buf = BytesMut::with_capacity(128).writer();
19 match serde_json::to_writer(&mut buf, &self.0) {
20 Ok(()) => (
21 [(
22 header::CONTENT_TYPE,
23 HeaderValue::from_static("application/scim+json"),
24 )],
25 buf.into_inner().freeze(),
26 )
27 .into_response(),
28 Err(err) => (
29 StatusCode::INTERNAL_SERVER_ERROR,
30 [(
31 header::CONTENT_TYPE,
32 HeaderValue::from_static("text/plain; charset=utf-8"),
33 )],
34 err.to_string(),
35 )
36 .into_response(),
37 }
38 }
39}