From 83576f1126ed3c2c331cf5aadc528d3a0d0fb0b9 Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 1 Jun 2020 22:50:58 -0700 Subject: [PATCH] basic query string parser function used to generate hashmaps of parameters This hashmap is then borrowed by child functions down to the route handlers themselves if they need it --- server/src/main.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index ced58f9..d24f79a 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -20,9 +20,22 @@ use dotenv::dotenv; mod auth; -fn map_qs(string: Option<&str>) -> HashMap<&str, Option<&str>> { - // if we can't find anything in the map then we no there is nothing - unimplemented!() +fn map_qs(query_string_raw: Option<&str>) -> HashMap<&str, &str> { + /* + * Parse a given query string and build a hashmap from it + */ + let mut map: HashMap<&str, &str> = HashMap::new(); + if let Some(qs) = query_string_raw { + let raw_pairs: Vec<&str> = qs.split('&').collect(); + for raw_pair in raw_pairs.iter() { + let sub_segment: Vec<&str> = raw_pair.split('=').collect(); + match sub_segment.len() { + 2 => map.insert(sub_segment[0], sub_segment[1]), + _ => continue + }; + } + } + map } async fn route_dispatcher(resp: &Response, meth: &Method, path: &str, params: &HashMap<&str, Option<&str>>) {