diff --git a/src/client/from_response.rs b/src/client/from_response.rs new file mode 100644 index 0000000..1416a13 --- /dev/null +++ b/src/client/from_response.rs @@ -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; +} + +/// 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" } +} diff --git a/src/client.rs b/src/client/mod.rs similarity index 98% rename from src/client.rs rename to src/client/mod.rs index d9c142a..c69df71 100644 --- a/src/client.rs +++ b/src/client/mod.rs @@ -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 { http_client: hyper::Client,