|
| 1 | +//! An utility library to send JSON response. |
| 2 | +//! |
| 3 | +//! # Examples |
| 4 | +//! |
| 5 | +//! ``` |
| 6 | +//! use json_response; |
| 7 | +//! |
| 8 | +//! # fn run() { |
| 9 | +//! println!("{}", json_response::add(2, 3)); |
| 10 | +//! # } |
| 11 | +//! # run(); |
| 12 | +//! ``` |
| 13 | +
|
| 14 | +use crate::prelude::*; |
| 15 | +use http::StatusCode; |
| 16 | +use hyper::{header, Body, Response}; |
| 17 | +use serde::Serialize; |
| 18 | + |
| 19 | +pub struct JsonResponse<T = ()> |
| 20 | +where |
| 21 | + T: Serialize, |
| 22 | +{ |
| 23 | + inner: Inner<T>, |
| 24 | +} |
| 25 | + |
| 26 | +enum Inner<T> { |
| 27 | + Success(SuccessData<T>), |
| 28 | + Error(ErrorData), |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Serialize, Debug, Clone)] |
| 32 | +#[serde(rename_all = "camelCase")] |
| 33 | +struct SuccessData<T> { |
| 34 | + status: Status, |
| 35 | + code: u16, |
| 36 | + data: T, |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Serialize, Debug, Clone)] |
| 40 | +#[serde(rename_all = "camelCase")] |
| 41 | +struct ErrorData { |
| 42 | + status: Status, |
| 43 | + code: u16, |
| 44 | + message: String, |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Serialize, Debug, Clone)] |
| 48 | +enum Status { |
| 49 | + #[serde(rename = "Success")] |
| 50 | + SUCCESS, |
| 51 | + #[serde(rename = "Failed")] |
| 52 | + FAILED, |
| 53 | +} |
| 54 | + |
| 55 | +impl<T: Serialize> JsonResponse<T> { |
| 56 | + pub fn with_success(code: StatusCode, data: T) -> Self { |
| 57 | + JsonResponse { |
| 58 | + inner: Inner::Success(SuccessData { |
| 59 | + status: Status::SUCCESS, |
| 60 | + code: code.as_u16(), |
| 61 | + data, |
| 62 | + }), |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl JsonResponse<()> { |
| 68 | + pub fn with_error<M: Into<String>>(code: StatusCode, message: M) -> Self { |
| 69 | + JsonResponse { |
| 70 | + inner: Inner::Error(ErrorData { |
| 71 | + status: Status::FAILED, |
| 72 | + code: code.as_u16(), |
| 73 | + message: message.into(), |
| 74 | + }), |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + pub fn with_error_include_code<M: Into<String>>(code: StatusCode, message: M) -> Self { |
| 79 | + Self::with_error( |
| 80 | + code, |
| 81 | + format!("{}: {}", code.canonical_reason().unwrap(), message.into()), |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + pub fn with_error_code(code: StatusCode) -> Self { |
| 86 | + Self::with_error(code, code.canonical_reason().unwrap().to_owned()) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<T: Serialize> JsonResponse<T> { |
| 91 | + pub fn into_response(self) -> crate::Result<Response<Body>> { |
| 92 | + let code; |
| 93 | + let body; |
| 94 | + |
| 95 | + match self.inner { |
| 96 | + Inner::Success(success_data) => { |
| 97 | + code = success_data.code; |
| 98 | + body = Body::from( |
| 99 | + serde_json::to_vec(&success_data) |
| 100 | + .context("JsonResponse: Failed to convert success data to JSON")?, |
| 101 | + ); |
| 102 | + } |
| 103 | + Inner::Error(err_data) => { |
| 104 | + code = err_data.code; |
| 105 | + body = Body::from( |
| 106 | + serde_json::to_vec(&err_data).context("JsonResponse: Failed to convert error data to JSON")?, |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + Ok(Response::builder() |
| 112 | + .status(StatusCode::from_u16(code).unwrap()) |
| 113 | + .header(header::CONTENT_TYPE, "application/json; charset=utf-8") |
| 114 | + .body(body) |
| 115 | + .context("JsonResponse: Failed to create a response")?) |
| 116 | + } |
| 117 | +} |
0 commit comments