pilota_thrift_parser/parser/
enum_.rs

1use nom::{
2    IResult,
3    bytes::complete::tag,
4    combinator::{map, opt},
5    sequence::tuple,
6};
7
8use super::super::{
9    descriptor::{Annotations, Enum, EnumValue, Ident, IntConstant},
10    parser::*,
11};
12
13impl Parser for EnumValue {
14    fn parse(input: &str) -> IResult<&str, EnumValue> {
15        map(
16            tuple((
17                Ident::parse,
18                opt(blank),
19                opt(map(
20                    tuple((tag("="), opt(blank), IntConstant::parse)),
21                    |(_, _, value)| value,
22                )),
23                opt(blank),
24                opt(Annotations::parse),
25                opt(list_separator),
26                opt(blank),
27            )),
28            |(name, _, value, _, annotations, _, _)| EnumValue {
29                name,
30                value,
31                annotations: annotations.unwrap_or_default(),
32            },
33        )(input)
34    }
35}
36
37impl Parser for Enum {
38    fn parse(input: &str) -> IResult<&str, Enum> {
39        map(
40            tuple((
41                tag("enum"),
42                blank,
43                Ident::parse,
44                opt(blank),
45                tag("{"),
46                opt(blank),
47                many0(EnumValue::parse),
48                opt(blank),
49                tag("}"),
50                opt(blank),
51                opt(Annotations::parse),
52            )),
53            |(_, _, name, _, _, _, values, _, _, _, annotations)| Enum {
54                name,
55                values,
56                annotations: annotations.unwrap_or_default(),
57            },
58        )(input)
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    #[test]
66    fn test_enum() {
67        let (_remain, _e) = Enum::parse(
68            r#"enum Sex {
69                UNKNOWN = 0,
70                MALE = 1 (pilota.key="male") // male
71                FEMALE = 2,
72            }"#,
73        )
74        .unwrap();
75    }
76}