1 use core
::fmt
::{self, Debug, Display}
;
6 /// An error with context around it.
8 /// The context is intended to be a human-readable, user-facing explanation for the
9 /// error that has occurred. The underlying error is not assumed to be end-user-relevant
12 /// The `Display` impl for `Context` only prints the human-readable context, while the
13 /// `Debug` impl also prints the underlying error.
14 pub struct Context
<D
: Display
+ Send
+ Sync
+ '
static> {
18 impl<D
: Display
+ Send
+ Sync
+ '
static> Context
<D
> {
19 /// Creates a new context without an underlying error message.
20 pub fn new(context
: D
) -> Context
<D
> {
24 /// Returns a reference to the context provided with this error.
25 pub fn get_context(&self) -> &D
{
29 /// Maps `Context<D>` to `Context<T>` by applying a function to the contained context.
30 pub fn map
<F
, T
>(self, op
: F
) -> Context
<T
>
31 where F
: FnOnce(D
) -> T
,
32 T
: Display
+ Send
+ Sync
+ '
static
35 context
: op(self.context
),
39 pub(crate) fn with_err
<E
: Fail
>(context
: D
, _
: E
) -> Context
<D
> {
44 impl<D
: Display
+ Send
+ Sync
+ '
static> Fail
for Context
<D
> { }
46 impl<D
: Display
+ Send
+ Sync
+ '
static> Debug
for Context
<D
> {
47 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
48 write
!(f
, "{}", self.context
)
52 impl<D
: Display
+ Send
+ Sync
+ '
static> Display
for Context
<D
> {
53 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
54 write
!(f
, "{}", self.context
)
60 let ctx
= Context
::new("a string").map(|s
| format
!("{} with some more stuff", s
));
61 assert_eq
!(ctx
.context
, String
::from("a string with some more stuff"));
66 use {Error, Backtrace}
;
68 /// An error with context around it.
70 /// The context is intended to be a human-readable, user-facing explanation for the
71 /// error that has occurred. The underlying error is not assumed to be end-user-relevant
74 /// The `Display` impl for `Context` only prints the human-readable context, while the
75 /// `Debug` impl also prints the underlying error.
76 pub struct Context
<D
: Display
+ Send
+ Sync
+ '
static> {
78 failure
: Either
<Backtrace
, Error
>,
81 impl<D
: Display
+ Send
+ Sync
+ '
static> Context
<D
> {
82 /// Creates a new context without an underlying error message.
83 pub fn new(context
: D
) -> Context
<D
> {
84 let failure
= Either
::This(Backtrace
::new());
85 Context { context, failure }
88 /// Returns a reference to the context provided with this error.
89 pub fn get_context(&self) -> &D
{
93 /// Maps `Context<D>` to `Context<T>` by applying a function to the contained context.
94 pub fn map
<F
, T
>(self, op
: F
) -> Context
<T
>
95 where F
: FnOnce(D
) -> T
,
96 T
: Display
+ Send
+ Sync
+ '
static
99 context
: op(self.context
),
100 failure
: self.failure
,
104 pub(crate) fn with_err
<E
: Into
<Error
>>(context
: D
, error
: E
) -> Context
<D
> {
105 let failure
= Either
::That(error
.into());
106 Context { context, failure }
110 impl<D
: Display
+ Send
+ Sync
+ '
static> Fail
for Context
<D
> {
111 fn name(&self) -> Option
<&str> {
112 self.failure
.as_cause().and_then(|x
| x
.name())
115 fn cause(&self) -> Option
<&Fail
> {
116 self.failure
.as_cause()
119 fn backtrace(&self) -> Option
<&Backtrace
> {
120 Some(self.failure
.backtrace())
124 impl<D
: Display
+ Send
+ Sync
+ '
static> Debug
for Context
<D
> {
125 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
126 write
!(f
, "{:?}\n\n{}", self.failure
, self.context
)
130 impl<D
: Display
+ Send
+ Sync
+ '
static> Display
for Context
<D
> {
131 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
132 write
!(f
, "{}", self.context
)
141 impl Either
<Backtrace
, Error
> {
142 fn backtrace(&self) -> &Backtrace
{
144 Either
::This(ref backtrace
) => backtrace
,
145 Either
::That(ref error
) => error
.backtrace(),
149 fn as_cause(&self) -> Option
<&Fail
> {
151 Either
::This(_
) => None
,
152 Either
::That(ref error
) => Some(error
.as_fail())
157 impl Debug
for Either
<Backtrace
, Error
> {
158 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
160 Either
::This(ref backtrace
) => write
!(f
, "{:?}", backtrace
),
161 Either
::That(ref error
) => write
!(f
, "{:?}", error
),
168 let ctx
= Context
::new("a string").map(|s
| format
!("{} with some more stuff", s
));
169 assert_eq
!(ctx
.context
, String
::from("a string with some more stuff"));
173 impl<D
> From
<D
> for Context
<D
>
175 D
: Display
+ Send
+ Sync
+ '
static,
177 fn from(display
: D
) -> Context
<D
> {
178 Context
::new(display
)