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
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use syn::{spanned::Spanned, Error, GenericArgument, PathArguments};
use syn::{Path, Visibility};
use crate::{macros::Macros, ItemImpl};
mod funcs;
mod model_types;
mod token_streams;
pub(crate) fn generate_tokens(
visibility: Option<Visibility>,
relm4_path: Path,
data: ItemImpl,
) -> TokenStream2 {
if !data.types.is_empty() {
return Error::new(data.types[0].span(), "Didn't expect a type parameter")
.to_compile_error();
}
let trait_generics = if let PathArguments::AngleBracketed(generics) =
&data.trait_.segments.last().unwrap().arguments
{
generics
} else {
return Error::new(
data.trait_.segments.span(),
"Expected generic parameters for the micro model",
)
.to_compile_error();
};
let model_types::ModelTypes { model } = match model_types::ModelTypes::new(trait_generics) {
Ok(model) => model,
Err(err) => return err.to_compile_error(),
};
let trait_ = data.trait_;
let ty = data.self_ty;
let outer_attrs = &data.outer_attrs;
let path_args = trait_
.segments
.last()
.map(|segment| &segment.arguments)
.unwrap();
let model_ty_opt = if let PathArguments::AngleBracketed(angle_args) = path_args {
if let Some(GenericArgument::Type(model_ty)) = angle_args.args.first() {
Some(model_ty)
} else {
None
}
} else {
None
};
let model_type = if let Some(model_type) = model_ty_opt {
model_type
} else {
return Error::new(
path_args.span(),
"Expected generic parameters for the micro model",
)
.to_compile_error();
};
let Macros {
widgets,
additional_fields,
menus,
} = match Macros::new(&data.macros, data.brace_span.unwrap()) {
Ok(macros) => macros,
Err(err) => return err.to_compile_error(),
};
let menus_stream = menus.map(|m| m.menus_stream(&relm4_path));
let funcs::Funcs {
pre_init,
post_init,
pre_view,
post_view,
} = match funcs::Funcs::new(&data.funcs) {
Ok(macros) => macros,
Err(err) => return err.to_compile_error(),
};
let root_widget_name = &widgets.name;
let root_widget_type = widgets.func.type_token_stream();
let mut streams = token_streams::TokenStreams::default();
widgets.generate_micro_widget_tokens_recursively(
&mut streams,
&visibility,
model_type,
&relm4_path,
);
let token_streams::TokenStreams {
struct_fields,
init_widgets,
connect_widgets,
assign_properties,
connect,
return_fields,
connect_components,
view,
track,
} = streams;
let impl_generics = data.impl_generics;
let where_clause = data.where_clause;
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()
};
quote! {
#[allow(dead_code)]
#outer_attrs
#visibility struct #ty {
#struct_fields
#additional_fields
}
impl #impl_generics #trait_ for #ty #where_clause {
type Root = #root_widget_type;
fn init_view(model: &#model, sender: #relm4_path::Sender<<#model as #relm4_path::MicroModel>::Msg>) -> Self {
#pre_init
#init_widgets
#connect_widgets
#menus_stream
#assign_properties
#connect
#connect_components
#post_init
Self {
#return_fields
#additional_fields_return_stream
}
}
fn root_widget(&self) -> Self::Root {
self.#root_widget_name.clone()
}
fn view(&mut self, model: &#model, sender: #relm4_path::Sender<<#model as #relm4_path::MicroModel>::Msg>) {
(|| { #pre_view })();
#view
#track
(|| { #post_view })();
}
}
}
}