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
#[macro_use]
pub mod macros;
mod framework;
pub use self::framework::{FrameworkError, FrameworkErrorKind};
pub use failure::{Backtrace, Context, Fail};
use std::fmt::{self, Display};
#[derive(Debug)]
pub struct Error<Kind>
where
Kind: Fail + Clone + Display + Eq + PartialEq,
{
inner: Context<Kind>,
description: Option<String>,
}
impl<Kind> Error<Kind>
where
Kind: Fail + Clone + Display + Eq + PartialEq,
{
pub fn new<C>(into_context: C, description: Option<String>) -> Self
where
C: Into<Context<Kind>>,
{
let inner = into_context.into();
Self { inner, description }
}
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)
}
}