use std::error::Error as StdError;
use std::fmt;
use std::result;
pub(crate) fn new_error(kind: ErrorKind) -> Error {
Error(Box::new(kind))
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub struct Error(Box<ErrorKind>);
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.0
}
pub fn into_kind(self) -> ErrorKind {
*self.0
}
}
#[derive(Debug)]
pub enum ErrorKind {
InvalidToken,
InvalidSignature,
InvalidEcdsaKey,
InvalidRsaKey,
InvalidAlgorithmName,
InvalidKeyFormat,
ExpiredSignature,
InvalidIssuer,
InvalidAudience,
InvalidSubject,
ImmatureSignature,
InvalidAlgorithm,
Base64(base64::DecodeError),
Json(serde_json::Error),
Utf8(::std::string::FromUtf8Error),
Crypto(::ring::error::Unspecified),
#[doc(hidden)]
__Nonexhaustive,
}
impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> {
match *self.0 {
ErrorKind::InvalidToken => None,
ErrorKind::InvalidSignature => None,
ErrorKind::InvalidEcdsaKey => None,
ErrorKind::InvalidRsaKey => None,
ErrorKind::ExpiredSignature => None,
ErrorKind::InvalidIssuer => None,
ErrorKind::InvalidAudience => None,
ErrorKind::InvalidSubject => None,
ErrorKind::ImmatureSignature => None,
ErrorKind::InvalidAlgorithm => None,
ErrorKind::InvalidAlgorithmName => None,
ErrorKind::InvalidKeyFormat => None,
ErrorKind::Base64(ref err) => Some(err),
ErrorKind::Json(ref err) => Some(err),
ErrorKind::Utf8(ref err) => Some(err),
ErrorKind::Crypto(ref err) => Some(err),
ErrorKind::__Nonexhaustive => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self.0 {
ErrorKind::InvalidToken
| ErrorKind::InvalidSignature
| ErrorKind::InvalidEcdsaKey
| ErrorKind::InvalidRsaKey
| ErrorKind::ExpiredSignature
| ErrorKind::InvalidIssuer
| ErrorKind::InvalidAudience
| ErrorKind::InvalidSubject
| ErrorKind::ImmatureSignature
| ErrorKind::InvalidAlgorithm
| ErrorKind::InvalidKeyFormat
| ErrorKind::InvalidAlgorithmName => write!(f, "{:?}", self.0),
ErrorKind::Json(ref err) => write!(f, "JSON error: {}", err),
ErrorKind::Utf8(ref err) => write!(f, "UTF-8 error: {}", err),
ErrorKind::Crypto(ref err) => write!(f, "Crypto error: {}", err),
ErrorKind::Base64(ref err) => write!(f, "Base64 error: {}", err),
ErrorKind::__Nonexhaustive => write!(f, "Unknown error"),
}
}
}
impl From<base64::DecodeError> for Error {
fn from(err: base64::DecodeError) -> Error {
new_error(ErrorKind::Base64(err))
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
new_error(ErrorKind::Json(err))
}
}
impl From<::std::string::FromUtf8Error> for Error {
fn from(err: ::std::string::FromUtf8Error) -> Error {
new_error(ErrorKind::Utf8(err))
}
}
impl From<::ring::error::Unspecified> for Error {
fn from(err: ::ring::error::Unspecified) -> Error {
new_error(ErrorKind::Crypto(err))
}
}
impl From<::ring::error::KeyRejected> for Error {
fn from(_err: ::ring::error::KeyRejected) -> Error {
new_error(ErrorKind::InvalidEcdsaKey)
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
new_error(kind)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_rendering() {
assert_eq!(
"InvalidAlgorithmName",
Error::from(ErrorKind::InvalidAlgorithmName).to_string()
);
}
}