use serde_json::{self, Value}; use hyper::http::HeaderValue; use hyper::Response; use hyper::Body; use std::collections::HashMap; const APP_JSON_HEADER: &'static str = "application/json"; const CONTENT_TYPE: &'static str = "Content-Type"; pub fn set_json_body(response: &mut Response, values: Value) { response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static(APP_JSON_HEADER)); *response.body_mut() = Body::from(values.to_string()); } pub fn parse_query_string<'qs>(string: &'qs str) -> HashMap<&str, &str> { let mut map: HashMap<&str, &str> = HashMap::new(); // get pairs of [key1=value1, key2=value2, ...] for pair in string.split('&') { let kv: Vec<&str> = pair.split('=').collect(); match (kv.get(0), kv.get(1)) { // only if the format is some_key=some_value do we actually care about it (Some(key), Some(value)) => { map.insert(key, value); }, // ignore all non-pairs _ => continue }; } return map; } // Pulls out Option from a HashMap<&str, &str> // NOTE: If you need &str just use the hashmap directly #[macro_export] macro_rules! qs_param { ($obj:expr, $id:literal, $type:ty) => { match $obj.get($id) { Some(val) => { if let Ok(pval) = val.to_string().parse::<$type>() { Some(pval) } else{ None } }, None => None } } } #[cfg(test)] mod tests { use std::collections::HashMap; #[test] fn validate_qs_param() { let mut map: HashMap<&str, &str> = HashMap::new(); map.insert("key", "value"); map.insert("asdf", "123"); // It should be noted if you want &str values then just // use the hashmap directly, since the macro is a bit clumsy with strings // Thust the cast to a String not &str is required assert_eq!(qs_param!(map, "key", String).is_some(), true); assert_eq!(qs_param!(map, "not-there", String).is_some(), false); assert_eq!(qs_param!(map, "asdf", u64).is_some(), true); assert_eq!(qs_param!(map, "asdf", bool).is_some(), false); } }