- Removed parse_json_params as its no longer used

+ Adding qs_param macro to remove boilerplate in pulling out Option<T> data from hashmaps
This commit is contained in:
shockrah 2021-02-05 16:24:39 -08:00
parent c2e384a13a
commit 8e98df1d37

View File

@ -2,9 +2,7 @@ use serde_json::{self, Value};
use hyper::http::HeaderValue; use hyper::http::HeaderValue;
use hyper::Response; use hyper::Response;
use hyper::Body; use hyper::Body;
use hyper::body::to_bytes;
use std::u8;
use std::collections::HashMap; use std::collections::HashMap;
const APP_JSON_HEADER: &'static str = "application/json"; const APP_JSON_HEADER: &'static str = "application/json";
@ -18,17 +16,6 @@ pub fn set_json_body(response: &mut Response<Body>, values: Value) {
*response.body_mut() = Body::from(values.to_string()); *response.body_mut() = Body::from(values.to_string());
} }
pub async fn parse_json_params(body_raw: &mut Body) -> Result<Value, serde_json::error::Error> {
let bytes: &[u8] = &*to_bytes(body_raw).await.unwrap(); // rarely fails
let values: Value;
if bytes.len() == 0 {
values = serde_json::from_str("{}")?;
}
else {
values = serde_json::from_slice(bytes)?;
}
Ok(values)
}
pub fn parse_query_string<'qs>(string: &'qs str) pub fn parse_query_string<'qs>(string: &'qs str)
-> HashMap<&str, &str> { -> HashMap<&str, &str> {
@ -52,12 +39,19 @@ pub fn parse_query_string<'qs>(string: &'qs str)
return map; return map;
} }
#[inline] // Pulls out Option<type> from a HashMap<&str, &str>
pub fn extract_uid(values: &HashMap<&str, &str>) -> u64 { #[macro_export]
// NOTE: this is only to be used _AFTER_ the auth module passes since it macro_rules! qs_param {
// guarantees the id is verified ($obj:expr, $id:literal, $type:ty) => {
let map = *values; match $obj.get($id) {
let val = *map.get("id").unwrap(); Some(val) => {
let s: String = String::from(val); if let Ok(pval) = val.to_string().parse::<$type>() {
return s.parse::<u64>().unwrap(); Some(pval)
} else{
None
}
},
None => None
}
}
} }