Implement Error, Display for Error enum

This commit is contained in:
Thomas Gideon 2017-03-14 14:40:02 -04:00
parent 053327f7dd
commit 52452923d2
2 changed files with 38 additions and 3 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "medallion"
version = "2.0.1"
version = "2.0.2"
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
description = "JWT library for rust using serde, serde_json and openssl"
homepage = "http://github.com/commandline/medallion"

View File

@ -1,18 +1,53 @@
use base64::Base64Error;
use serde_json;
use std::error;
use std::fmt;
use std::string::FromUtf8Error;
use openssl::error::ErrorStack;
#[derive(Debug)]
pub enum Error {
Format,
Custom(String),
Utf8(FromUtf8Error),
Base64(Base64Error),
JSON(serde_json::Error),
Custom(String),
Crypto(ErrorStack),
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Custom(ref message) => &message,
Error::Utf8(ref err) => err.description(),
Error::Base64(ref err) => err.description(),
Error::JSON(ref err) => err.description(),
Error::Crypto(ref err) => err.description(),
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::Custom(_) => None,
Error::Utf8(ref err) => Some(err),
Error::Base64(ref err) => Some(err),
Error::JSON(ref err) => Some(err),
Error::Crypto(ref err) => Some(err),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Custom(ref message) => f.write_str(&message),
Error::Utf8(ref err) => err.fmt(f),
Error::Base64(ref err) => err.fmt(f),
Error::JSON(ref err) => err.fmt(f),
Error::Crypto(ref err) => err.fmt(f),
}
}
}
macro_rules! error_wrap {
($f: ty, $e: expr) => {
impl From<$f> for Error {