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

Commit c8b409b

Browse files
committed
Better error reporting in the macro call.
1 parent 27c6003 commit c8b409b

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Error.1 is a struct instead of a tuple.
1010
- Error is now a struct.
1111
- The declarations order is more flexible.
12+
- Way better error reporting when there is a syntax error in the macro call.
1213

1314
# 0.5.0
1415

src/error_chain.rs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
#[macro_export]
2-
macro_rules! error_chain {
2+
macro_rules! error_chain_processed {
3+
// Default values for `types`.
34
(
4-
@processed
55
types {}
6-
links $b:tt
7-
foreign_links $c:tt
8-
errors $d:tt
6+
$( $rest: tt )*
97
) => {
10-
error_chain! {
11-
@processed
8+
error_chain_processed! {
129
types {
1310
Error, ErrorKind, Result;
1411
}
15-
links $b
16-
foreign_links $c
17-
errors $d
12+
$( $rest )*
1813
}
1914
};
2015
(
21-
@processed
2216
types {
2317
$error_name:ident, $error_kind_name:ident, $result_name:ident;
2418
}
@@ -213,69 +207,70 @@ macro_rules! error_chain {
213207
/// Convenient wrapper around `std::Result`.
214208
pub type $result_name<T> = ::std::result::Result<T, $error_name>;
215209
};
210+
}
211+
212+
/// Internal macro used for reordering of the fields.
213+
#[doc(hidden)]
214+
#[macro_export]
215+
macro_rules! error_chain_processing {
216216
(
217-
@processed
218-
$( $block_name:ident $block_content:tt )*
219-
) => {
220-
!!!!!parse error!!!!!
221-
};
222-
(
223-
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
217+
($a:tt, $b:tt, $c:tt, $d:tt)
224218
types $content:tt
225219
$( $tail:tt )*
226220
) => {
227-
error_chain! {
228-
@processing ($content, $b, $c, $d)
221+
error_chain_processing! {
222+
($content, $b, $c, $d)
229223
$($tail)*
230224
}
231225
};
232226
(
233-
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
227+
($a:tt, $b:tt, $c:tt, $d:tt)
234228
links $content:tt
235229
$( $tail:tt )*
236230
) => {
237-
error_chain! {
238-
@processing ($a, $content, $c, $d)
231+
error_chain_processing! {
232+
($a, $content, $c, $d)
239233
$($tail)*
240234
}
241235
};
242236
(
243-
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
237+
($a:tt, $b:tt, $c:tt, $d:tt)
244238
foreign_links $content:tt
245239
$( $tail:tt )*
246240
) => {
247-
error_chain! {
248-
@processing ($a, $b, $content, $d)
241+
error_chain_processing! {
242+
($a, $b, $content, $d)
249243
$($tail)*
250244
}
251245
};
252246
(
253-
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
247+
($a:tt, $b:tt, $c:tt, $d:tt)
254248
errors $content:tt
255249
$( $tail:tt )*
256250
) => {
257-
error_chain! {
258-
@processing ($a, $b, $c, $content)
251+
error_chain_processing! {
252+
($a, $b, $c, $content)
259253
$($tail)*
260254
}
261255
};
262-
(
263-
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
264-
) => {
265-
error_chain! {
266-
@processed
256+
( ($a:tt, $b:tt, $c:tt, $d:tt) ) => {
257+
error_chain_processed! {
267258
types $a
268259
links $b
269260
foreign_links $c
270261
errors $d
271262
}
272263
};
273-
(
274-
$( $block_name:ident $block_content:tt )*
275-
) => {
276-
error_chain! {
277-
@processing ({}, {}, {}, {})
278-
$($block_name $block_content)*
264+
}
265+
266+
/// This macro is used for handling of duplicated and out-of-order fields. For
267+
/// the exact rules, see `error_chain_processed`.
268+
#[macro_export]
269+
macro_rules! error_chain {
270+
( $( $block_name:ident { $( $block_content:tt )* } )* ) => {
271+
error_chain_processing! {
272+
({}, {}, {}, {})
273+
$($block_name { $( $block_content )* })*
279274
}
280275
};
281276
}

0 commit comments

Comments
 (0)