2024-07-08 02:50:10 +00:00
|
|
|
use axum::response::{IntoResponse, Response};
|
|
|
|
use bytes::BytesMut;
|
2024-12-14 21:58:01 -05:00
|
|
|
use conduwuit::{error, Error};
|
2024-07-08 02:50:10 +00:00
|
|
|
use http::StatusCode;
|
|
|
|
use http_body_util::Full;
|
|
|
|
use ruma::api::{client::uiaa::UiaaResponse, OutgoingResponse};
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
pub(crate) struct RumaResponse<T>(pub(crate) T)
|
|
|
|
where
|
|
|
|
T: OutgoingResponse;
|
2024-07-08 02:50:10 +00:00
|
|
|
|
|
|
|
impl From<Error> for RumaResponse<UiaaResponse> {
|
|
|
|
fn from(t: Error) -> Self { Self(t.into()) }
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
impl<T> IntoResponse for RumaResponse<T>
|
|
|
|
where
|
|
|
|
T: OutgoingResponse,
|
|
|
|
{
|
2024-07-08 02:50:10 +00:00
|
|
|
fn into_response(self) -> Response {
|
2024-07-09 03:36:08 +00:00
|
|
|
self.0
|
|
|
|
.try_into_http_response::<BytesMut>()
|
|
|
|
.inspect_err(|e| error!("response error: {e}"))
|
|
|
|
.map_or_else(
|
|
|
|
|_| StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
|
|
|
|r| r.map(BytesMut::freeze).map(Full::new).into_response(),
|
|
|
|
)
|
2024-07-08 02:50:10 +00:00
|
|
|
}
|
|
|
|
}
|