macro_rules! struct_parser {
[
$struct:ident $( :: $struct_x:ident )* {
$( $field:ident : $parser:expr ),+
$(,)?
}
] => { ... };
[
$struct:ident $( :: $struct_x:ident )* (
$( $parser:expr ),+
$(,)?
)
] => { ... };
}
Expand description
Parse struct fields in order using an initializer-like syntax
ยงExample
type Parser<'a, T> = fn(&'a [u8]) -> IResult<&'a [u8], T, ()>;
struct Foo<'a> { a: u16, b: &'a [u8] };
// Will parse `a`, then `b`, and return the struct if both succeed
let parse_foo: Parser<Foo> = struct_parser!(
Foo {
a: le_u16,
b: alphanumeric1,
}
);
assert_eq!(
parse_foo(b"\x34\x12Bar10"),
Ok((b"" as &[u8], Foo { a: 0x1234, b: b"Bar10" }))
);