also adding json_with_status

This commit is contained in:
2026-03-11 21:44:34 +01:00
parent 04dff9dbe2
commit 5a672a560e

View File

@@ -23,14 +23,7 @@ impl Responder {
}
pub fn json<T: Serialize>(value: &T) -> Result<Response<Full<Bytes>>, Infallible> {
match serde_json::to_vec(value) {
Ok(bytes) => Ok(Response::builder()
.status(StatusCode::OK)
.header(CONTENT_TYPE, "application/json")
.body(Full::new(Bytes::from(bytes)))
.unwrap()),
Err(e) => Self::internal_error(format!("JSON Serialization Error: {}", e)),
}
Self::json_with_status(StatusCode::OK, value)
}
pub fn redirect(url: &str) -> Result<Response<Full<Bytes>>, Infallible> {
@@ -66,4 +59,18 @@ impl Responder {
.body(Full::new(body.into()))
.unwrap())
}
pub fn json_with_status<T: Serialize>(
status: StatusCode,
value: &T,
) -> Result<Response<Full<Bytes>>, Infallible> {
match serde_json::to_vec(value) {
Ok(bytes) => Ok(Response::builder()
.status(status)
.header(CONTENT_TYPE, "application/json")
.body(Full::new(Bytes::from(bytes)))
.unwrap()),
Err(e) => Self::internal_error(format!("JSON Serialization Error: {}", e)),
}
}
}