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

Commit f7110f8

Browse files
committed
More flexible declarations order.
Closes #42, thank you!
1 parent 043cca5 commit f7110f8

File tree

3 files changed

+143
-2
lines changed

3 files changed

+143
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Add ResultExt trait.
99
- Error.1 is a struct instead of a tuple.
1010
- Error is now a struct.
11+
- The declarations order is more flexible.
1112

1213
# 0.5.0
1314

src/error_chain.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[macro_export]
22
macro_rules! error_chain {
33
(
4+
$( @processed )*
45
types {
56
$error_name:ident, $error_kind_name:ident, $result_name:ident;
67
}
@@ -18,8 +19,6 @@ macro_rules! error_chain {
1819
}
1920

2021
) => {
21-
22-
2322
/// The Error type
2423
///
2524
/// This has a simple structure to support pattern matching
@@ -206,6 +205,7 @@ macro_rules! error_chain {
206205
//
207206
// Case 1: types fully specified
208207
(
208+
$( @processed )*
209209
types {
210210
$error_name:ident, $error_kind_name:ident, $result_name:ident;
211211
}
@@ -223,6 +223,7 @@ macro_rules! error_chain {
223223
} ) *
224224
) => (
225225
error_chain! {
226+
@processed
226227
types {
227228
$error_name, $error_kind_name, $result_name;
228229
}
@@ -242,6 +243,7 @@ macro_rules! error_chain {
242243
);
243244
// Case 2: types section present, but empty
244245
(
246+
$( @processed )*
245247
types { }
246248

247249
$( links {
@@ -257,6 +259,7 @@ macro_rules! error_chain {
257259
} ) *
258260
) => (
259261
error_chain! {
262+
@processed
260263
types {
261264
Error, ErrorKind, Result;
262265
}
@@ -276,6 +279,7 @@ macro_rules! error_chain {
276279
);
277280
// Case 3: types section not present
278281
(
282+
$( @processed )*
279283
$( links {
280284
$( $link_chunks:tt ) *
281285
} ) *
@@ -289,6 +293,7 @@ macro_rules! error_chain {
289293
} ) *
290294
) => (
291295
error_chain! {
296+
@processed
292297
types { }
293298

294299
links {
@@ -304,6 +309,71 @@ macro_rules! error_chain {
304309
}
305310
}
306311
);
312+
(
313+
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
314+
types $content:tt
315+
$( $tail:tt )*
316+
) => {
317+
error_chain! {
318+
@processing ($content, $b, $c, $d)
319+
$($tail)*
320+
}
321+
};
322+
(
323+
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
324+
links $content:tt
325+
$( $tail:tt )*
326+
) => {
327+
error_chain! {
328+
@processing ($a, $content, $c, $d)
329+
$($tail)*
330+
}
331+
};
332+
(
333+
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
334+
foreign_links $content:tt
335+
$( $tail:tt )*
336+
) => {
337+
error_chain! {
338+
@processing ($a, $b, $content, $d)
339+
$($tail)*
340+
}
341+
};
342+
(
343+
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
344+
errors $content:tt
345+
$( $tail:tt )*
346+
) => {
347+
error_chain! {
348+
@processing ($a, $b, $c, $content)
349+
$($tail)*
350+
}
351+
};
352+
(
353+
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
354+
) => {
355+
error_chain! {
356+
@processed
357+
types $a
358+
links $b
359+
foreign_links $c
360+
errors $d
361+
}
362+
};
363+
(
364+
@processed
365+
$( $block_name:ident $block_content:tt )*
366+
) => {
367+
!!!!!parse error!!!!!
368+
};
369+
(
370+
$( $block_name:ident $block_content:tt )*
371+
) => {
372+
error_chain! {
373+
@processing ({}, {}, {}, {})
374+
$($block_name $block_content)+
375+
}
376+
};
307377
}
308378

309379
/// Macro used to manage the `backtrace` feature.

tests/tests.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,76 @@ fn smoke_test_8() {
124124
};
125125
}
126126

127+
#[test]
128+
fn order_test_1() {
129+
error_chain! { types { } links { } foreign_links { } errors { } };
130+
}
131+
132+
#[test]
133+
fn order_test_2() {
134+
error_chain! { links { } types { } foreign_links { } errors { } };
135+
}
136+
137+
#[test]
138+
fn order_test_3() {
139+
error_chain! { foreign_links { } links { } errors { } types { } };
140+
}
141+
142+
#[test]
143+
fn order_test_4() {
144+
error_chain! { errors { } types { } foreign_links { } };
145+
}
146+
147+
#[test]
148+
fn order_test_5() {
149+
error_chain! { foreign_links { } types { } };
150+
}
151+
152+
#[test]
153+
fn order_test_6() {
154+
error_chain! {
155+
links { }
156+
157+
errors {
158+
HttpStatus(e: u32) {
159+
description("http request returned an unsuccessful status code")
160+
display("http request returned an unsuccessful status code: {}", e)
161+
}
162+
}
163+
164+
165+
foreign_links { }
166+
};
167+
}
168+
169+
#[test]
170+
fn order_test_7() {
171+
error_chain! {
172+
links { }
173+
174+
foreign_links { }
175+
176+
types {
177+
Error, ErrorKind, Result;
178+
}
179+
};
180+
}
181+
182+
183+
#[test]
184+
fn order_test_8() {
185+
error_chain! {
186+
links { }
187+
188+
foreign_links { }
189+
foreign_links { }
190+
191+
types {
192+
Error, ErrorKind, Result;
193+
}
194+
};
195+
}
196+
127197
#[test]
128198
fn empty() {
129199
error_chain! { };

0 commit comments

Comments
 (0)