use axum::{
response::{IntoResponse, Response},
Json,
};
use http::{header, StatusCode};
use crate::ProblemDetails;
#[cfg(feature = "json")]
use crate::JsonProblemDetails;
#[cfg(feature = "xml")]
use crate::XmlProblemDetails;
#[cfg(feature = "json")]
impl<Ext> IntoResponse for JsonProblemDetails<Ext>
where
Ext: serde::Serialize,
{
fn into_response(self) -> Response {
let status_code = self.0.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
let content_type = [(header::CONTENT_TYPE, Self::CONTENT_TYPE)];
let content = Json(self.0);
(status_code, content_type, content).into_response()
}
}
#[cfg(feature = "xml")]
impl<Ext> IntoResponse for XmlProblemDetails<Ext>
where
Ext: serde::Serialize,
{
fn into_response(self) -> Response {
let status_code = self.0.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
let content_type = [(header::CONTENT_TYPE, Self::CONTENT_TYPE)];
let content = match self.to_body_string() {
Ok(xml) => xml,
Err(_) => return StatusCode::INTERNAL_SERVER_ERROR.into_response(),
};
(status_code, content_type, content).into_response()
}
}
#[cfg(feature = "json")]
impl<Ext> IntoResponse for ProblemDetails<Ext>
where
Ext: serde::Serialize,
{
fn into_response(self) -> Response {
JsonProblemDetails(self).into_response()
}
}