Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop requiring error kinds to impl Display #1066

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions async-nats/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@

use std::fmt::{Debug, Display};

// A trait to mark enums that can be used as error kinds.
pub trait ErrorKind: Clone + Debug + PartialEq {}

impl<T> ErrorKind for T where T: Clone + Debug + PartialEq {}

/// The error type for the NATS client, generic by the kind of error.
#[derive(Debug)]
pub struct Error<Kind>
where
Kind: Clone + Debug + Display + PartialEq,
Kind: ErrorKind,
{
pub(crate) kind: Kind,
pub(crate) source: Option<crate::Error>,
}

impl<Kind> Error<Kind>
where
Kind: Clone + Debug + Display + PartialEq,
Kind: ErrorKind,
{
pub(crate) fn new(kind: Kind) -> Self {
Self { kind, source: None }
Expand All @@ -49,7 +54,7 @@ where

impl<Kind> Display for Error<Kind>
where
Kind: Clone + Debug + Display + PartialEq,
Kind: ErrorKind + Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(err) = &self.source {
Expand All @@ -60,11 +65,16 @@ where
}
}

impl<Kind> std::error::Error for Error<Kind> where Kind: Clone + Debug + Display + PartialEq {}
impl<Kind> std::error::Error for Error<Kind>
where
Kind: ErrorKind,
Error<Kind>: Display,
{
}

impl<Kind> From<Kind> for Error<Kind>
where
Kind: Clone + Debug + Display + PartialEq,
Kind: ErrorKind,
{
fn from(kind: Kind) -> Self {
Self { kind, source: None }
Expand All @@ -75,7 +85,7 @@ where
/// by additionally specifying the kind of the target error.
trait WithKind<Kind>
where
Kind: Clone + Debug + Display + PartialEq,
Kind: ErrorKind,
Self: Into<crate::Error>,
{
fn with_kind(self, kind: Kind) -> Error<Kind> {
Expand All @@ -88,7 +98,7 @@ where

impl<E, Kind> WithKind<Kind> for E
where
Kind: Clone + Debug + Display + PartialEq,
Kind: ErrorKind,
E: Into<crate::Error>,
{
}
Expand Down