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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Take a look at the license at the top of the repository in the LICENSE file.

use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{ext::IdentExt, spanned::Spanned, Token};

use crate::utils::crate_ident_new;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum CaptureKind {
    Watch,
    WeakAllowNone,
    Strong,
    ToOwned,
}

struct Capture {
    name: TokenStream,
    alias: Option<syn::Ident>,
    kind: CaptureKind,
    start: Span,
}

impl Capture {
    fn alias(&self) -> TokenStream {
        if let Some(ref a) = self.alias {
            a.to_token_stream()
        } else {
            self.name.to_token_stream()
        }
    }
    fn outer_before_tokens(&self, crate_ident: &TokenStream) -> TokenStream {
        let alias = self.alias();
        let name = &self.name;
        match self.kind {
            CaptureKind::Watch => quote! {
                let #alias = #crate_ident::object::Watchable::watched_object(&#name);
            },
            CaptureKind::WeakAllowNone => quote! {
                let #alias = #crate_ident::clone::Downgrade::downgrade(&#name);
            },
            CaptureKind::Strong => quote! {
                let #alias = #name.clone();
            },
            CaptureKind::ToOwned => quote! {
                let #alias = ::std::borrow::ToOwned::to_owned(&*#name);
            },
        }
    }

    fn outer_after_tokens(&self, crate_ident: &TokenStream, closure_ident: &Ident) -> TokenStream {
        let name = &self.name;
        match self.kind {
            CaptureKind::Watch => quote! {
                #crate_ident::object::Watchable::watch_closure(&#name, &#closure_ident);
            },
            _ => Default::default(),
        }
    }

    fn inner_before_tokens(&self, crate_ident: &TokenStream) -> TokenStream {
        let alias = self.alias();
        match self.kind {
            CaptureKind::Watch => {
                quote! {
                    let #alias = unsafe { #alias.borrow() };
                    let #alias = ::core::convert::AsRef::as_ref(&#alias);
                }
            }
            CaptureKind::WeakAllowNone => quote! {
                let #alias = #crate_ident::clone::Upgrade::upgrade(&#alias);
            },
            _ => Default::default(),
        }
    }
}

impl syn::parse::Parse for CaptureKind {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        input.parse::<Token![@]>()?;
        let mut idents = TokenStream::new();
        idents.append(input.call(syn::Ident::parse_any)?);
        while input.peek(Token![-]) {
            input.parse::<Token![-]>()?;
            idents.append(input.call(syn::Ident::parse_any)?);
        }
        let keyword = idents
            .clone()
            .into_iter()
            .map(|i| i.to_string())
            .collect::<Vec<_>>()
            .join("-");
        match keyword.as_str() {
            "strong" => Ok(CaptureKind::Strong),
            "watch" => Ok(CaptureKind::Watch),
            "weak-allow-none" => Ok(CaptureKind::WeakAllowNone),
            "to-owned" => Ok(CaptureKind::ToOwned),
            k => Err(syn::Error::new(
                idents.span(),
                format!("Unknown keyword `{}`, only `watch`, `weak-allow-none`, `to-owned` and `strong` are allowed",
                k),
            )),
        }
    }
}

impl syn::parse::Parse for Capture {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let start = input.span();
        let kind = input.parse()?;
        let mut name = TokenStream::new();
        name.append(input.call(syn::Ident::parse_any)?);
        while input.peek(Token![.]) {
            input.parse::<Token![.]>()?;
            name.append(proc_macro2::Punct::new('.', proc_macro2::Spacing::Alone));
            name.append(input.call(syn::Ident::parse_any)?);
        }
        let alias = if input.peek(Token![as]) {
            input.parse::<Token![as]>()?;
            input.parse()?
        } else {
            None
        };
        if alias.is_none() {
            if name.to_string() == "self" {
                return Err(syn::Error::new_spanned(
                    name,
                    "Can't use `self` as variable name. Try storing it in a temporary variable or \
                    rename it using `as`.",
                ));
            }
            if name.to_string().contains('.') {
                return Err(syn::Error::new(
                    name.span(),
                    format!(
                        "`{}`: Field accesses are not allowed as is, you must rename it!",
                        name
                    ),
                ));
            }
        }
        Ok(Capture {
            name,
            alias,
            kind,
            start,
        })
    }
}

struct Closure {
    captures: Vec<Capture>,
    args: Vec<Ident>,
    closure: syn::ExprClosure,
    constructor: &'static str,
}

impl syn::parse::Parse for Closure {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let mut captures: Vec<Capture> = vec![];
        if input.peek(Token![@]) {
            loop {
                let capture = input.parse::<Capture>()?;
                if capture.kind == CaptureKind::Watch {
                    if let Some(existing) = captures.iter().find(|c| c.kind == CaptureKind::Watch) {
                        return Err(syn::Error::new(
                            existing.start,
                            "Only one `@watch` capture is allowed per closure",
                        ));
                    }
                }
                captures.push(capture);
                if input.peek(Token![,]) {
                    input.parse::<Token![,]>()?;
                    if !input.peek(Token![@]) {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
        if !captures.is_empty() {
            input.parse::<Token![=>]>()?;
        }
        let mut closure = input.parse::<syn::ExprClosure>()?;
        if closure.asyncness.is_some() {
            return Err(syn::Error::new_spanned(
                closure,
                "Async closure not allowed",
            ));
        }
        if !captures.is_empty() && closure.capture.is_none() {
            return Err(syn::Error::new_spanned(
                closure,
                "Closure with captures needs to be \"moved\" so please add `move` before closure",
            ));
        }
        let args = closure
            .inputs
            .iter()
            .enumerate()
            .map(|(i, _)| Ident::new(&format!("____value{i}"), Span::call_site()))
            .collect();
        closure.capture = None;
        Ok(Closure {
            captures,
            args,
            closure,
            constructor: "new",
        })
    }
}

impl ToTokens for Closure {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let closure_ident = Ident::new("____closure", Span::call_site());
        let values_ident = Ident::new("____values", Span::call_site());
        let crate_ident = crate_ident_new();

        let outer_before = self
            .captures
            .iter()
            .map(|c| c.outer_before_tokens(&crate_ident));
        let inner_before = self
            .captures
            .iter()
            .map(|c| c.inner_before_tokens(&crate_ident));
        let outer_after = self
            .captures
            .iter()
            .map(|c| c.outer_after_tokens(&crate_ident, &closure_ident));

        let arg_values = self.args.iter().enumerate().map(|(index, arg)| {
            let err_msg = format!("Wrong type for argument {index}: {{:?}}");
            quote! {
                let #arg = ::core::result::Result::unwrap_or_else(
                    #crate_ident::Value::get(&#values_ident[#index]),
                    |e| panic!(#err_msg, e),
                );
            }
        });
        let arg_names = &self.args;
        let args_len = self.args.len();
        let closure = &self.closure;
        let constructor = Ident::new(self.constructor, Span::call_site());

        tokens.extend(quote! {
            {
                let #closure_ident = {
                    #(#outer_before)*
                    #crate_ident::closure::RustClosure::#constructor(move |#values_ident| {
                        assert_eq!(
                            #values_ident.len(),
                            #args_len,
                            "Expected {} arguments but got {}",
                            #args_len,
                            #values_ident.len(),
                        );
                        #(#inner_before)*
                        #(#arg_values)*
                        #crate_ident::closure::IntoClosureReturnValue::into_closure_return_value(
                            (#closure)(#(#arg_names),*)
                        )
                    })
                };
                #(#outer_after)*
                #closure_ident
            }
        });
    }
}

pub(crate) fn closure_inner(
    input: proc_macro::TokenStream,
    constructor: &'static str,
) -> proc_macro::TokenStream {
    let mut closure = syn::parse_macro_input!(input as Closure);
    closure.constructor = constructor;
    closure.into_token_stream().into()
}