From 5a672a560e3f0ec28b9e27089d85c2da95ed98b3 Mon Sep 17 00:00:00 2001 From: midefos Date: Wed, 11 Mar 2026 21:44:34 +0100 Subject: [PATCH] also adding json_with_status --- src/responder.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/responder.rs b/src/responder.rs index 953e2db..2e6c6e4 100644 --- a/src/responder.rs +++ b/src/responder.rs @@ -23,14 +23,7 @@ impl Responder { } pub fn json(value: &T) -> Result>, 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>, Infallible> { @@ -66,4 +59,18 @@ impl Responder { .body(Full::new(body.into())) .unwrap()) } + + pub fn json_with_status( + status: StatusCode, + value: &T, + ) -> Result>, 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)), + } + } }