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

Commit 1d00e82

Browse files
committed
Allow conditional compilation for error variants.
Closes #53.
1 parent 6af2cc5 commit 1d00e82

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

src/lib.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,12 @@
131131
//! // the `rustup_dist::ErrorKind`, with conversions from
132132
//! // `rustup_dist::Error`.
133133
//! //
134+
//! // Optionally, some attributes can be added to a variant.
135+
//! //
134136
//! // This section can be empty.
135137
//! links {
136138
//! rustup_dist::Error, rustup_dist::ErrorKind, Dist;
137-
//! rustup_utils::Error, rustup_utils::ErrorKind, Utils;
139+
//! rustup_utils::Error, rustup_utils::ErrorKind, Utils, #[cfg(unix)];
138140
//! }
139141
//!
140142
//! // Automatic conversions between this error chain and other
@@ -143,9 +145,12 @@
143145
//! // `ErrorKind::Temp` variant. The description and cause will
144146
//! // forward to the description and cause of the original error.
145147
//! //
148+
//! // Optionally, some attributes can be added to a variant.
149+
//! //
146150
//! // This section can be empty.
147151
//! foreign_links {
148152
//! temp::Error, Temp;
153+
//! io::Error, Io, #[cfg(unix)];
149154
//! }
150155
//!
151156
//! // Define additional `ErrorKind` variants. The syntax here is
@@ -321,11 +326,11 @@ macro_rules! error_chain {
321326
}
322327

323328
links {
324-
$( $link_error_path:path, $link_kind_path:path, $link_variant:ident; ) *
329+
$( $link_error_path:path, $link_kind_path:path, $link_variant:ident $(, #[$meta_links:meta])*; ) *
325330
}
326331

327332
foreign_links {
328-
$( $foreign_link_error_path:path, $foreign_link_variant:ident; )*
333+
$( $foreign_link_error_path:path, $foreign_link_variant:ident $(, #[$meta_foreign_links:meta])*; )*
329334
}
330335

331336
errors {
@@ -373,6 +378,7 @@ macro_rules! error_chain {
373378
None => {
374379
match self.0 {
375380
$(
381+
$(#[$meta_foreign_links])*
376382
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
377383
foreign_err.cause()
378384
}
@@ -391,6 +397,7 @@ macro_rules! error_chain {
391397
}
392398

393399
$(
400+
$(#[$meta_links])*
394401
impl From<$link_error_path> for $error_name {
395402
fn from(e: $link_error_path) -> Self {
396403
$error_name($error_kind_name::$link_variant(e.0), e.1)
@@ -399,6 +406,7 @@ macro_rules! error_chain {
399406
) *
400407

401408
$(
409+
$(#[$meta_foreign_links])*
402410
impl From<$foreign_link_error_path> for $error_name {
403411
fn from(e: $foreign_link_error_path) -> Self {
404412
$error_name(
@@ -443,13 +451,15 @@ macro_rules! error_chain {
443451
}
444452

445453
$(
454+
$(#[$meta_links])*
446455
$link_variant(e: $link_kind_path) {
447456
description(e.description())
448457
display("{}", e)
449458
}
450459
) *
451460

452461
$(
462+
$(#[$meta_foreign_links])*
453463
$foreign_link_variant(err: $foreign_link_error_path) {
454464
description(::std::error::Error::description(err))
455465
display("{}", err)
@@ -461,6 +471,7 @@ macro_rules! error_chain {
461471
}
462472

463473
$(
474+
$(#[$meta_links])*
464475
impl From<$link_kind_path> for $error_kind_name {
465476
fn from(e: $link_kind_path) -> Self {
466477
$error_kind_name::$link_variant(e)
@@ -559,11 +570,11 @@ macro_rules! error_chain {
559570
}
560571

561572
$( links {
562-
$( $link_error_path:path, $link_kind_path:path, $link_variant:ident; ) *
573+
$( $link_chunks:tt ) *
563574
} ) *
564575

565576
$( foreign_links {
566-
$( $foreign_link_error_path:path, $foreign_link_variant:ident; ) *
577+
$( $foreign_link_chunks:tt ) *
567578
} ) *
568579

569580
$( errors {
@@ -576,11 +587,11 @@ macro_rules! error_chain {
576587
}
577588

578589
links {
579-
$( $( $link_error_path, $link_kind_path, $link_variant; ) * ) *
590+
$( $( $link_chunks ) * ) *
580591
}
581592

582593
foreign_links {
583-
$( $( $foreign_link_error_path, $foreign_link_variant; ) * ) *
594+
$( $( $foreign_link_chunks ) * ) *
584595
}
585596

586597
errors {
@@ -593,11 +604,11 @@ macro_rules! error_chain {
593604
types { }
594605

595606
$( links {
596-
$( $link_error_path:path, $link_kind_path:path, $link_variant:ident; ) *
607+
$( $link_chunks:tt ) *
597608
} ) *
598609

599610
$( foreign_links {
600-
$( $foreign_link_error_path:path, $foreign_link_variant:ident; ) *
611+
$( $foreign_link_chunks:tt ) *
601612
} ) *
602613

603614
$( errors {
@@ -610,11 +621,11 @@ macro_rules! error_chain {
610621
}
611622

612623
links {
613-
$( $( $link_error_path, $link_kind_path, $link_variant; ) * ) *
624+
$( $( $link_chunks ) * ) *
614625
}
615626

616627
foreign_links {
617-
$( $( $foreign_link_error_path, $foreign_link_variant; ) * ) *
628+
$( $( $foreign_link_chunks ) * ) *
618629
}
619630

620631
errors {
@@ -625,11 +636,11 @@ macro_rules! error_chain {
625636
// Case 3: types section not present
626637
(
627638
$( links {
628-
$( $link_error_path:path, $link_kind_path:path, $link_variant:ident; ) *
639+
$( $link_chunks:tt ) *
629640
} ) *
630641

631642
$( foreign_links {
632-
$( $foreign_link_error_path:path, $foreign_link_variant:ident; ) *
643+
$( $foreign_link_chunks:tt ) *
633644
} ) *
634645

635646
$( errors {
@@ -640,11 +651,11 @@ macro_rules! error_chain {
640651
types { }
641652

642653
links {
643-
$( $( $link_error_path, $link_kind_path, $link_variant; ) * ) *
654+
$( $( $link_chunks ) * ) *
644655
}
645656

646657
foreign_links {
647-
$( $( $foreign_link_error_path, $foreign_link_variant; ) * ) *
658+
$( $( $foreign_link_chunks ) * ) *
648659
}
649660

650661
errors {

src/quick_error.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! quick_error {
3030
=> $iitem: $imode [$( $ivar: $ityp ),*] )*]
3131
);
3232
quick_error!(IMPLEMENTATIONS $name {$(
33-
$iitem: $imode [$( $ivar: $ityp ),*] {$( $ifuncs )*}
33+
$iitem: $imode [$(#[$imeta])*] [$( $ivar: $ityp ),*] {$( $ifuncs )*}
3434
)*});
3535
$(
3636
quick_error!(ERROR_CHECK $imode $($ifuncs)*);
@@ -49,7 +49,7 @@ macro_rules! quick_error {
4949
=> $iitem: $imode [$( $ivar: $ityp ),*] )*]
5050
);
5151
quick_error!(IMPLEMENTATIONS $name {$(
52-
$iitem: $imode [$( $ivar: $ityp ),*] {$( $ifuncs )*}
52+
$iitem: $imode [$(#[$imeta])*] [$( $ivar: $ityp ),*] {$( $ifuncs )*}
5353
)*});
5454
$(
5555
quick_error!(ERROR_CHECK $imode $($ifuncs)*);
@@ -254,7 +254,7 @@ macro_rules! quick_error {
254254
};
255255
(IMPLEMENTATIONS
256256
$name:ident {$(
257-
$item:ident: $imode:tt [$( $var:ident: $typ:ty ),*] {$( $funcs:tt )*}
257+
$item:ident: $imode:tt [$(#[$imeta:meta])*] [$( $var:ident: $typ:ty ),*] {$( $funcs:tt )*}
258258
)*}
259259
) => {
260260
#[allow(unused)]
@@ -264,6 +264,7 @@ macro_rules! quick_error {
264264
{
265265
match *self {
266266
$(
267+
$(#[$imeta])*
267268
quick_error!(ITEM_PATTERN
268269
$name $item: $imode [$( ref $var ),*]
269270
) => {
@@ -311,6 +312,7 @@ macro_rules! quick_error {
311312
pub fn description(&self) -> &str {
312313
match *self {
313314
$(
315+
$(#[$imeta])*
314316
quick_error!(ITEM_PATTERN
315317
$name $item: $imode [$( ref $var ),*]
316318
) => {

tests/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,36 @@ mod foreign_link_test {
253253
Ok(())
254254
}
255255
}
256+
257+
#[cfg(test)]
258+
mod attributes_test {
259+
#[allow(unused_imports)]
260+
use std::io;
261+
262+
mod inner {
263+
error_chain! {
264+
265+
}
266+
}
267+
268+
error_chain! {
269+
types {
270+
Error, ErrorKind, ErrorChain, Result;
271+
}
272+
273+
links {
274+
inner::Error, inner::ErrorKind, Inner, #[cfg(not(test))];
275+
}
276+
277+
foreign_links {
278+
io::Error, Io, #[cfg(not(test))];
279+
}
280+
281+
errors {
282+
#[cfg(not(test))]
283+
AnError {
284+
285+
}
286+
}
287+
}
288+
}

0 commit comments

Comments
 (0)