Impl Display and Error for Error

This commit is contained in:
Curtis McEnroe 2015-11-28 14:39:18 -05:00
parent 82958bab00
commit 096a9da639
1 changed files with 42 additions and 7 deletions

View File

@ -1,4 +1,4 @@
use std::result; use std::{error, fmt, io, result};
use url; use url;
use hyper; use hyper;
@ -6,21 +6,56 @@ use hyper;
/// Errors that can occur during authentication flow. /// Errors that can occur during authentication flow.
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
Io(io::Error),
Url(url::ParseError), Url(url::ParseError),
Hyper(hyper::Error), Hyper(hyper::Error),
Todo,
} }
/// Result type returned from authentication flow methods. /// Result type returned from authentication flow methods.
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
impl From<url::ParseError> for Error { impl fmt::Display for Error {
fn from(err: url::ParseError) -> Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Error::Url(err) match *self {
Error::Io(ref err) => write!(f, "{}", err),
Error::Url(ref err) => write!(f, "{}", err),
Error::Hyper(ref err) => write!(f, "{}", err),
Error::Todo => write!(f, "Not implemented!"),
}
} }
} }
impl From<hyper::Error> for Error { impl error::Error for Error {
fn from(err: hyper::Error) -> Error { fn description(&self) -> &str {
Error::Hyper(err) match *self {
Error::Io(_) => "OAuth2 IO error",
Error::Url(_) => "OAuth2 URL error",
Error::Hyper(_) => "OAuth2 Hyper error",
Error::Todo => "OAuth2 not implemented error",
} }
} }
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::Io(ref err) => Some(err),
Error::Url(ref err) => Some(err),
Error::Hyper(ref err) => Some(err),
_ => None,
}
}
}
macro_rules! impl_from {
($v:path, $t:ty) => {
impl From<$t> for Error {
fn from(err: $t) -> Error {
$v(err)
}
}
}
}
impl_from!(Error::Io, io::Error);
impl_from!(Error::Url, url::ParseError);
impl_from!(Error::Hyper, hyper::Error);