I need to implement the axum::IntoResponse trait for serde structures because they come from another crate:
struct BucketInfoListAxum(BucketList);
impl IntoResponse for BucketInfoListAxum {
fn into_response(self) -> Response {
let mut headers = HeaderMap::new();
headers.typed_insert(headers::ContentType::json());
(
StatusCode::OK,
headers,
serde_json::to_string(&self.0).unwrap(),
)
.into_response()
}
}
struct ServerInfoAxum(ServerInfo);
impl IntoResponse for ServerInfoAxum {
fn into_response(self) -> Response {
let mut headers = HeaderMap::new();
headers.typed_insert(headers::ContentType::json());
(
StatusCode::OK,
headers,
serde_json::to_string(&self.0).unwrap(),
)
.into_response()
}
}
All the implementations are exactly the same, and I'm wondering if there is a more elegant way in Rust to avoid the duplication rather than extracting it into a function.