From c578fcd9e2ccf9ba085f536333e3d97e438ecf2e Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 5 Dec 2018 18:22:56 +0100 Subject: [PATCH] simplify formatter code --- src/server/formatter.rs | 74 +++++++++++++++++++++++++++++++---------- src/server/rest.rs | 28 +--------------- 2 files changed, 58 insertions(+), 44 deletions(-) diff --git a/src/server/formatter.rs b/src/server/formatter.rs index fa76b4c7..decd7063 100644 --- a/src/server/formatter.rs +++ b/src/server/formatter.rs @@ -1,49 +1,89 @@ use failure::*; use serde_json::{json, Value}; +use hyper::{Body, Response, StatusCode}; +use hyper::header; + pub struct OutputFormatter { - pub format_result: fn(data: &Value) -> (Vec, &'static str), + pub format_result: fn(data: Result) -> Response, } -fn json_format_result(data: &Value) -> (Vec, &'static str) { +fn json_format_result(data: Result) -> Response { let content_type = "application/json;charset=UTF-8"; - let result = json!({ - "data": data - }); + match data { + Ok(value) => { + let result = json!({ + "data": value + }); - // todo: set result.total result.changes + // todo: set result.total result.changes - let json_str = result.to_string(); + let json_str = result.to_string(); - let raw = json_str.into_bytes(); + let raw = json_str.into_bytes(); - (raw, content_type) + let mut response = Response::new(raw.into()); + response.headers_mut().insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static(content_type)); + return response; + } + Err(err) => { + let mut response = Response::new(Body::from(err.to_string())); + response.headers_mut().insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static(content_type)); + *response.status_mut() = StatusCode::BAD_REQUEST; + return response; + } + } } pub static JSON_FORMATTER: OutputFormatter = OutputFormatter { format_result: json_format_result, }; - -fn extjs_format_result(data: &Value) -> (Vec, &'static str) { +fn extjs_format_result(data: Result) -> Response { let content_type = "application/json;charset=UTF-8"; - let result = json!({ - "data": data, - "success": true - }); + let result = match data { + Ok(value) => { + let result = json!({ + "data": value, + "success": true + }); - // todo: set result.total result.changes + // todo: set result.total result.changes + + result + } + Err(err) => { + let mut errors = vec![]; + + errors.push(err.to_string()); + + let result = json!({ + "errors": errors, + "success": false + }); + + result + } + }; let json_str = result.to_string(); let raw = json_str.into_bytes(); - (raw, content_type) + let mut response = Response::new(raw.into()); + response.headers_mut().insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static(content_type)); + response } pub static EXTJS_FORMATTER: OutputFormatter = OutputFormatter { diff --git a/src/server/rest.rs b/src/server/rest.rs index b7b8b08c..0b2ff930 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -171,36 +171,10 @@ fn handle_sync_api_request( let resp = params .and_then(move |params| { - - println!("GOT PARAMS {}", params); - - /* - let when = Instant::now() + Duration::from_millis(3000); - let task = Delay::new(when).then(|_| { - println!("A LAZY TASK"); - ok(()) - }); - - tokio::spawn(task); - */ - let res = (info.handler)(params, info)?; - Ok(res) - }).then(move |result| { - match result { - Ok(ref value) => { - - let (raw, content_type) = (formatter.format_result)(value); - - Ok(Response::builder() - .status(StatusCode::OK) - .header(header::CONTENT_TYPE, content_type) - .body(Body::from(raw))?) - } - Err(err) => Err(http_err!(BAD_REQUEST, err.to_string())) - } + Ok((formatter.format_result)(result)) }); Box::new(resp)