Skip to content

Commit

Permalink
Implement Clone, Eq and PartialEq for Error. (#218)
Browse files Browse the repository at this point in the history
* Implement Eq and PartialEq for Error.

* Implement Clone for Error.

serde_json::Error doesn't implement Clone, so wrapped it in an Arc.
  • Loading branch information
qwandor authored Nov 19, 2021
1 parent f913693 commit 2c89e18
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::error::Error as StdError;
use std::fmt;
use std::result;
use std::sync::Arc;

/// A crate private constructor for `Error`.
pub(crate) fn new_error(kind: ErrorKind) -> Error {
Expand All @@ -11,7 +12,7 @@ pub(crate) fn new_error(kind: ErrorKind) -> Error {
pub type Result<T> = result::Result<T, Error>;

/// An error that can occur when encoding/decoding JWTs
#[derive(Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Error(Box<ErrorKind>);

impl Error {
Expand All @@ -32,7 +33,7 @@ impl Error {
/// attribute makes sure clients don't count on exhaustive matching.
/// (Otherwise, adding a new variant could break existing code.)
#[non_exhaustive]
#[derive(Debug)]
#[derive(Clone, Debug)]
pub enum ErrorKind {
/// When a token doesn't have a valid JWT shape
InvalidToken,
Expand Down Expand Up @@ -70,7 +71,7 @@ pub enum ErrorKind {
/// An error happened when decoding some base64 text
Base64(base64::DecodeError),
/// An error happened while serializing/deserializing JSON
Json(serde_json::Error),
Json(Arc<serde_json::Error>),
/// Some of the text was invalid UTF-8
Utf8(::std::string::FromUtf8Error),
/// Something unspecified went wrong with crypto
Expand All @@ -95,7 +96,7 @@ impl StdError for Error {
ErrorKind::InvalidAlgorithmName => None,
ErrorKind::InvalidKeyFormat => None,
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::Crypto(ref err) => Some(err),
}
Expand Down Expand Up @@ -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 {
fn from(err: base64::DecodeError) -> Error {
new_error(ErrorKind::Base64(err))
Expand All @@ -141,7 +145,7 @@ impl From<base64::DecodeError> for Error {

impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
new_error(ErrorKind::Json(err))
new_error(ErrorKind::Json(Arc::new(err)))
}
}

Expand Down

0 comments on commit 2c89e18

Please sign in to comment.