Add FromResponse trait and ParseError

This commit is contained in:
Curtis McEnroe 2015-12-22 00:02:29 -05:00
parent 557257e71d
commit bc9555c9b9
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,45 @@
use std::error::Error;
use std::fmt;
use rustc_serialize::json::Json;
/// Response parsing.
pub trait FromResponse: Sized {
/// Parse a JSON response.
fn from_response(json: &Json) -> Result<Self, ParseError>;
}
/// Response parse errors.
#[derive(Debug)]
pub enum ParseError {
/// Expected response to be of type.
ExpectedType(&'static str),
/// Expected field to be of type.
ExpectedFieldType(&'static str, &'static str),
/// Expected field to equal value.
ExpectedFieldValue(&'static str, &'static str),
/// Expected field to not be present.
UnexpectedField(&'static str),
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
ParseError::ExpectedType(t) =>
write!(f, "Expected response of type {}", t),
ParseError::ExpectedFieldType(k, t) =>
write!(f, "Expected field {} of type {}", k, t),
ParseError::ExpectedFieldValue(k, v) =>
write!(f, "Expected field {} to equal {}", k, v),
ParseError::UnexpectedField(k) =>
write!(f, "Unexpected field {}", k),
}
}
}
impl Error for ParseError {
fn description(&self) -> &str { "response parse error" }
}

View File

@ -7,6 +7,9 @@ use url::{self, form_urlencoded, Url};
use provider::Provider;
pub use self::from_response::{FromResponse, ParseError};
mod from_response;
/// OAuth 2.0 client.
pub struct Client<P: Provider> {
http_client: hyper::Client,