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
//! Error types used by this crate

#[macro_use]
pub mod macros;
mod framework;

pub use self::framework::{FrameworkError, FrameworkErrorKind};
pub use failure::{Backtrace, Context, Fail};
use std::fmt::{self, Display};

/// Error types used by this library, generic around `Kind`s
#[derive(Debug)]
pub struct Error<Kind>
where
    Kind: Fail + Clone + Display + Eq + PartialEq,
{
    /// Contextual information about the error
    inner: Context<Kind>,

    /// Description of the error providing additional information
    description: Option<String>,
}

impl<Kind> Error<Kind>
where
    Kind: Fail + Clone + Display + Eq + PartialEq,
{
    /// Create a new error from the given context object and description
    pub fn new<C>(into_context: C, description: Option<String>) -> Self
    where
        C: Into<Context<Kind>>,
    {
        let inner = into_context.into();
        Self { inner, description }
    }

    /// Obtain the error's `Kind`
    pub fn kind(&self) -> &Kind {
        self.inner.get_context()
    }
}

impl<Kind> Display for Error<Kind>
where
    Kind: Fail + Clone + Display + Eq + PartialEq,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.description {
            Some(ref desc) => write!(f, "{}: {}", self.kind(), desc),
            None => write!(f, "{}", self.kind()),
        }
    }
}

impl<Kind> Fail for Error<Kind>
where
    Kind: Fail + Clone + Display + Eq + PartialEq,
{
    fn cause(&self) -> Option<&dyn Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl<Kind> From<Kind> for Error<Kind>
where
    Kind: Fail + Clone + Display + Eq + PartialEq,
{
    fn from(kind: Kind) -> Self {
        Error::new(kind, None)
    }
}