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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
    Appellation: error <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::kinds::{ErrorKind, ExternalError, SyncError};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display};

#[cfg_attr(feature = "serde", derive(Deserialize, Serialize,))]
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Error<K = String> {
    kind: ErrorKind<K>,
    message: String,
}

impl<K> Error<K> {
    pub fn new(kind: ErrorKind<K>, msg: impl ToString) -> Self {
        Self {
            kind,
            message: msg.to_string(),
        }
    }
    /// Get an owned reference to the error kind
    pub fn kind(&self) -> &ErrorKind<K> {
        &self.kind
    }
    /// Get an owned reference to the error message
    pub fn message(&self) -> &str {
        &self.message
    }
    /// Set the error message
    pub fn set_message(&mut self, msg: impl ToString) {
        self.message = msg.to_string();
    }
    /// Consume the error and return the message
    pub fn into_message(self) -> String {
        self.message
    }
    /// A functional method for setting the error kind
    pub fn with_kind(mut self, kind: ErrorKind<K>) -> Self {
        self.kind = kind;
        self
    }
    /// A functional method for setting the error message
    pub fn with_message(mut self, msg: impl ToString) -> Self {
        self.message = msg.to_string();
        self
    }
}

impl<K> Display for Error<K>
where
    K: ToString,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}: {}", self.kind.to_string(), self.message)
    }
}

impl<K> std::error::Error for Error<K> where K: Debug + Display {}

impl<K> From<ErrorKind<K>> for Error<K> {
    fn from(kind: ErrorKind<K>) -> Self {
        Self::new(kind, "")
    }
}

impl<K, T> From<std::sync::TryLockError<T>> for Error<K> {
    fn from(err: std::sync::TryLockError<T>) -> Self {
        Self::new(ErrorKind::Sync(SyncError::TryLock), err.to_string())
    }
}

macro_rules! err_from {
    ($kind:expr, $t:ty) => {
        impl<E> From<$t> for Error<E> {
            fn from(err: $t) -> Self {
                Self::new($kind, err.to_string())
            }
        }
    };
    ($kind:expr => ($($t:ty),*)) => {
        $(err_from!($kind, $t);)*
    };
}

err_from!(ErrorKind::External(ExternalError::Unknown) => (&str, String, Box<dyn std::error::Error>));