From 6d77fb406322ebba79db9a9e3db8af18ac3d97aa Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 5 Nov 2018 15:20:27 +0100 Subject: [PATCH] move parse_query into json_schema --- src/json_schema.rs | 21 +++++++++++++++++++-- src/main.rs | 30 +++++++++++------------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/json_schema.rs b/src/json_schema.rs index 6e7d7eb6..737b5805 100644 --- a/src/json_schema.rs +++ b/src/json_schema.rs @@ -1,4 +1,7 @@ +use failure::*; use std::collections::HashMap; +use url::form_urlencoded; +use serde_json::{json, Value}; pub type PropertyMap = HashMap<&'static str, Jss>; @@ -39,7 +42,7 @@ pub struct JssObject { pub description: &'static str, pub optional: Option, pub additional_properties: Option, - pub properties: Box>, + pub properties: HashMap<&'static str, Jss>, } #[derive(Debug)] @@ -107,7 +110,7 @@ macro_rules! parameter { $( map.insert(stringify!($name), $e); )* - Box::new(map) + map } }; @@ -116,6 +119,20 @@ macro_rules! parameter { } +pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss) -> Result { + + println!("QUERY Strings {:?}", data); + + Ok(json!(null)) +} + +pub fn parse_query(query: &str, schema: &Jss) -> Result { + + let raw_param: Vec<(String, String)> = + form_urlencoded::parse(query.as_bytes()).into_owned().collect(); + + parse_parameter_strings(&raw_param, schema) +} #[test] fn test_shema1() { diff --git a/src/main.rs b/src/main.rs index 9f203dbe..f2261366 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ extern crate apitest; -//use failure::*; +use failure::*; use std::collections::HashMap; use lazy_static::lazy_static; //use apitest::json_schema::*; use apitest::api_info::*; +use apitest::json_schema::*; //use serde_derive::{Serialize, Deserialize}; use serde_json::{json, Value}; @@ -25,20 +26,6 @@ macro_rules! http_error { }} } -fn parse_query(query: &str) -> Value { - - println!("PARSE QUERY {}", query); - - // fixme: what about repeated parameters (arrays?) - let mut raw_param = HashMap::new(); - for (k, v) in form_urlencoded::parse(query.as_bytes()) { - println!("QUERY PARAM {} value {}", k, v); - raw_param.insert(k, v); - } - println!("QUERY HASH {:?}", raw_param); - - return json!(null); -} fn handle_request(req: Request) -> Response { @@ -63,9 +50,9 @@ fn handle_request(req: Request) -> Response { println!("FOUND INFO"); let api_method_opt = match method { &Method::GET => &info.get, - // &Method::PUT => info.put, - // &Method::POST => info.post, - // &Method::DELETE => info.delete, + &Method::PUT => &info.put, + &Method::POST => &info.post, + &Method::DELETE => &info.delete, _ => &None, }; let api_method = match api_method_opt { @@ -77,7 +64,12 @@ fn handle_request(req: Request) -> Response { // extract param let param = match query { - Some(data) => parse_query(data), + Some(data) => { + match parse_query(data, &api_method.parameters) { + Ok(query) => query, + Err(err) => http_error!(NOT_FOUND, format!("Unable to parse query parameters '{}' - {}", data, err)), + } + } None => json!({}), };