Skip to main content

px_wsdom_ts_parse/parser/
declare_function.rs

1use winnow::{
2    combinator::{delimited, opt, separated0, separated_pair},
3    Parser,
4};
5
6use super::{
7    generic::GenericsDeclaration,
8    method::MethodArg,
9    ts_type::TsType,
10    util::{token, token_word, word1, Parsable},
11};
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct DeclareFunction<'a> {
15    pub name: &'a str,
16    pub generics: GenericsDeclaration<'a>,
17    pub args: Vec<MethodArg<'a>>,
18    pub ret: TsType<'a>,
19}
20
21impl<'a> Parsable<'a> for DeclareFunction<'a> {
22    fn parse(input: &mut &'a str) -> winnow::PResult<Self> {
23        delimited(
24            (opt(token_word("declare")), token_word("function")),
25            (
26                word1,
27                opt(GenericsDeclaration::parse),
28                separated_pair(
29                    delimited(
30                        token('('),
31                        separated0(MethodArg::parse, token(',')),
32                        token(')'),
33                    ),
34                    token(':'),
35                    TsType::parse,
36                ),
37            ),
38            token(';'),
39        )
40        .parse_next(input)
41        .map(|(name, generics, (args, ret))| Self {
42            name,
43            generics: generics.unwrap_or_default(),
44            args,
45            ret,
46        })
47    }
48}