From 17798190cf6fd4fd57273a91f906d36666f6ee65 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 17 Jun 2017 03:36:12 +0100 Subject: [PATCH] error: Implement fmt::Display and error::Error. --- src/error.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/error.rs b/src/error.rs index 899e25f0..f8d7372b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,6 +8,8 @@ use std::convert::From; use std::io; use std::num; use std::string; +use std::fmt; +use std::error; use base64; use minidom; @@ -26,6 +28,36 @@ pub enum Error { ChronoParseError(chrono::ParseError), } +impl fmt::Display for Error { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match *self { + Error::ParseError(s) => write!(fmt, "{}", s), + Error::IoError(ref e) => write!(fmt, "{}", e), + Error::XMLError(ref e) => write!(fmt, "{}", e), + Error::Base64Error(ref e) => write!(fmt, "{}", e), + Error::ParseIntError(ref e) => write!(fmt, "{}", e), + Error::ParseStringError(ref e) => write!(fmt, "{}", e), + Error::JidParseError(_) => write!(fmt, "JID parse error"), + Error::ChronoParseError(ref e) => write!(fmt, "{}", e), + } + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + match *self { + Error::ParseError(s) => s, + Error::IoError(ref e) => e.description(), + Error::XMLError(ref e) => e.description(), + Error::Base64Error(ref e) => e.description(), + Error::ParseIntError(ref e) => e.description(), + Error::ParseStringError(ref e) => e.description(), + Error::JidParseError(_) => "JID parse error", + Error::ChronoParseError(ref e) => e.description(), + } + } +} + impl From for Error { fn from(err: io::Error) -> Error { Error::IoError(err)