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

/// The macro this crate provides. See the [top-level documentation](index.html) for details.
#[macro_export]
macro_rules! fields {
    ($($struct_path:ident)::+ : $($field:ident = $parser:expr),+) => {
        nom::combinator::map(
            nom::sequence::tuple((
                $($parser),+
            )),
            |($($field),+)| $($struct_path)::+ {
                $($field),+
            }
        )
    };
}