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
|
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{Attribute, Ident, LitStr, Result as SynResult, Token};
#[cfg(feature = "dispatch_tree")]
use crate::COMPILE_TIME_DISPATCHERS;
enum DispatcherChainInput {
Explicit {
cmd_attrs: Vec<Attribute>,
entry_attrs: Vec<Attribute>,
group_name: syn::Path,
command_name: syn::LitStr,
command_struct: Ident,
pack: Ident,
},
Default {
cmd_attrs: Vec<Attribute>,
entry_attrs: Vec<Attribute>,
command_name: syn::LitStr,
command_struct: Ident,
pack: Ident,
},
#[cfg(feature = "extra_macros")]
Auto {
cmd_attrs: Vec<Attribute>,
command_name: syn::LitStr,
},
}
impl Parse for DispatcherChainInput {
fn parse(input: ParseStream) -> SynResult<Self> {
// Collect outer attributes for the CMD struct
let cmd_attrs = input.call(Attribute::parse_outer)?;
if (input.peek(Ident) || input.peek(Token![crate]))
&& (input.peek2(Token![::]) || input.peek2(Token![,]))
{
let group_name = input.parse::<syn::Path>()?;
input.parse::<Token![,]>()?;
let command_name = input.parse()?;
input.parse::<Token![,]>()?;
let command_struct = input.parse()?;
input.parse::<Token![=>]>()?;
let entry_attrs = input.call(Attribute::parse_outer)?;
let pack = input.parse()?;
Ok(DispatcherChainInput::Explicit {
cmd_attrs,
entry_attrs,
group_name,
command_name,
command_struct,
pack,
})
} else if input.peek(syn::LitStr) {
// Parse the command name string first
let command_name: LitStr = input.parse()?;
// Check if this is the abbreviated form: just "command_name" without ", CMD => Entry"
if input.is_empty() {
#[cfg(feature = "extra_macros")]
{
return Ok(DispatcherChainInput::Auto {
cmd_attrs,
command_name,
});
}
#[cfg(not(feature = "extra_macros"))]
{
return Err(syn::Error::new(
command_name.span(),
"expected `, CommandStruct => EntryStruct` after command name",
));
}
}
// Default format: "command_name", CommandStruct => ChainStruct
input.parse::<Token![,]>()?;
let command_struct = input.parse()?;
input.parse::<Token![=>]>()?;
let entry_attrs = input.call(Attribute::parse_outer)?;
let pack = input.parse()?;
Ok(DispatcherChainInput::Default {
cmd_attrs,
entry_attrs,
command_name,
command_struct,
pack,
})
} else {
Err(input.lookahead1().error())
}
}
}
// NOTICE: This implementation contains significant code duplication between the explicit
// and default cases in both `dispatcher_chain` and `dispatcher_render` functions.
// The logic for handling default vs explicit group names and generating the appropriate
// code should be extracted into common helper functions to reduce redundancy.
// Additionally, the token stream generation patterns are nearly identical between
// the two main functions and could benefit from refactoring.
pub fn dispatcher(input: TokenStream) -> TokenStream {
// Parse the input
let dispatcher_input = syn::parse_macro_input!(input as DispatcherChainInput);
#[cfg(not(feature = "extra_macros"))]
let (command_name, command_struct, pack, cmd_attrs, entry_attrs, _use_default, group_path) =
match dispatcher_input {
DispatcherChainInput::Explicit {
cmd_attrs,
entry_attrs,
group_name,
command_name,
command_struct,
pack,
} => (
command_name,
command_struct,
pack,
cmd_attrs,
entry_attrs,
false,
quote! { #group_name },
),
DispatcherChainInput::Default {
cmd_attrs,
entry_attrs,
command_name,
command_struct,
pack,
} => (
command_name,
command_struct,
pack,
cmd_attrs,
entry_attrs,
true,
crate::default_program_path(),
),
};
#[cfg(feature = "extra_macros")]
let (command_name, command_struct, pack, cmd_attrs, entry_attrs, _use_default, group_path) =
match dispatcher_input {
DispatcherChainInput::Explicit {
cmd_attrs,
entry_attrs,
group_name,
command_name,
command_struct,
pack,
} => (
command_name,
command_struct,
pack,
cmd_attrs,
entry_attrs,
false,
quote! { #group_name },
),
DispatcherChainInput::Default {
cmd_attrs,
entry_attrs,
command_name,
command_struct,
pack,
} => (
command_name,
command_struct,
pack,
cmd_attrs,
entry_attrs,
true,
crate::default_program_path(),
),
DispatcherChainInput::Auto {
cmd_attrs,
command_name,
} => {
let command_name_str = command_name.value();
let pascal = dotted_to_pascal_case(&command_name_str);
let command_struct = Ident::new(&format!("CMD{pascal}"), command_name.span());
let pack = Ident::new(&format!("Entry{pascal}"), command_name.span());
(
command_name,
command_struct,
pack,
cmd_attrs,
Vec::new(),
true,
crate::default_program_path(),
)
}
};
let command_name_str = command_name.value();
let comp_entry = get_comp_entry(&pack);
let dispatch_tree_entry = get_dispatch_tree_entry(&command_name_str, &command_struct, &pack);
let expanded = {
let program_path = group_path;
quote! {
#(#cmd_attrs)*
#[derive(Debug, Default)]
pub struct #command_struct;
::mingling::macros::pack!(#(#entry_attrs)* #program_path, #pack = Vec<String>);
#comp_entry
#dispatch_tree_entry
impl ::mingling::Dispatcher<#program_path> for #command_struct {
fn node(&self) -> ::mingling::Node {
::mingling::macros::node!(#command_name_str)
}
fn begin(&self, args: Vec<String>) -> ::mingling::ChainProcess<#program_path> {
#pack::new(args).to_chain()
}
fn clone_dispatcher(&self) -> Box<dyn ::mingling::Dispatcher<#program_path>> {
Box::new(#command_struct)
}
}
}
};
expanded.into()
}
#[cfg(feature = "comp")]
fn get_comp_entry(entry_name: &Ident) -> TokenStream2 {
let comp_entry = quote! {
impl ::mingling::CompletionEntry for #entry_name {
fn get_input(self) -> Vec<String> {
self.inner.clone()
}
}
};
comp_entry
}
#[cfg(not(feature = "comp"))]
fn get_comp_entry(_entry_name: &Ident) -> TokenStream2 {
quote! {}
}
#[cfg(feature = "dispatch_tree")]
fn get_dispatch_tree_entry(
command_name_str: &str,
command_struct: &Ident,
entry_name: &Ident,
) -> TokenStream2 {
let node_name_lit = syn::LitStr::new(command_name_str, proc_macro2::Span::call_site());
quote! {
::mingling::macros::register_dispatcher!(#node_name_lit, #command_struct, #entry_name);
}
}
#[cfg(not(feature = "dispatch_tree"))]
fn get_dispatch_tree_entry(
_command_name_str: &str,
_command_struct: &Ident,
_entry_name: &Ident,
) -> TokenStream2 {
quote! {}
}
#[cfg(feature = "dispatch_tree")]
/// Input format: ("node.name", DispatcherType, EntryName)
struct RegisterDispatcherInput {
node_name: syn::LitStr,
dispatcher_type: Ident,
entry_name: Ident,
}
#[cfg(feature = "dispatch_tree")]
impl Parse for RegisterDispatcherInput {
fn parse(input: ParseStream) -> SynResult<Self> {
let node_name = input.parse()?;
input.parse::<Token![,]>()?;
let dispatcher_type = input.parse()?;
input.parse::<Token![,]>()?;
let entry_name = input.parse()?;
Ok(RegisterDispatcherInput {
node_name,
dispatcher_type,
entry_name,
})
}
}
#[cfg(feature = "dispatch_tree")]
pub fn register_dispatcher(input: TokenStream) -> TokenStream {
let RegisterDispatcherInput {
node_name,
dispatcher_type,
entry_name,
} = syn::parse_macro_input!(input as RegisterDispatcherInput);
let node_name_str = node_name.value();
let static_name = format!("__internal_dispatcher_{}", node_name_str.replace('.', "_"));
let static_ident = Ident::new(&static_name, proc_macro2::Span::call_site());
// Register node info in the global collection at compile time
// Format: "node.name:DispatcherType:EntryName"
crate::get_global_set(&COMPILE_TIME_DISPATCHERS)
.lock()
.unwrap()
.insert(format!(
"{}:{}:{}",
node_name_str, dispatcher_type, entry_name
));
let expanded = quote! {
#[doc(hidden)]
#[allow(nonstandard_style)]
pub static #static_ident: #dispatcher_type = #dispatcher_type;
};
expanded.into()
}
#[cfg(not(feature = "dispatch_tree"))]
pub fn register_dispatcher(_input: TokenStream) -> TokenStream {
quote! {}.into()
}
/// Converts a dotted command name (e.g. "remote.add") to PascalCase (e.g. "RemoteAdd").
///
/// Each segment is split by `.`, the first character of each segment is uppercased,
/// and the segments are joined. This is used by the abbreviated `dispatcher!` syntax
/// (when `Command => Entry` is omitted) to auto-derive struct names.
#[cfg(feature = "extra_macros")]
fn dotted_to_pascal_case(s: &str) -> String {
s.split('.')
.map(|segment| {
let mut chars = segment.chars();
match chars.next() {
None => String::new(),
Some(c) => c.to_uppercase().to_string() + chars.as_str(),
}
})
.collect()
}
|