@@ -313,44 +313,6 @@ mod quick_error;
313
313
314
314
#[ macro_export]
315
315
macro_rules! error_chain {
316
-
317
- // Provide default for types block
318
- (
319
- types {
320
- }
321
-
322
- links {
323
- $( $link_error_path: path, $link_kind_path: path, $link_variant: ident; ) *
324
- }
325
-
326
- foreign_links {
327
- $( $foreign_link_error_path: path, $foreign_link_variant: ident; ) *
328
- }
329
-
330
- errors {
331
- $( $error_chunks: tt ) *
332
- }
333
-
334
- ) => (
335
- error_chain! {
336
- types {
337
- Error , ErrorKind , ChainErr , Result ;
338
- }
339
-
340
- links {
341
- $( $link_error_path, $link_kind_path, $link_variant; ) *
342
- }
343
-
344
- foreign_links {
345
- $( $foreign_link_error_path, $foreign_link_variant; ) *
346
- }
347
-
348
- errors {
349
- $( $error_chunks ) *
350
- }
351
- }
352
- ) ;
353
-
354
316
(
355
317
types {
356
318
$error_name: ident, $error_kind_name: ident,
@@ -583,17 +545,52 @@ macro_rules! error_chain {
583
545
pub type $result_name<T > = :: std:: result:: Result <T , $error_name>;
584
546
} ;
585
547
586
- // Allow missing sections
587
- // There should only ever be zero or one of each section, but there's currently no
588
- // way to express that in a macro
548
+ // Handle missing sections, or missing type names in types { }
549
+ //
550
+ // Macros cannot specify "zero or one repetitions" at the moment, so we allow
551
+ // repeating sections. Only for the `types` section this makes no sense, which
552
+ // is the reason for the three separate cases.
553
+ //
554
+ // Case 1: types fully specified
589
555
(
556
+ types {
557
+ $error_name: ident, $error_kind_name: ident,
558
+ $chain_error_name: ident, $result_name: ident;
559
+ }
590
560
591
- $( types {
592
- $(
593
- $error_name: ident, $error_kind_name: ident,
594
- $chain_error_name: ident, $result_name: ident;
595
- ) *
561
+ $( links {
562
+ $( $link_error_path: path, $link_kind_path: path, $link_variant: ident; ) *
563
+ } ) *
564
+
565
+ $( foreign_links {
566
+ $( $foreign_link_error_path: path, $foreign_link_variant: ident; ) *
567
+ } ) *
568
+
569
+ $( errors {
570
+ $( $error_chunks: tt ) *
596
571
} ) *
572
+ ) => (
573
+ error_chain! {
574
+ types {
575
+ $error_name, $error_kind_name, $chain_error_name, $result_name;
576
+ }
577
+
578
+ links {
579
+ $( $( $link_error_path, $link_kind_path, $link_variant; ) * ) *
580
+ }
581
+
582
+ foreign_links {
583
+ $( $( $foreign_link_error_path, $foreign_link_variant; ) * ) *
584
+ }
585
+
586
+ errors {
587
+ $( $( $error_chunks ) * ) *
588
+ }
589
+ }
590
+ ) ;
591
+ // Case 2: types section present, but empty
592
+ (
593
+ types { }
597
594
598
595
$( links {
599
596
$( $link_error_path: path, $link_kind_path: path, $link_variant: ident; ) *
@@ -609,12 +606,39 @@ macro_rules! error_chain {
609
606
) => (
610
607
error_chain! {
611
608
types {
612
- $( $(
613
- $error_name, $error_kind_name,
614
- $chain_error_name, $result_name;
615
- ) * ) *
609
+ Error , ErrorKind , ChainErr , Result ;
610
+ }
611
+
612
+ links {
613
+ $( $( $link_error_path, $link_kind_path, $link_variant; ) * ) *
616
614
}
617
615
616
+ foreign_links {
617
+ $( $( $foreign_link_error_path, $foreign_link_variant; ) * ) *
618
+ }
619
+
620
+ errors {
621
+ $( $( $error_chunks ) * ) *
622
+ }
623
+ }
624
+ ) ;
625
+ // Case 3: types section not present
626
+ (
627
+ $( links {
628
+ $( $link_error_path: path, $link_kind_path: path, $link_variant: ident; ) *
629
+ } ) *
630
+
631
+ $( foreign_links {
632
+ $( $foreign_link_error_path: path, $foreign_link_variant: ident; ) *
633
+ } ) *
634
+
635
+ $( errors {
636
+ $( $error_chunks: tt ) *
637
+ } ) *
638
+ ) => (
639
+ error_chain! {
640
+ types { }
641
+
618
642
links {
619
643
$( $( $link_error_path, $link_kind_path, $link_variant; ) * ) *
620
644
}
0 commit comments