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
8/// Converts a typed Ruma response into an HTTP response.
9///
10/// Conversion failures produce an internal server error response.
11pub struct RumaResponse<T>(
12	/// Typed response to serialize.
13	pub T,
14)
15where
16	T: OutgoingResponse;
17
18impl From<Error> for RumaResponse<UiaaResponse> {
19	fn from(t: Error) -> Self { Self(t.into()) }
20}
21
22impl<T> IntoResponse for RumaResponse<T>
23where
24	T: OutgoingResponse,
25{
26	fn into_response(self) -> Response {
27		self.0
28			.try_into_http_response::<BytesMut>()
29			.inspect_err(|e| error!("response error: {e}"))
30			.map_or_else(
31				|_| StatusCode::INTERNAL_SERVER_ERROR.into_response(),
32				|r| {
33					r.map(BytesMut::freeze)
34						.map(Full::new)
35						.into_response()
36				},
37			)
38	}
39}