]> git.proxmox.com Git - rustc.git/blob - src/vendor/failure/src/context.rs
New upstream version 1.29.0+dfsg1
[rustc.git] / src / vendor / failure / src / context.rs
1 use core::fmt::{self, Debug, Display};
2
3 use Fail;
4
5 without_std! {
6 /// An error with context around it.
7 ///
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
10 /// information.
11 ///
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> {
15 context: D,
16 }
17
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> {
21 Context { context }
22 }
23
24 /// Returns a reference to the context provided with this error.
25 pub fn get_context(&self) -> &D {
26 &self.context
27 }
28
29 pub(crate) fn with_err<E: Fail>(context: D, _: E) -> Context<D> {
30 Context { context }
31 }
32 }
33
34 impl<D: Display + Send + Sync + 'static> Fail for Context<D> { }
35
36 impl<D: Display + Send + Sync + 'static> Debug for Context<D> {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 write!(f, "{}", self.context)
39 }
40 }
41
42 impl<D: Display + Send + Sync + 'static> Display for Context<D> {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 write!(f, "{}", self.context)
45 }
46 }
47 }
48
49 with_std! {
50 use {Error, Backtrace};
51
52 /// An error with context around it.
53 ///
54 /// The context is intended to be a human-readable, user-facing explanation for the
55 /// error that has occurred. The underlying error is not assumed to be end-user-relevant
56 /// information.
57 ///
58 /// The `Display` impl for `Context` only prints the human-readable context, while the
59 /// `Debug` impl also prints the underlying error.
60 pub struct Context<D: Display + Send + Sync + 'static> {
61 context: D,
62 failure: Either<Backtrace, Error>,
63 }
64
65 impl<D: Display + Send + Sync + 'static> Context<D> {
66 /// Creates a new context without an underlying error message.
67 pub fn new(context: D) -> Context<D> {
68 let failure = Either::This(Backtrace::new());
69 Context { context, failure }
70 }
71
72 /// Returns a reference to the context provided with this error.
73 pub fn get_context(&self) -> &D {
74 &self.context
75 }
76
77 pub(crate) fn with_err<E: Into<Error>>(context: D, error: E) -> Context<D> {
78 let failure = Either::That(error.into());
79 Context { context, failure }
80 }
81 }
82
83 impl<D: Display + Send + Sync + 'static> Fail for Context<D> {
84 fn cause(&self) -> Option<&Fail> {
85 self.failure.as_cause()
86 }
87
88 fn backtrace(&self) -> Option<&Backtrace> {
89 Some(self.failure.backtrace())
90 }
91 }
92
93 impl<D: Display + Send + Sync + 'static> Debug for Context<D> {
94 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95 write!(f, "{:?}\n\n{}", self.failure, self.context)
96 }
97 }
98
99 impl<D: Display + Send + Sync + 'static> Display for Context<D> {
100 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101 write!(f, "{}", self.context)
102 }
103 }
104
105 enum Either<A, B> {
106 This(A),
107 That(B),
108 }
109
110 impl Either<Backtrace, Error> {
111 fn backtrace(&self) -> &Backtrace {
112 match *self {
113 Either::This(ref backtrace) => backtrace,
114 Either::That(ref error) => error.backtrace(),
115 }
116 }
117
118 fn as_cause(&self) -> Option<&Fail> {
119 match *self {
120 Either::This(_) => None,
121 Either::That(ref error) => Some(error.as_fail())
122 }
123 }
124 }
125
126 impl Debug for Either<Backtrace, Error> {
127 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128 match *self {
129 Either::This(ref backtrace) => write!(f, "{:?}", backtrace),
130 Either::That(ref error) => write!(f, "{:?}", error),
131 }
132 }
133 }
134 }
135
136 impl<D> From<D> for Context<D>
137 where
138 D: Display + Send + Sync + 'static,
139 {
140 fn from(display: D) -> Context<D> {
141 Context::new(display)
142 }
143 }