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
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned, ToTokens};
use syn::parse_quote;
use syn::visit_mut::VisitMut;
use crate::attrs::Attrs;
use crate::token_streams::{TokenStreams, TraitImplDetails};
use crate::util;
use crate::visitors::{ComponentVisitor, PreAndPostView, ViewOutputExpander};
pub(crate) fn generate_tokens(
global_attributes: Attrs,
mut component_impl: syn::ItemImpl,
) -> TokenStream2 {
let Attrs {
visibility,
asyncness,
} = global_attributes;
let mut errors = vec![];
let mut component_visitor = ComponentVisitor::new(&mut errors);
component_visitor.visit_item_impl_mut(&mut component_impl);
let additional_fields = component_visitor.additional_fields.take();
let menus_stream = component_visitor
.menus
.take()
.map(|menus| menus.menus_stream());
let mut struct_fields = None;
match &component_visitor.view_widgets {
None => component_visitor.errors.push(syn::Error::new_spanned(
&component_impl,
"expected `view!` macro invocation",
)),
Some(Err(e)) => component_visitor.errors.push(e.clone()),
_ => (),
}
if let ComponentVisitor {
view_widgets: Some(Ok(view_widgets)),
model_name: Some(model_name),
root_name: Some(root_name),
sender_name: Some(sender_name),
errors,
..
} = component_visitor
{
let trait_impl_details = TraitImplDetails {
vis: visibility.clone(),
model_name,
sender_name,
root_name: Some(root_name),
};
let TokenStreams {
error,
init_root,
rename_root,
struct_fields: struct_fields_stream,
init: init_widgets,
assign,
return_fields,
destructure_fields,
update_view,
} = view_widgets.generate_streams(&trait_impl_details, false);
let model_name = trait_impl_details.model_name;
struct_fields = Some(struct_fields_stream);
let root_widget_type = view_widgets.root_type();
let additional_fields_return_stream = if let Some(fields) = &additional_fields {
let mut tokens = TokenStream2::new();
for field in fields.inner.pairs() {
tokens.extend(field.value().ident.to_token_stream());
tokens.extend(quote! {,});
}
tokens
} else {
TokenStream2::new()
};
let view_code = quote! {
#rename_root
#menus_stream
#init_widgets
#assign
{
#error
}
};
let widgets_return_code = parse_quote! {
Self::Widgets {
#return_fields
#additional_fields_return_stream
}
};
ViewOutputExpander::expand(&mut component_impl, view_code, widgets_return_code, errors);
component_impl.items.push(parse_quote! {
type Root = #root_widget_type;
});
let ty: syn::Type = parse_quote!(Self::Root);
let init_root = util::verbatim_impl_item_method("init_root", Vec::new(), ty, init_root);
component_impl.items.push(init_root);
let PreAndPostView {
pre_view,
post_view,
..
} = PreAndPostView::extract(&mut component_impl, errors);
let sender_ty: syn::TypePath = if asyncness.is_some() {
parse_quote! { relm4::AsyncComponentSender }
} else {
parse_quote! { relm4::ComponentSender }
};
component_impl.items.push(parse_quote! {
fn update_view(
&self,
widgets: &mut Self::Widgets,
sender: #sender_ty<Self>,
) {
struct __DoNotReturnManually;
let _no_manual_return: __DoNotReturnManually = (move || {
#[allow(unused_variables)]
let Self::Widgets {
#destructure_fields
#additional_fields_return_stream
} = widgets;
#[allow(unused_variables)]
let #model_name = self;
#(#pre_view)*
#update_view
(move || { #(#post_view)* })();
__DoNotReturnManually
})();
}
});
}
let widgets_name = util::generate_widgets_type(
component_visitor.widgets_ty,
&mut component_impl,
&mut errors,
);
let widgets_struct = widgets_name.map(|widgets_name| {
let outer_attrs = &component_impl.attrs;
quote! {
#[allow(dead_code)]
#(#outer_attrs)*
#[derive(Debug)]
#visibility struct #widgets_name {
#struct_fields
#additional_fields
}
}
});
let errors = errors.iter().map(syn::Error::to_compile_error);
let async_trait = asyncness.map(
|async_token| quote_spanned!(async_token.span => #[relm4::async_trait::async_trait(?Send)]),
);
quote! {
#widgets_struct
#async_trait
#component_impl
#(#errors)*
}
}