From a0c69aac432521602898a39b4685673d3efa9fa9 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Thu, 9 Nov 2017 06:43:53 -0500 Subject: [PATCH] Implement std::error::Error for the Error enum (#12) --- src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ddffd05..04dad61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,8 @@ pub mod entities; pub mod registration; use std::ops; +use std::fmt; +use std::error::Error as StdError; use std::io::Error as IoError; use json::Error as SerdeError; @@ -196,6 +198,26 @@ pub enum Error { AccessTokenRequired, } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl StdError for Error { + fn description(&self) -> &str { + match *self { + Error::Api(ref e) => &e.error, + Error::Serde(ref e) => e.description(), + Error::Http(ref e) => e.description(), + Error::Io(ref e) => e.description(), + Error::ClientIdRequired => "ClientIdRequired", + Error::ClientSecretRequired => "ClientSecretRequired", + Error::AccessTokenRequired => "AccessTokenRequired", + } + } +} + /// Error returned from the Mastodon API. #[derive(Clone, Debug, Deserialize)] pub struct ApiError {