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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
extern crate proc_macro;

use std::collections::HashSet;

use proc_macro::TokenStream;
use quote::{
    format_ident,
    quote,
};
use syn::{
    parse_macro_input,
    ItemImpl,
    Type,
    TypePath,
    TypeTuple,
};

/// A helper attribute for deriving `State` for a struct.
#[proc_macro_attribute]
pub fn partial_derive_state(_: TokenStream, input: TokenStream) -> TokenStream {
    let impl_block: syn::ItemImpl = parse_macro_input!(input as syn::ItemImpl);

    let has_create_fn = impl_block
        .items
        .iter()
        .any(|item| matches!(item, syn::ImplItem::Fn(method) if method.sig.ident == "create"));

    let parent_dependencies = impl_block
        .items
        .iter()
        .find_map(|item| {
            if let syn::ImplItem::Type(syn::ImplItemType { ident, ty, .. }) = item {
                (ident == "ParentDependencies").then_some(ty)
            } else {
                None
            }
        })
        .expect("ParentDependencies must be defined");
    let child_dependencies = impl_block
        .items
        .iter()
        .find_map(|item| {
            if let syn::ImplItem::Type(syn::ImplItemType { ident, ty, .. }) = item {
                (ident == "ChildDependencies").then_some(ty)
            } else {
                None
            }
        })
        .expect("ChildDependencies must be defined");
    let node_dependencies = impl_block
        .items
        .iter()
        .find_map(|item| {
            if let syn::ImplItem::Type(syn::ImplItemType { ident, ty, .. }) = item {
                (ident == "NodeDependencies").then_some(ty)
            } else {
                None
            }
        })
        .expect("NodeDependencies must be defined");

    let this_type = &impl_block.self_ty;
    let this_type = extract_type_path(this_type)
        .unwrap_or_else(|| panic!("Self must be a type path, found {}", quote!(#this_type)));

    let mut combined_dependencies = HashSet::new();

    let self_path: TypePath = syn::parse_quote!(Self);

    let parent_dependencies = match extract_tuple(parent_dependencies) {
        Some(tuple) => {
            let mut parent_dependencies = Vec::new();
            for type_ in &tuple.elems {
                let mut type_ = extract_type_path(type_).unwrap_or_else(|| {
                    panic!(
                        "ParentDependencies must be a tuple of type paths, found {}",
                        quote!(#type_)
                    )
                });
                if type_ == self_path {
                    type_ = this_type.clone();
                }
                combined_dependencies.insert(type_.clone());
                parent_dependencies.push(type_);
            }
            parent_dependencies
        }
        _ => panic!(
            "ParentDependencies must be a tuple, found {}",
            quote!(#parent_dependencies)
        ),
    };
    let child_dependencies = match extract_tuple(child_dependencies) {
        Some(tuple) => {
            let mut child_dependencies = Vec::new();
            for type_ in &tuple.elems {
                let mut type_ = extract_type_path(type_).unwrap_or_else(|| {
                    panic!(
                        "ChildDependencies must be a tuple of type paths, found {}",
                        quote!(#type_)
                    )
                });
                if type_ == self_path {
                    type_ = this_type.clone();
                }
                combined_dependencies.insert(type_.clone());
                child_dependencies.push(type_);
            }
            child_dependencies
        }
        _ => panic!(
            "ChildDependencies must be a tuple, found {}",
            quote!(#child_dependencies)
        ),
    };
    let node_dependencies = match extract_tuple(node_dependencies) {
        Some(tuple) => {
            let mut node_dependencies = Vec::new();
            for type_ in &tuple.elems {
                let mut type_ = extract_type_path(type_).unwrap_or_else(|| {
                    panic!(
                        "NodeDependencies must be a tuple of type paths, found {}",
                        quote!(#type_)
                    )
                });
                if type_ == self_path {
                    type_ = this_type.clone();
                }
                combined_dependencies.insert(type_.clone());
                node_dependencies.push(type_);
            }
            node_dependencies
        }
        _ => panic!(
            "NodeDependencies must be a tuple, found {}",
            quote!(#node_dependencies)
        ),
    };
    combined_dependencies.insert(this_type.clone());

    let combined_dependencies: Vec<_> = combined_dependencies.into_iter().collect();
    let parent_dependancies_idxes: Vec<_> = parent_dependencies
        .iter()
        .filter_map(|ident| combined_dependencies.iter().position(|i| i == ident))
        .collect();
    let child_dependencies_idxes: Vec<_> = child_dependencies
        .iter()
        .filter_map(|ident| combined_dependencies.iter().position(|i| i == ident))
        .collect();
    let node_dependencies_idxes: Vec<_> = node_dependencies
        .iter()
        .filter_map(|ident| combined_dependencies.iter().position(|i| i == ident))
        .collect();
    let this_type_idx = combined_dependencies
        .iter()
        .enumerate()
        .find_map(|(i, ident)| (this_type == *ident).then_some(i))
        .unwrap();
    let this_view = format_ident!("__data{}", this_type_idx);

    let combined_dependencies_quote = combined_dependencies.iter().map(|ident| {
        if ident == &this_type {
            quote! {shipyard::ViewMut<#ident>}
        } else {
            quote! {shipyard::View<#ident>}
        }
    });
    let combined_dependencies_quote = quote!((#(#combined_dependencies_quote,)*));

    let ItemImpl {
        attrs,
        defaultness,
        unsafety,
        impl_token,
        generics,
        trait_,
        self_ty,
        items,
        ..
    } = impl_block;
    let for_ = trait_.as_ref().map(|t| t.2);
    let trait_ = trait_.map(|t| t.1);

    let split_views: Vec<_> = (0..combined_dependencies.len())
        .map(|i| {
            let ident = format_ident!("__data{}", i);
            if i == this_type_idx {
                quote! {mut #ident}
            } else {
                quote! {#ident}
            }
        })
        .collect();

    let node_view = node_dependencies_idxes
        .iter()
        .map(|i| format_ident!("__data{}", i))
        .collect::<Vec<_>>();
    let get_node_view = {
        if node_dependencies.is_empty() {
            quote! {
                let raw_node = ();
            }
        } else {
            let temps = (0..node_dependencies.len())
                .map(|i| format_ident!("__temp{}", i))
                .collect::<Vec<_>>();
            quote! {
                let raw_node: (#(*const #node_dependencies,)*) = {
                    let (#(#temps,)*) = (#(&#node_view,)*).get(id).unwrap_or_else(|err| panic!("Failed to get node view {:?}", err));
                    (#(#temps as *const _,)*)
                };
            }
        }
    };
    let deref_node_view = {
        if node_dependencies.is_empty() {
            quote! {
                let node = raw_node;
            }
        } else {
            let indexes = (0..node_dependencies.len()).map(syn::Index::from);
            quote! {
                let node = unsafe { (#(freya_native_core::prelude::DependancyView::new(&*raw_node.#indexes),)*) };
            }
        }
    };

    let parent_view = parent_dependancies_idxes
        .iter()
        .map(|i| format_ident!("__data{}", i))
        .collect::<Vec<_>>();
    let get_parent_view = {
        if parent_dependencies.is_empty() {
            quote! {
                let raw_parent = tree.parent_id_advanced(id, Self::TRAVERSE_SHADOW_DOM).map(|_| ());
            }
        } else {
            let temps = (0..parent_dependencies.len())
                .map(|i| format_ident!("__temp{}", i))
                .collect::<Vec<_>>();
            quote! {
                let raw_parent = tree.parent_id_advanced(id, Self::TRAVERSE_SHADOW_DOM).and_then(|parent_id| {
                    let raw_parent: Option<(#(*const #parent_dependencies,)*)> = (#(&#parent_view,)*).get(parent_id).ok().map(|c| {
                        let (#(#temps,)*) = c;
                        (#(#temps as *const _,)*)
                    });
                    raw_parent
                });
            }
        }
    };
    let deref_parent_view = {
        if parent_dependencies.is_empty() {
            quote! {
                let parent = raw_parent;
            }
        } else {
            let indexes = (0..parent_dependencies.len()).map(syn::Index::from);
            quote! {
                let parent = unsafe { raw_parent.map(|raw_parent| (#(freya_native_core::prelude::DependancyView::new(&*raw_parent.#indexes),)*)) };
            }
        }
    };

    let child_view = child_dependencies_idxes
        .iter()
        .map(|i| format_ident!("__data{}", i))
        .collect::<Vec<_>>();
    let get_child_view = {
        if child_dependencies.is_empty() {
            quote! {
                let raw_children: Vec<_> = tree.children_ids_advanced(id, Self::TRAVERSE_SHADOW_DOM).into_iter().map(|_| ()).collect();
            }
        } else {
            let temps = (0..child_dependencies.len())
                .map(|i| format_ident!("__temp{}", i))
                .collect::<Vec<_>>();
            quote! {
                let raw_children: Vec<_> = tree.children_ids_advanced(id, Self::TRAVERSE_SHADOW_DOM).into_iter().filter_map(|id| {
                    let raw_children: Option<(#(*const #child_dependencies,)*)> = (#(&#child_view,)*).get(id).ok().map(|c| {
                        let (#(#temps,)*) = c;
                        (#(#temps as *const _,)*)
                    });
                    raw_children
                }).collect();
            }
        }
    };
    let deref_child_view = {
        if child_dependencies.is_empty() {
            quote! {
                let children = raw_children;
            }
        } else {
            let indexes = (0..child_dependencies.len()).map(syn::Index::from);
            quote! {
                let children = unsafe { raw_children.iter().map(|raw_children| (#(freya_native_core::prelude::DependancyView::new(&*raw_children.#indexes),)*)).collect::<Vec<_>>() };
            }
        }
    };

    let trait_generics = trait_
        .as_ref()
        .unwrap()
        .segments
        .last()
        .unwrap()
        .arguments
        .clone();

    // if a create function is defined, we don't generate one
    // otherwise we generate a default one that uses the update function and the default constructor
    let create_fn = (!has_create_fn).then(|| {
        quote! {
            fn create<'a>(
                node_view: freya_native_core::prelude::NodeView # trait_generics,
                node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
                parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
                children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
                context: &freya_native_core::prelude::SendAnyMap,
            ) -> Self {
                let mut myself = Self::default();
                myself.update(node_view, node, parent, children, context);
                myself
            }
        }
    });

    quote!(
        #(#attrs)*
        #defaultness #unsafety #impl_token #generics #trait_ #for_ #self_ty {
            #create_fn

            #(#items)*

            fn workload_system(type_id: std::any::TypeId, dependants: std::sync::Arc<freya_native_core::prelude::Dependants>, pass_direction: freya_native_core::prelude::PassDirection) -> freya_native_core::exports::shipyard::WorkloadSystem {
                use freya_native_core::exports::shipyard::{IntoWorkloadSystem, Get, AddComponent};
                use freya_native_core::tree::TreeRef;
                use freya_native_core::prelude::{NodeType, NodeView};

                let node_mask = Self::NODE_MASK.build();

                (move |data: #combined_dependencies_quote, run_view: freya_native_core::prelude::RunPassView #trait_generics| {
                    let (#(#split_views,)*) = data;
                    let tree = run_view.tree.clone();
                    let node_types = run_view.node_type.clone();
                    freya_native_core::prelude::run_pass(type_id, dependants.clone(), pass_direction, run_view, |id, context, height| {
                        let node_data: &NodeType<_> = node_types.get(id).unwrap_or_else(|err| panic!("Failed to get node type {:?}", err));
                        if node_data.is_text() {
                            return false;
                        }
                        // get all of the states from the tree view
                        // Safety: No node has itself as a parent or child.
                        let raw_myself: Option<*mut Self> = (&mut #this_view).get(id).ok().map(|c| c as *mut _);
                        #get_node_view
                        #get_parent_view
                        #get_child_view

                        let myself: Option<&mut Self> = unsafe { raw_myself.map(|val| &mut *val) };
                        #deref_node_view
                        #deref_parent_view
                        #deref_child_view

                        let view = NodeView::new(id, node_data, &node_mask, height);
                        if let Some(myself) = myself {
                            myself
                                .update(view, node, parent, children, context)
                        }
                        else {
                            (&mut #this_view).add_component_unchecked(
                                id,
                                Self::create(view, node, parent, children, context));
                            true
                        }
                    })
                }).into_workload_system().unwrap()
            }
        }
    )
    .into()
}

fn extract_tuple(ty: &Type) -> Option<TypeTuple> {
    match ty {
        Type::Tuple(tuple) => Some(tuple.clone()),
        Type::Group(group) => extract_tuple(&group.elem),
        _ => None,
    }
}

fn extract_type_path(ty: &Type) -> Option<TypePath> {
    match ty {
        Type::Path(path) => Some(path.clone()),
        Type::Group(group) => extract_type_path(&group.elem),
        _ => None,
    }
}