Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit caec9fc

Browse files
committed
proper handling of has_error_source flag
1 parent dcce9c6 commit caec9fc

File tree

1 file changed

+72
-34
lines changed

1 file changed

+72
-34
lines changed

src/error_chain.rs

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,69 @@
1+
#[doc(hidden)]
2+
#[macro_export]
3+
#[cfg(not(has_error_source))]
4+
macro_rules! impl_error_chain_source {
5+
(
6+
types {
7+
$error_kind_name:ident
8+
}
9+
foreign_links {
10+
$( $foreign_link_variant:ident ( $foreign_link_error_path:path )
11+
$( #[$meta_foreign_links:meta] )*; )*
12+
}) => {
13+
14+
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
15+
fn cause(&self) -> Option<&::std::error::Error> {
16+
match self.1.next_error {
17+
Some(ref c) => Some(&**c),
18+
None => {
19+
match self.0 {
20+
$(
21+
$(#[$meta_foreign_links])*
22+
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
23+
foreign_err.cause()
24+
}
25+
) *
26+
_ => None
27+
}
28+
}
29+
}
30+
}
31+
};
32+
}
33+
34+
#[cfg(has_error_source)]
35+
#[doc(hidden)]
36+
#[macro_export]
37+
macro_rules! impl_error_chain_source {
38+
(
39+
types {
40+
$error_kind_name:ident
41+
}
42+
foreign_links {
43+
$( $foreign_link_variant:ident ( $foreign_link_error_path:path )
44+
$( #[$meta_foreign_links:meta] )*; )*
45+
}) => {
46+
47+
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
48+
fn source(&self) -> Option<&(std::error::Error + 'static)> {
49+
match self.1.next_error {
50+
Some(ref c) => Some(&**c),
51+
None => {
52+
match self.0 {
53+
$(
54+
$(#[$meta_foreign_links])*
55+
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
56+
foreign_err.source()
57+
}
58+
) *
59+
_ => None
60+
}
61+
}
62+
}
63+
}
64+
};
65+
}
66+
167
/// Prefer to use `error_chain` instead of this macro.
268
#[doc(hidden)]
369
#[macro_export]
@@ -179,41 +245,13 @@ macro_rules! impl_error_chain_processed {
179245
self.description()
180246
}
181247

182-
#[cfg(not(has_error_source))]
183-
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
184-
fn cause(&self) -> Option<&::std::error::Error> {
185-
match self.1.next_error {
186-
Some(ref c) => Some(&**c),
187-
None => {
188-
match self.0 {
189-
$(
190-
$(#[$meta_foreign_links])*
191-
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
192-
foreign_err.cause()
193-
}
194-
) *
195-
_ => None
196-
}
197-
}
248+
$crate::impl_error_chain_source!{
249+
types {
250+
$error_kind_name
198251
}
199-
}
200-
201-
#[cfg(has_error_source)]
202-
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
203-
fn source(&self) -> Option<&(std::error::Error + 'static)> {
204-
match self.1.next_error {
205-
Some(ref c) => Some(&**c),
206-
None => {
207-
match self.0 {
208-
$(
209-
$(#[$meta_foreign_links])*
210-
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
211-
foreign_err.source()
212-
}
213-
) *
214-
_ => None
215-
}
216-
}
252+
foreign_links {
253+
$( $foreign_link_variant ( $foreign_link_error_path )
254+
$( #[$meta_foreign_links] )*; )*
217255
}
218256
}
219257
}

0 commit comments

Comments
 (0)