const NULL_CHAR: &'static str = "\0"; #[derive(Debug)] pub struct HTTPBody { data: json::JsonValue, } /// HTTPBody represents an HTTP request body /// for simplicity, only json body is allowed impl HTTPBody { fn new(data: json::JsonValue) -> HTTPBody { HTTPBody { data } } pub fn get_data(&self) -> &json::JsonValue { &self.data } } impl TryFrom<&str> for HTTPBody { type Error = String; fn try_from(body: &str) -> Result { let body = body.replace(NULL_CHAR, ""); match json::parse(&body) { Ok(v) => Ok(HTTPBody::new(v)), Err(e) => Err(format!("during request body parsing details={}", e)), } } } #[test] fn test_from_str() { let test_cases: [(&str, bool); 2] = [ (r#"{"access_token":"toto","refresh_token":"tutu"}"#, true), ("", false), ]; for (value, is_valid) in test_cases { let res = HTTPBody::try_from(value); if is_valid { assert!(res.is_ok()); continue; } assert!(res.is_err()); } }