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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use quote::ToTokens;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{bracketed, parenthesized, token, Error, Expr, Ident, Lit, LitStr, Path, Result, Token};

use crate::widgets::{Attr, Attrs};

impl Parse for Attrs {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let mut attrs = Vec::new();

        while input.peek(Token![#]) {
            let _sharp: Token![#] = input.parse()?;
            let attr_tokens;
            bracketed!(attr_tokens in input);
            let path: Path = attr_tokens.parse()?;

            // Name attribute
            attrs.push(if attr_tokens.is_empty() {
                if let Some(ident) = path.get_ident() {
                    if ident == "local" {
                        Attr::Local(ident.clone())
                    } else if ident == "local_ref" {
                        Attr::LocalRef(ident.clone())
                    } else if ident == "root" {
                        Attr::Root(ident.clone())
                    } else if ident == "watch" {
                        Attr::Watch(ident.clone())
                    } else if ident == "track" {
                        Attr::Track(ident.clone(), None)
                    } else if ident == "iterate" {
                        Attr::Iterate(ident.clone())
                    } else if ident == "template" {
                        Attr::Template(ident.clone())
                    } else if ident == "template_child" {
                        Attr::TemplateChild(ident.clone())
                    } else {
                        return Err(unexpected_attr_name(ident));
                    }
                } else {
                    return Err(Error::new(path.span(), "Expected identifier."));
                }

            // List attribute: `#[name(item1, item2)]
            } else if attr_tokens.peek(token::Paren) {
                let paren_input;
                parenthesized!(paren_input in attr_tokens);
                let nested: Punctuated<Expr, token::Comma> =
                    Punctuated::parse_terminated(&paren_input)?;

                if let Some(ident) = path.get_ident() {
                    if ident == "block_signal" {
                        let mut signal_idents = Vec::with_capacity(nested.len());
                        for expr in nested {
                            let ident = expect_ident_from_expr(&expr)?;
                            signal_idents.push(ident);
                        }
                        Attr::BlockSignal(ident.clone(), signal_idents)
                    } else if ident == "track" {
                        let expr = expect_one_nested_expr(&nested)?;
                        Attr::Track(ident.clone(), Some(Box::new(expr.clone())))
                    } else if ident == "transition" {
                        let expr = expect_one_nested_expr(&nested)?;
                        let ident = expect_ident_from_expr(expr)?;
                        Attr::Transition(ident.clone(), ident)
                    } else if ident == "name" {
                        let expr = expect_one_nested_expr(&nested)?;
                        let ident = expect_ident_from_expr(expr)?;
                        Attr::Name(ident.clone(), ident)
                    } else if ident == "wrap" {
                        let expr = expect_one_nested_expr(&nested)?;
                        let path = expect_path_from_expr(expr)?;
                        Attr::Wrap(ident.clone(), path)
                    } else if ident == "chain" {
                        let expr = expect_one_nested_expr(&nested)?;
                        Attr::Chain(ident.clone(), Box::new(expr.clone()))
                    } else {
                        return Err(unexpected_attr_name(ident));
                    }
                } else {
                    return Err(Error::new(path.span(), "Expected identifier."));
                }

            // Value attribute: `#[name = literal)]
            } else if attr_tokens.peek(Token![=]) {
                let _eq: Token![=] = attr_tokens.parse()?;
                let lit = attr_tokens.parse()?;

                if let Some(ident) = path.get_ident() {
                    if ident == "track" {
                        let string = expect_string_lit(&lit)?;
                        Attr::Track(ident.clone(), Some(string.parse()?))
                    } else if ident == "doc" {
                        Attr::Doc(lit.into_token_stream())
                    } else if ident == "transition" {
                        let string = expect_string_lit(&lit)?;
                        Attr::Transition(ident.clone(), string.parse()?)
                    } else if ident == "name" {
                        let string = expect_string_lit(&lit)?;
                        Attr::Name(ident.clone(), string.parse()?)
                    } else {
                        return Err(unexpected_attr_name(ident));
                    }
                } else {
                    return Err(Error::new(path.span(), "Expected identifier."));
                }
            } else {
                return Err(Error::new(attr_tokens.span(), "Expected `]`, `(` or `=`."));
            });
        }

        Ok(Attrs { inner: attrs })
    }
}

fn unexpected_attr_name(ident: &Ident) -> Error {
    Error::new(
        ident.span(),
        format!("Unexpected attribute name `{ident}`."),
    )
}

fn expect_string_lit(lit: &Lit) -> Result<&LitStr> {
    if let Lit::Str(string) = lit {
        Ok(string)
    } else {
        Err(Error::new(
            lit.span(),
            "Expected string literal. Try this: `\"value\"`.",
        ))
    }
}

fn expect_path_from_expr(expr: &Expr) -> Result<Path> {
    if let Expr::Path(path) = expr {
        Ok(path.path.clone())
    } else {
        Err(Error::new(
            expr.span(),
            format!("Expected path `{}`.", expr.to_token_stream()),
        ))
    }
}

fn expect_ident_from_expr(expr: &Expr) -> Result<Ident> {
    if let Expr::Path(path) = expr {
        expect_ident_from_path(&path.path)
    } else {
        Err(Error::new(
            expr.span(),
            format!("Expected identifier `{}`.", expr.to_token_stream()),
        ))
    }
}

fn expect_ident_from_path(path: &Path) -> Result<Ident> {
    if let Some(ident) = path.get_ident() {
        Ok(ident.clone())
    } else {
        Err(Error::new(path.span(), "Expected identifier."))
    }
}

fn expect_one_nested_expr(nested: &Punctuated<Expr, token::Comma>) -> Result<&Expr> {
    if nested.len() == 1 {
        Ok(nested.first().unwrap())
    } else {
        Err(Error::new(nested.span(), "Expected only one expression."))
    }
}