Skip to main content

tuwunel_api/router/
response.rs

1use axum::response::{IntoResponse, Response};
2use bytes::BytesMut;
3use http::StatusCode;
4use http_body_util::Full;
5use ruma::api::{OutgoingResponse, client::uiaa::UiaaResponse};
6use tuwunel_core::{Error, error};
7
8pub(crate) struct RumaResponse<T>(pub(crate) T)
9where
10	T: OutgoingResponse;
11
12impl From<Error> for RumaResponse<UiaaResponse> {
13	fn from(t: Error) -> Self { Self(t.into()) }
14}
15
16impl<T> IntoResponse for RumaResponse<T>
17where
18	T: OutgoingResponse,
19{
20	fn into_response(self) -> Response {
21		self.0
22			.try_into_http_response::<BytesMut>()
23			.inspect_err(|e| error!("response error: {e}"))
24			.map_or_else(
25				|_| StatusCode::INTERNAL_SERVER_ERROR.into_response(),
26				|r| {
27					r.map(BytesMut::freeze)
28						.map(Full::new)
29						.into_response()
30				},
31			)
32	}
33}