pub extern crate json_gettext;
extern crate rocket;
mod json_response_code;
pub use json_response_code::JSONResponseCode;
use std::io::Cursor;
use json_gettext::JSONGetTextValue;
use rocket::request::Request;
use rocket::response::{self, Response, Responder};
pub struct JSONResponse<'a> {
pub code: Box<JSONResponseCode>,
pub data: JSONGetTextValue<'a>,
}
impl<'a> Responder<'a> for JSONResponse<'a> {
fn respond_to(self, _: &Request) -> response::Result<'a> {
let json = format!("{{\"code\":{},\"data\":{}}}", self.code.get_code(), self.data.to_json());
let mut response = Response::build();
response
.raw_header("Content-Type", "application/json")
.raw_header("Content-Length", json.len().to_string())
.sized_body(Cursor::new(json));
response.ok()
}
}
impl<'a> JSONResponse<'a> {
pub fn ok(data: JSONGetTextValue<'a>) -> Self {
JSONResponse {
code: Box::new(0),
data,
}
}
pub fn err<T: JSONResponseCode + 'static>(code: T, data: JSONGetTextValue<'a>) -> Self {
JSONResponse {
code: Box::new(code),
data,
}
}
}
pub struct JSONResponseWithoutData {
pub code: Box<JSONResponseCode>
}
impl JSONResponseWithoutData {
pub fn ok() -> Self {
JSONResponseWithoutData { code: Box::new(0) }
}
pub fn err<T: JSONResponseCode + 'static>(code: T) -> Self {
JSONResponseWithoutData { code: Box::new(code) }
}
}
impl<'a> Responder<'a> for JSONResponseWithoutData {
fn respond_to(self, _: &Request) -> response::Result<'a> {
let json = format!("{{\"code\":{}}}", self.code.get_code());
let mut response = Response::build();
response
.raw_header("Content-Type", "application/json")
.raw_header("Content-Length", json.len().to_string())
.sized_body(Cursor::new(json));
response.ok()
}
}