- 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:
parent
c2e384a13a
commit
8e98df1d37
@ -2,9 +2,7 @@ use serde_json::{self, Value};
|
||||
use hyper::http::HeaderValue;
|
||||
use hyper::Response;
|
||||
use hyper::Body;
|
||||
use hyper::body::to_bytes;
|
||||
|
||||
use std::u8;
|
||||
use std::collections::HashMap;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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)
|
||||
-> HashMap<&str, &str> {
|
||||
@ -52,12 +39,19 @@ pub fn parse_query_string<'qs>(string: &'qs str)
|
||||
return map;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn extract_uid(values: &HashMap<&str, &str>) -> u64 {
|
||||
// NOTE: this is only to be used _AFTER_ the auth module passes since it
|
||||
// guarantees the id is verified
|
||||
let map = *values;
|
||||
let val = *map.get("id").unwrap();
|
||||
let s: String = String::from(val);
|
||||
return s.parse::<u64>().unwrap();
|
||||
// Pulls out Option<type> from a HashMap<&str, &str>
|
||||
#[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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user