Implement Clone, Eq and PartialEq for Error. (#218)
* Implement Eq and PartialEq for Error. * Implement Clone for Error. serde_json::Error doesn't implement Clone, so wrapped it in an Arc.
This commit is contained in:
parent
855793f6a9
commit
7301e928b0
|
@ -1,6 +1,7 @@
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::result;
|
use std::result;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
/// A crate private constructor for `Error`.
|
/// A crate private constructor for `Error`.
|
||||||
pub(crate) fn new_error(kind: ErrorKind) -> Error {
|
pub(crate) fn new_error(kind: ErrorKind) -> Error {
|
||||||
|
@ -11,7 +12,7 @@ pub(crate) fn new_error(kind: ErrorKind) -> Error {
|
||||||
pub type Result<T> = result::Result<T, Error>;
|
pub type Result<T> = result::Result<T, Error>;
|
||||||
|
|
||||||
/// An error that can occur when encoding/decoding JWTs
|
/// An error that can occur when encoding/decoding JWTs
|
||||||
#[derive(Debug)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Error(Box<ErrorKind>);
|
pub struct Error(Box<ErrorKind>);
|
||||||
|
|
||||||
impl Error {
|
impl Error {
|
||||||
|
@ -32,7 +33,7 @@ impl Error {
|
||||||
/// attribute makes sure clients don't count on exhaustive matching.
|
/// attribute makes sure clients don't count on exhaustive matching.
|
||||||
/// (Otherwise, adding a new variant could break existing code.)
|
/// (Otherwise, adding a new variant could break existing code.)
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[derive(Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum ErrorKind {
|
pub enum ErrorKind {
|
||||||
/// When a token doesn't have a valid JWT shape
|
/// When a token doesn't have a valid JWT shape
|
||||||
InvalidToken,
|
InvalidToken,
|
||||||
|
@ -70,7 +71,7 @@ pub enum ErrorKind {
|
||||||
/// An error happened when decoding some base64 text
|
/// An error happened when decoding some base64 text
|
||||||
Base64(base64::DecodeError),
|
Base64(base64::DecodeError),
|
||||||
/// An error happened while serializing/deserializing JSON
|
/// An error happened while serializing/deserializing JSON
|
||||||
Json(serde_json::Error),
|
Json(Arc<serde_json::Error>),
|
||||||
/// Some of the text was invalid UTF-8
|
/// Some of the text was invalid UTF-8
|
||||||
Utf8(::std::string::FromUtf8Error),
|
Utf8(::std::string::FromUtf8Error),
|
||||||
/// Something unspecified went wrong with crypto
|
/// Something unspecified went wrong with crypto
|
||||||
|
@ -95,7 +96,7 @@ impl StdError for Error {
|
||||||
ErrorKind::InvalidAlgorithmName => None,
|
ErrorKind::InvalidAlgorithmName => None,
|
||||||
ErrorKind::InvalidKeyFormat => None,
|
ErrorKind::InvalidKeyFormat => None,
|
||||||
ErrorKind::Base64(ref err) => Some(err),
|
ErrorKind::Base64(ref err) => Some(err),
|
||||||
ErrorKind::Json(ref err) => Some(err),
|
ErrorKind::Json(ref err) => Some(err.as_ref()),
|
||||||
ErrorKind::Utf8(ref err) => Some(err),
|
ErrorKind::Utf8(ref err) => Some(err),
|
||||||
ErrorKind::Crypto(ref err) => Some(err),
|
ErrorKind::Crypto(ref err) => Some(err),
|
||||||
}
|
}
|
||||||
|
@ -133,6 +134,9 @@ impl PartialEq for ErrorKind {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Equality of ErrorKind is an equivalence relation: it is reflexive, symmetric and transitive.
|
||||||
|
impl Eq for ErrorKind {}
|
||||||
|
|
||||||
impl From<base64::DecodeError> for Error {
|
impl From<base64::DecodeError> for Error {
|
||||||
fn from(err: base64::DecodeError) -> Error {
|
fn from(err: base64::DecodeError) -> Error {
|
||||||
new_error(ErrorKind::Base64(err))
|
new_error(ErrorKind::Base64(err))
|
||||||
|
@ -141,7 +145,7 @@ impl From<base64::DecodeError> for Error {
|
||||||
|
|
||||||
impl From<serde_json::Error> for Error {
|
impl From<serde_json::Error> for Error {
|
||||||
fn from(err: serde_json::Error) -> Error {
|
fn from(err: serde_json::Error) -> Error {
|
||||||
new_error(ErrorKind::Json(err))
|
new_error(ErrorKind::Json(Arc::new(err)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue