use axum::response::{IntoResponse, Response};
use bytes::{BufMut, BytesMut};
use http::{HeaderValue, StatusCode, header};
use serde::Serialize;
pub struct Yaml<T>(pub T);
impl<T> IntoResponse for Yaml<T>
where
T: Serialize,
{
fn into_response(self) -> Response {
let mut buf = BytesMut::with_capacity(128).writer();
match serde_yaml::to_writer(&mut buf, &self.0) {
Ok(()) => (
[(
header::CONTENT_TYPE,
HeaderValue::from_static("text/x-yaml"),
)],
buf.into_inner().freeze(),
)
.into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
[(
header::CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
)],
err.to_string(),
)
.into_response(),
}
}
}