nom_fields/
lib.rs

1//! This crate provides a single function-like macro that
2//! removes some boilerplate from a common pattern when using
3//! the parsing framework [nom](https://www.crates.io/crates/nom).
4//!
5//! With this macro, the following parser...
6//! ```
7//! nom::combinator::map(
8//!     nom::sequence::tuple((
9//!         some_parser,
10//!         some_other_parser,
11//!         a_third_parser
12//!     )),
13//!     |(some_field, some_other_field, a_third_field)| SomeStruct {
14//!         some_field,
15//!         some_other_field,
16//!         a_third_field
17//!     }
18//! )(input)
19//! ```
20//! ...becomes this:
21//! ```
22//! nom_fields::fields!(SomeStruct:
23//!     some_field = some_parser,
24//!     some_other_field = some_other_parser,
25//!     a_third_field = a_third_parser
26//! )(input)
27//! ```
28
29/// The macro this crate provides. See the [top-level documentation](index.html) for details.
30#[macro_export]
31macro_rules! fields {
32    ($($struct_path:ident)::+ : $($field:ident = $parser:expr),+) => {
33        nom::combinator::map(
34            nom::sequence::tuple((
35                $($parser),+
36            )),
37            |($($field),+)| $($struct_path)::+ {
38                $($field),+
39            }
40        )
41    };
42}