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

Commit 27c6003

Browse files
committed
Simplification of the main macro.
1 parent ddba232 commit 27c6003

File tree

2 files changed

+27
-120
lines changed

2 files changed

+27
-120
lines changed

src/error_chain.rs

Lines changed: 24 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
#[macro_export]
22
macro_rules! error_chain {
33
(
4-
$( @processed )*
4+
@processed
5+
types {}
6+
links $b:tt
7+
foreign_links $c:tt
8+
errors $d:tt
9+
) => {
10+
error_chain! {
11+
@processed
12+
types {
13+
Error, ErrorKind, Result;
14+
}
15+
links $b
16+
foreign_links $c
17+
errors $d
18+
}
19+
};
20+
(
21+
@processed
522
types {
623
$error_name:ident, $error_kind_name:ident, $result_name:ident;
724
}
@@ -196,119 +213,12 @@ macro_rules! error_chain {
196213
/// Convenient wrapper around `std::Result`.
197214
pub type $result_name<T> = ::std::result::Result<T, $error_name>;
198215
};
199-
200-
// Handle missing sections, or missing type names in types { }
201-
//
202-
// Macros cannot specify "zero or one repetitions" at the moment, so we allow
203-
// repeating sections. Only for the `types` section this makes no sense, which
204-
// is the reason for the three separate cases.
205-
//
206-
// Case 1: types fully specified
207-
(
208-
$( @processed )*
209-
types {
210-
$error_name:ident, $error_kind_name:ident, $result_name:ident;
211-
}
212-
213-
$( links {
214-
$( $link_chunks:tt ) *
215-
} ) *
216-
217-
$( foreign_links {
218-
$( $foreign_link_chunks:tt ) *
219-
} ) *
220-
221-
$( errors {
222-
$( $error_chunks:tt ) *
223-
} ) *
224-
) => (
225-
error_chain! {
226-
@processed
227-
types {
228-
$error_name, $error_kind_name, $result_name;
229-
}
230-
231-
links {
232-
$( $( $link_chunks ) * ) *
233-
}
234-
235-
foreign_links {
236-
$( $( $foreign_link_chunks ) * ) *
237-
}
238-
239-
errors {
240-
$( $( $error_chunks ) * ) *
241-
}
242-
}
243-
);
244-
// Case 2: types section present, but empty
245-
(
246-
$( @processed )*
247-
types { }
248-
249-
$( links {
250-
$( $link_chunks:tt ) *
251-
} ) *
252-
253-
$( foreign_links {
254-
$( $foreign_link_chunks:tt ) *
255-
} ) *
256-
257-
$( errors {
258-
$( $error_chunks:tt ) *
259-
} ) *
260-
) => (
261-
error_chain! {
262-
@processed
263-
types {
264-
Error, ErrorKind, Result;
265-
}
266-
267-
links {
268-
$( $( $link_chunks ) * ) *
269-
}
270-
271-
foreign_links {
272-
$( $( $foreign_link_chunks ) * ) *
273-
}
274-
275-
errors {
276-
$( $( $error_chunks ) * ) *
277-
}
278-
}
279-
);
280-
// Case 3: types section not present
281216
(
282-
$( @processed )*
283-
$( links {
284-
$( $link_chunks:tt ) *
285-
} ) *
286-
287-
$( foreign_links {
288-
$( $foreign_link_chunks:tt ) *
289-
} ) *
290-
291-
$( errors {
292-
$( $error_chunks:tt ) *
293-
} ) *
294-
) => (
295-
error_chain! {
296-
@processed
297-
types { }
298-
299-
links {
300-
$( $( $link_chunks ) * ) *
301-
}
302-
303-
foreign_links {
304-
$( $( $foreign_link_chunks ) * ) *
305-
}
306-
307-
errors {
308-
$( $( $error_chunks ) * ) *
309-
}
310-
}
311-
);
217+
@processed
218+
$( $block_name:ident $block_content:tt )*
219+
) => {
220+
!!!!!parse error!!!!!
221+
};
312222
(
313223
@processing ($a:tt, $b:tt, $c:tt, $d:tt)
314224
types $content:tt
@@ -360,18 +270,12 @@ macro_rules! error_chain {
360270
errors $d
361271
}
362272
};
363-
(
364-
@processed
365-
$( $block_name:ident $block_content:tt )*
366-
) => {
367-
!!!!!parse error!!!!!
368-
};
369273
(
370274
$( $block_name:ident $block_content:tt )*
371275
) => {
372276
error_chain! {
373277
@processing ({}, {}, {}, {})
374-
$($block_name $block_content)+
278+
$($block_name $block_content)*
375279
}
376280
};
377281
}

tests/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#![allow(dead_code)]
2+
//#![feature(trace_macros)]
3+
//
4+
//trace_macros!(true);
25

36
#[macro_use]
47
extern crate error_chain;

0 commit comments

Comments
 (0)