Add JsonObjectHelper::get_string_option
This commit is contained in:
parent
cb404df6d8
commit
228c732e2e
|
@ -73,10 +73,13 @@ impl<'a> JsonHelper<'a> {
|
|||
pub struct JsonObjectHelper<'a>(pub &'a json::Object);
|
||||
|
||||
impl<'a> JsonObjectHelper<'a> {
|
||||
/// Gets a field as a string or returns `None`.
|
||||
pub fn get_string_option(&self, key: &'static str) -> Option<&'a str> {
|
||||
self.0.get(key).and_then(Json::as_string)
|
||||
}
|
||||
|
||||
/// Gets a field as a string or fails with `ParseError::ExpectedFieldType`.
|
||||
pub fn get_string(&self, key: &'static str) -> Result<&'a str, ParseError> {
|
||||
self.0.get(key)
|
||||
.and_then(Json::as_string)
|
||||
.ok_or(ParseError::ExpectedFieldType(key, "string"))
|
||||
self.get_string_option(key).ok_or(ParseError::ExpectedFieldType(key, "string"))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,8 +92,8 @@ impl FromResponse for OAuth2Error {
|
|||
let obj = try!(json.as_object());
|
||||
|
||||
let code = try!(obj.get_string("error"));
|
||||
let description = obj.0.get("error_description").and_then(Json::as_string);
|
||||
let uri = obj.0.get("error_uri").and_then(Json::as_string);
|
||||
let description = obj.get_string_option("error_description");
|
||||
let uri = obj.get_string_option("error_uri");
|
||||
|
||||
Ok(OAuth2Error {
|
||||
code: code.into(),
|
||||
|
|
Loading…
Reference in New Issue