Add JsonObjectHelper::get_string_option

This commit is contained in:
Curtis McEnroe 2015-12-23 22:38:14 -05:00
parent cb404df6d8
commit 228c732e2e
2 changed files with 8 additions and 5 deletions

View File

@ -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"))
}
}

View File

@ -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(),