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
use quote::quote;
use syn::parse_quote;
use crate::cmd::{
scope_builder_fields, with_params::cmd_ctx_builder_with_params_selected,
CmdCtxBuilderTypeBuilder, ImplHeaderBuilder, ParamsScope, Scope, ScopeStruct,
};
/// Generates the `with_workspace_params_k` / `with_profile_params_k` /
/// `with_flow_params_k` methods.
pub fn impl_with_params_k(scope_struct: &ScopeStruct) -> proc_macro2::TokenStream {
let scope = scope_struct.scope();
ParamsScope::iter().fold(
proc_macro2::TokenStream::new(),
|mut impl_tokens, params_scope| {
match params_scope {
ParamsScope::Workspace => {}
ParamsScope::Profile => {
if !scope.profile_params_supported() {
return impl_tokens;
}
}
ParamsScope::Flow => {
if !scope.flow_params_supported() {
return impl_tokens;
}
}
}
let impl_with_params_k_key_unknown =
impl_with_params_k_key_unknown(scope_struct, params_scope);
impl_tokens.extend(impl_with_params_k_key_unknown);
match scope {
// Single profile params technically don't need this, but it is convenient to
// have a common `with_*_param` registration for each param type.
//
// Single profile params will also have `with_*_param_value` from the
// `impl_with_param_value` module.
Scope::SingleProfileNoFlow |
Scope::SingleProfileSingleFlow |
// Multi profile commands need to register the type of each profile param.
Scope::MultiProfileNoFlow |
Scope::MultiProfileSingleFlow => {
let impl_with_param_key_known = impl_with_param_key_known(scope_struct, params_scope);
impl_tokens.extend(impl_with_param_key_known);
}
Scope::NoProfileNoFlow if params_scope == ParamsScope::Workspace => {
let impl_with_param_key_known = impl_with_param_key_known(scope_struct, params_scope);
impl_tokens.extend(impl_with_param_key_known);
}
// No profile commands do not support profile params.
Scope::NoProfileNoFlow => {}
}
impl_tokens
},
)
}
fn impl_with_params_k_key_unknown(
scope_struct: &ScopeStruct,
params_scope: ParamsScope,
) -> proc_macro2::TokenStream {
let scope = scope_struct.scope();
let scope_builder_name = &scope_struct.item_struct().ident;
let params_scope_str = params_scope.to_str();
let with_params_k_method_name = params_scope.params_k_method_name();
let params_k_method_name = params_scope.params_k_method_name();
let params_map_type = params_scope.params_map_type();
let params_k_type_param = params_scope.params_k_type_param();
let scope_builder_fields_params_none = scope_builder_fields::params_none(scope, params_scope);
let scope_builder_fields_params_some_new =
scope_builder_fields::params_some_new(scope, params_scope);
let doc_summary = format!("Registers a {params_scope_str} parameter type.");
// ```rust,ignore
// crate::ctx::CmdCtxBuilder<
// 'ctx,
// crate::ctx::CmdCtxBuilderTypesCollector<
// Output,
// AppError,
// peace_rt_model::params::ParamsKeysImpl<
// WorkspaceParamsKMaybe = peace_rt_model::params::KeyUnknown,
// ProfileParamsKMaybe,
// FlowParamsKMaybe,
// >,
// WorkspaceParamsSelection,
// ProfileParamsSelection,
// FlowParamsSelection,
// ProfileSelection,
// FlowSelection,
// >,
// >
// ```
let builder_type = {
let builder_type_builder = CmdCtxBuilderTypeBuilder::new(scope_builder_name.clone());
match params_scope {
ParamsScope::Workspace => builder_type_builder
.with_workspace_params_k_maybe(parse_quote!(peace_rt_model::params::KeyUnknown))
.with_workspace_params_selection(parse_quote!(
crate::scopes::type_params::WorkspaceParamsNone
)),
ParamsScope::Profile => builder_type_builder
.with_profile_params_k_maybe(parse_quote!(peace_rt_model::params::KeyUnknown))
.with_profile_params_selection(parse_quote!(
crate::scopes::type_params::ProfileParamsNone
)),
ParamsScope::Flow => builder_type_builder
.with_flow_params_k_maybe(parse_quote!(peace_rt_model::params::KeyUnknown))
.with_flow_params_selection(parse_quote!(
crate::scopes::type_params::FlowParamsNone
)),
}
.build()
};
let impl_header = {
let impl_header_builder = ImplHeaderBuilder::new(builder_type);
match params_scope {
ParamsScope::Workspace => impl_header_builder
.with_workspace_params_k_maybe(None)
.with_workspace_params_selection(None),
ParamsScope::Profile => impl_header_builder
.with_profile_params_k_maybe(None)
.with_profile_params_selection(None),
ParamsScope::Flow => impl_header_builder
.with_flow_params_k_maybe(None)
.with_flow_params_selection(None),
}
.build()
};
let return_type =
cmd_ctx_builder_with_params_selected(scope_builder_name, scope_struct, params_scope);
quote! {
#impl_header
{
#[doc = #doc_summary]
///
/// This is necessary to deserialize previously stored parameters.
///
// pub fn with_workspace_params_k<WorkspaceParamsK>
pub fn #with_params_k_method_name<#params_k_type_param>(
self,
) ->
// crate::ctx::CmdCtxBuilder<
// 'ctx,
// crate::ctx::CmdCtxBuilderTypesCollector<
// CmdCtxBuilderTypesT::Output,
// CmdCtxBuilderTypesT::AppError,
// peace_rt_model::params::ParamsKeysImpl<
// peace_rt_model::params::KeyKnown<WorkspaceParamsK>,
// ProfileParamsKMaybe,
// FlowParamsKMaybe,
// >,
// crate::scopes::type_params::WorkspaceParamsSome<WorkspaceParamsK>,
// CmdCtxBuilderTypesT::ProfileParamsSelection,
// CmdCtxBuilderTypesT::FlowParamsSelection,
// CmdCtxBuilderTypesT::ProfileSelection,
// CmdCtxBuilderTypesT::FlowSelection,
// >,
// >
#return_type
where
#params_k_type_param:
Clone + std::fmt::Debug + Eq + std::hash::Hash + serde::de::DeserializeOwned + serde::Serialize + Send + Sync + Unpin + 'static,
{
let Self {
output,
interruptibility,
workspace,
resources,
scope_builder:
#scope_builder_name {
// profile_selection,
// flow_selection,
// params_type_regs_builder,
// workspace_params_selection: WorkspaceParamsNone,
// profile_params_selection,
// flow_params_selection,
#scope_builder_fields_params_none
},
} = self;
// let mut params_type_regs_builder =
// params_type_regs_builder.with_workspace_params_k::<WorkspaceParamsK>();
let mut params_type_regs_builder =
params_type_regs_builder.#params_k_method_name::<#params_k_type_param>();
// let mut workspace_params = WorkspaceParams::<WorkspaceParam>::new();
let params_map = peace_rt_model::params::#params_map_type::<#params_k_type_param>::new();
let scope_builder = #scope_builder_name {
// profile_selection,
// flow_selection,
// params_type_regs_builder,
// workspace_params_selection: WorkspaceParamsSome(params_map),
// profile_params_selection,
// flow_params_selection,
#scope_builder_fields_params_some_new
};
crate::ctx::CmdCtxBuilder {
output,
interruptibility,
workspace,
resources,
scope_builder,
}
}
}
}
}
fn impl_with_param_key_known(
scope_struct: &ScopeStruct,
params_scope: ParamsScope,
) -> proc_macro2::TokenStream {
let scope = scope_struct.scope();
let scope_builder_name = &scope_struct.item_struct().ident;
let params_scope_str = params_scope.to_str();
let with_param_method_name = params_scope.with_param_method_name();
let with_param_value_method_name = params_scope.with_param_value_method_name();
let param_type_param = params_scope.param_type_param();
let params_k_type_param = params_scope.params_k_type_param();
let scope_builder_fields_params_some = scope_builder_fields::params_some(scope, params_scope);
let scope_builder_fields_passthrough = scope_builder_fields::passthrough(scope, params_scope);
let params_type_reg_method_name = params_scope.params_type_reg_mut_method_name();
let doc_summary = format!("Registers a {params_scope_str} parameter type.");
let doc_body =
format!("See the `{with_param_value_method_name}` method if you want to specify a value.");
let builder_type =
cmd_ctx_builder_with_params_selected(scope_builder_name, scope_struct, params_scope);
let impl_header = {
let impl_header_builder = ImplHeaderBuilder::new(builder_type);
match params_scope {
ParamsScope::Workspace => impl_header_builder
.with_workspace_params_k_maybe(None)
.with_workspace_params_k(Some(parse_quote!(WorkspaceParamsK)))
.with_workspace_params_selection(None),
ParamsScope::Profile => impl_header_builder
.with_profile_params_k_maybe(None)
.with_profile_params_k(Some(parse_quote!(ProfileParamsK)))
.with_profile_params_selection(None),
ParamsScope::Flow => impl_header_builder
.with_flow_params_k_maybe(None)
.with_flow_params_k(Some(parse_quote!(FlowParamsK)))
.with_flow_params_selection(None),
}
.build()
};
quote! {
#impl_header
{
#[doc = #doc_summary]
///
#[doc = #doc_body]
///
/// # Parameters
///
/// * `k`: Key to store the parameter with.
// pub fn with_workspace_params<WorkspaceParam>
///
/// # Type Parameters
///
/// * `#param_type_param` The type that is stored against `#params_k_type_param`.
pub fn #with_param_method_name<#param_type_param>(
self,
k: #params_k_type_param,
) -> Self
where
#param_type_param: Clone + std::fmt::Debug + serde::de::DeserializeOwned + serde::Serialize + Send + Sync + Unpin + 'static,
{
let Self {
output,
interruptibility,
workspace,
resources,
scope_builder:
#scope_builder_name {
// profile_selection,
// flow_selection,
// mut params_type_regs_builder,
// mut workspace_params_selection,
// profile_params_selection,
// flow_params_selection,
#scope_builder_fields_params_some
},
} = self;
params_type_regs_builder
.#params_type_reg_method_name()
.register::<#param_type_param>(k.clone());
let scope_builder = #scope_builder_name {
// profile_selection,
// flow_selection,
// params_type_regs_builder,
// workspace_params_selection,
// profile_params_selection,
// flow_params_selection,
#scope_builder_fields_passthrough
};
crate::ctx::CmdCtxBuilder {
output,
interruptibility,
workspace,
resources,
scope_builder,
}
}
}
}
}