pilota_thrift_parser/parser/
function.rs

1use nom::{
2    IResult,
3    bytes::complete::tag,
4    combinator::{map, opt},
5    multi::many1,
6    sequence::tuple,
7};
8
9use super::super::{
10    Attribute,
11    descriptor::{Annotations, Field, Function, Ident, Type},
12    parser::{Parser, blank, list_separator},
13};
14
15impl Parser for Function {
16    fn parse(input: &str) -> IResult<&str, Function> {
17        map(
18            tuple((
19                map(opt(tuple((tag("oneway"), blank))), |x| x.is_some()),
20                Type::parse,
21                blank,
22                Ident::parse,
23                opt(blank),
24                tag("("),
25                opt(many1(map(
26                    tuple((opt(blank), Field::parse)),
27                    |(_, field)| field,
28                ))),
29                opt(blank),
30                tag(")"),
31                opt(blank),
32                opt(map(
33                    tuple((
34                        tag("throws"),
35                        opt(blank),
36                        tag("("),
37                        many1(map(tuple((opt(blank), Field::parse)), |(_, field)| field)),
38                        opt(blank),
39                        tag(")"),
40                    )),
41                    |(_, _, _, fields, _, _)| fields,
42                )),
43                opt(blank),
44                opt(Annotations::parse),
45                opt(list_separator),
46            )),
47            |(oneway, r#type, _, name, _, _, arguments, _, _, _, throws, _, annotations, _)| {
48                let mut args = arguments.unwrap_or_default();
49                args.iter_mut().for_each(|f| {
50                    if f.attribute == Attribute::Default {
51                        f.attribute = Attribute::Required
52                    }
53                });
54                Function {
55                    name,
56                    oneway,
57                    result_type: r#type,
58                    arguments: args,
59                    throws: throws.unwrap_or_default(),
60                    annotations: annotations.unwrap_or_default(),
61                }
62            },
63        )(input)
64    }
65}