mm1_common/errors/
error_kind.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::fmt;

use crate::types::Never;

pub trait HasErrorKind<Kind: fmt::Display + Eq + Ord + Copy + Send + Sync + 'static> {
    fn kind(&self) -> Kind;
}

impl<AnyKind> HasErrorKind<AnyKind> for Never
where
    AnyKind: fmt::Display + Eq + Ord + Copy + Send + Sync + 'static,
{
    fn kind(&self) -> AnyKind {
        match *self {}
    }
}

#[macro_export]
macro_rules! impl_error_kind {
    ($ty:ty) => {
        impl std::fmt::Display for $ty {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                std::fmt::Debug::fmt(self, f)
            }
        }
        impl std::error::Error for $ty {}
        impl $crate::errors::error_kind::HasErrorKind<Self> for $ty {
            fn kind(&self) -> Self {
                *self
            }
        }
    };
}