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

Commit 42ebcb1

Browse files
committed
Fixup matching, disallow repeating "types" (closes #23)
It is actually not a problem to have repeating instances of `links`, `foreign_links` and `errors`, the items will just be appended. To handle the zero-or-one rule for `types`, we can get away with just one new matching rule, because there are three cases (no types, empty types, and fully specified types).
1 parent d1fac75 commit 42ebcb1

File tree

2 files changed

+94
-50
lines changed

2 files changed

+94
-50
lines changed

src/lib.rs

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -313,44 +313,6 @@ mod quick_error;
313313

314314
#[macro_export]
315315
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-
354316
(
355317
types {
356318
$error_name:ident, $error_kind_name:ident,
@@ -583,17 +545,52 @@ macro_rules! error_chain {
583545
pub type $result_name<T> = ::std::result::Result<T, $error_name>;
584546
};
585547

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
589555
(
556+
types {
557+
$error_name:ident, $error_kind_name:ident,
558+
$chain_error_name:ident, $result_name:ident;
559+
}
590560

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 ) *
596571
} ) *
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 { }
597594

598595
$( links {
599596
$( $link_error_path:path, $link_kind_path:path, $link_variant:ident; ) *
@@ -609,12 +606,39 @@ macro_rules! error_chain {
609606
) => (
610607
error_chain! {
611608
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; ) * ) *
616614
}
617615

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+
618642
links {
619643
$( $( $link_error_path, $link_kind_path, $link_variant; ) * ) *
620644
}

tests/tests.rs

100644100755
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ fn smoke_test_7() {
104104
}
105105
}
106106

107+
#[test]
108+
fn smoke_test_8() {
109+
error_chain! {
110+
types { }
111+
112+
links { }
113+
links { }
114+
115+
foreign_links { }
116+
foreign_links { }
117+
118+
errors {
119+
FileNotFound
120+
}
121+
errors {
122+
AccessDenied
123+
}
124+
}
125+
}
126+
107127
#[test]
108128
fn empty() {
109129
error_chain! { }

0 commit comments

Comments
 (0)