macro_rules! struct_parser {
    ($name: ident { $($tt: tt)* }) => { ... };
    ($name: ident ( $($arg: tt)* )) => { ... };
}
Expand description

Sequences multiple parsers and builds a struct out of them.

use combine::{Parser, between, from_str, many, struct_parser, token};
use combine::parser::range::take_while1;
use combine::parser::byte::{letter, spaces};

#[derive(Debug, PartialEq)]
struct Point(u32, u32);

#[derive(Debug, PartialEq)]
struct Field {
    name: Vec<u8>,
    value: Vec<u8>,
    point: Point,
}
fn main() {
    let num = || from_str(take_while1(|b: u8| b >= b'0' && b <= b'9'));
    let spaced = |b| between(spaces(), spaces(), token(b));
    let mut parser = struct_parser!{
        Field {
            name: many(letter()),
            // `_` fields are ignored when building the struct
            _: spaced(b':'),
            value: many(letter()),
            _: spaced(b':'),
            point: struct_parser!(Point(num(), _: spaced(b','), num())),
        }
    };
    assert_eq!(
        parser.parse(&b"test: data: 123 , 4"[..]),
        Ok((
            Field {
                name: b"test"[..].to_owned(),
                value: b"data"[..].to_owned(),
                point: Point(123, 4),
            },
            &b""[..]
        )),
    );
}