2 use serde_json
::{json, Value}
;
4 use hyper
::{Body, Response, StatusCode}
;
7 pub struct OutputFormatter
{
9 pub format_result
: fn(data
: Result
<Value
, Error
>) -> Response
<Body
>,
12 fn json_format_result(data
: Result
<Value
, Error
>) -> Response
<Body
> {
14 let content_type
= "application/json;charset=UTF-8";
22 // todo: set result.total result.changes
24 let json_str
= result
.to_string();
26 let raw
= json_str
.into_bytes();
28 let mut response
= Response
::new(raw
.into());
29 response
.headers_mut().insert(
31 header
::HeaderValue
::from_static(content_type
));
35 let mut response
= Response
::new(Body
::from(err
.to_string()));
36 response
.headers_mut().insert(
38 header
::HeaderValue
::from_static(content_type
));
39 *response
.status_mut() = StatusCode
::BAD_REQUEST
;
45 pub static JSON_FORMATTER
: OutputFormatter
= OutputFormatter
{
46 format_result
: json_format_result
,
49 fn extjs_format_result(data
: Result
<Value
, Error
>) -> Response
<Body
> {
51 let content_type
= "application/json;charset=UTF-8";
53 let result
= match data
{
60 // todo: set result.total result.changes
65 let mut errors
= vec
![];
67 errors
.push(err
.to_string());
78 let json_str
= result
.to_string();
80 let raw
= json_str
.into_bytes();
82 let mut response
= Response
::new(raw
.into());
83 response
.headers_mut().insert(
85 header
::HeaderValue
::from_static(content_type
));
89 pub static EXTJS_FORMATTER
: OutputFormatter
= OutputFormatter
{
90 format_result
: extjs_format_result
,