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)), + } + } }