Macro parser_parsable_enum

Source
macro_rules! parser_parsable_enum {
    (
        $(#[$enum_meta:meta])*
        enum $name:ident {$(
            $(#[$meta:meta])*
            $($l:literal)|+ => $variant:ident $(= $value:expr)?,
        )+}
    ) => { ... };
}
Expand description

Macro to define an enum that implements Parseable.

The parser is implemented using parser::literal_map!.

ยงExamples

parser::parsable_enum! {
    #[derive(Debug, PartialEq, Default)]
    enum Direction {
        #[default]
        "north" | "n" => North,
        "south" | "s" => South,
        "east" | "e" => East,
        "west" | "w" => West,
    }
}

assert_eq!(Direction::PARSER.parse(b"north"), Ok((Direction::North, &b""[..])));
assert_eq!(Direction::PARSER.parse(b"s"), Ok((Direction::South, &b""[..])));
assert!(Direction::PARSER.parse(b"a").is_err());