aboutsummaryrefslogtreecommitdiffstats
path: root/crates/mk_config/src/parse.rs
blob: 4308846dc5d27d4feb18b6d1a0e8823893b67437 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2026 Soispha Peetz <soispha@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use syn::parse::{Parse, ParseStream, Result};
use syn::token::Brace;
use syn::{Attribute, Expr, Ident, Token, Type, braced, bracketed};

#[derive(Debug)]
pub(crate) struct ParseBase {
    pub(crate) name: Ident,
    pub(crate) fields: Vec<ParsePart>,
    pub(crate) imports: Vec<syn::Path>,
}

#[derive(Clone, Debug)]
#[expect(clippy::large_enum_variant)]
pub(crate) enum ParsePart {
    Section {
        name: Ident,
        fields: Vec<ParsePart>,
        documentation: Vec<Attribute>,
    },
    Field {
        name: Ident,
        ty: Type,
        default: Option<FieldDefault>,
        documentation: Vec<Attribute>,
        finalizer: Option<Ident>,
    },
}

#[derive(Clone, Debug)]
pub(crate) struct FieldDefault {
    pub(crate) default: Expr,
    pub(crate) may_error: bool,
}

impl Parse for FieldDefault {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let lookahead = input.lookahead1();
        let (may_error, expr) = if lookahead.peek(Token![try]) {
            input.parse::<Token![try]>()?;
            (true, input.parse()?)
        } else {
            (false, input.parse()?)
        };

        Ok(Self {
            default: expr,
            may_error,
        })
    }
}

impl Parse for ParsePart {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let documentation = input.call(Attribute::parse_outer)?;

        let name = input.parse()?;
        input.parse::<Token![:]>()?;

        let lookahead = input.lookahead1();
        if lookahead.peek(Brace) {
            let content;
            braced!(content in input);
            let fields = content
                .parse_terminated(ParsePart::parse, Token![,])?
                .into_iter()
                .collect();

            Ok(Self::Section {
                name,
                fields,
                documentation,
            })
        } else {
            let ty = input.parse()?;

            let lookahead = input.lookahead1();
            let mut default = None;
            let mut finalizer = None;

            if lookahead.peek(Token![=>]) {
                input.parse::<Token![=>]>()?;
                finalizer = Some(input.parse::<Ident>()?);

                let lookahead = input.lookahead1();
                if !lookahead.peek(Token![,]) {
                    return Err(lookahead.error());
                }
            } else if lookahead.peek(Token![=]) {
                input.parse::<Token![=]>()?;
                default = Some(input.parse()?);

                let lookahead = input.lookahead1();
                if lookahead.peek(Token![=>]) {
                    input.parse::<Token![=>]>()?;
                    finalizer = Some(input.parse::<Ident>()?);

                    let lookahead = input.lookahead1();
                    if !lookahead.peek(Token![,]) {
                        return Err(lookahead.error());
                    }
                }
            } else if lookahead.peek(Token![,]) {
                // leave the `,` so that the `parse_terminated` can remove it
            } else {
                return Err(lookahead.error());
            }

            Ok(Self::Field {
                name,
                ty,
                default,
                documentation,
                finalizer,
            })
        }
    }
}

impl Parse for ParseBase {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        input.parse::<Token![use]>()?;
        let content;
        bracketed!(content in input);
        let imports = content
            .parse_terminated(syn::Path::parse, Token![;])?
            .into_iter()
            .collect();

        input.parse::<Token![struct]>()?;
        let name: Ident = input.parse()?;

        let content;
        braced!(content in input);
        let fields = content
            .parse_terminated(ParsePart::parse, Token![,])?
            .into_iter()
            .collect();

        Ok(ParseBase {
            name,
            fields,
            imports,
        })
    }
}