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
use std::fmt::{self, Debug, Display, Formatter};
use std::sync::Arc;

#[derive(Clone, Debug)]
enum Message {
    None,
    Static(&'static &'static str),
    Dynamic(Box<str>),
}

/// Error wraps error kind with concrete message and cause.
#[derive(Clone, Debug)]
pub struct Error<T: Clone + Debug + Display> {
    kind: T,
    message: Message,
    cause: Option<Arc<dyn std::error::Error + Send + Sync + 'static>>,
}

impl<K> Error<K>
where
    K: Clone + Debug + Display,
{
    pub(crate) fn new(kind: K) -> Error<K> {
        Error { kind, message: Message::None, cause: None }
    }

    pub(crate) fn with_description(kind: K, description: &'static &'static str) -> Error<K> {
        Error { kind, message: Message::Static(description), cause: None }
    }

    pub(crate) fn with_message<S: Into<Box<str>>>(kind: K, message: S) -> Error<K> {
        Error { kind, message: Message::Dynamic(message.into()), cause: None }
    }

    pub(crate) fn cause_by<E: std::error::Error + Send + Sync + 'static>(self, e: E) -> Self {
        let cause = Arc::new(e);
        Error { cause: Some(cause), ..self }
    }

    /// Returns error kind.
    pub fn kind(&self) -> K {
        self.kind.clone()
    }
}

impl<K> Display for Error<K>
where
    K: Clone + Debug + Display,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), fmt::Error> {
        match &self.message {
            Message::None => write!(f, "{}", self.kind),
            Message::Static(message) => write!(f, "{}: {}", self.kind, message),
            Message::Dynamic(message) => write!(f, "{}: {}", self.kind, &message),
        }
    }
}

impl<K> std::error::Error for Error<K>
where
    K: Clone + Debug + Display,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.cause {
            None => None,
            Some(arced) => Some(arced.as_ref()),
        }
    }
}