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
|
#![allow(clippy::too_many_arguments)]
use crate::res_injection::{
ResourceInjection, extract_args_info, generate_immut_resource_bindings,
wrap_body_with_mut_resources, wrap_body_with_mut_resources_async,
};
use proc_macro::TokenStream;
use quote::{ToTokens, quote};
use syn::spanned::Spanned;
use syn::{Ident, ItemFn, Pat, ReturnType, Signature, Type, TypePath, parse_macro_input};
/// Checks whether the return type is `()`
fn is_unit_return_type(sig: &Signature) -> bool {
match &sig.output {
ReturnType::Type(_, ty) => match &**ty {
Type::Tuple(tuple) => tuple.elems.is_empty(),
_ => false,
},
ReturnType::Default => true,
}
}
/// Validates that the return type is `Next`, `ChainProcess<ThisProgram>`, `()`, or omitted.
fn validate_return_type(sig: &Signature) -> Result<(), proc_macro2::TokenStream> {
// `()` or omitted is always valid
if is_unit_return_type(sig) {
return Ok(());
}
match &sig.output {
ReturnType::Type(_, ty) => match &**ty {
Type::Path(type_path) => {
let last_segment = type_path.path.segments.last().unwrap();
let ident_str = last_segment.ident.to_string();
if ident_str == "Next" || ident_str == "ChainProcess" {
return Ok(());
}
Err(syn::Error::new(
ty.span(),
"Chain function must return `Next`, `ChainProcess<ThisProgram>`, `()`, or omit the return type",
)
.to_compile_error())
}
_ => Err(syn::Error::new(
ty.span(),
"Chain function must return `Next`, `ChainProcess<ThisProgram>`, `()`, or omit the return type",
)
.to_compile_error()),
},
ReturnType::Default => {
Err(syn::Error::new(
sig.span(),
"Chain function must specify a return type (must be `Next`, `ChainProcess<ThisProgram>`, or `()`)",
)
.to_compile_error())
}
}
}
/// Builds the `proc` function implementation inside the generated `Chain` impl.
///
/// The user's function body is inlined directly, and its result is converted
/// via `.into()` to `ChainProcess<ProgramType>`.
#[allow(unused_variables)]
fn generate_proc_fn(
has_resources: bool,
resources: &[ResourceInjection],
program_type: &proc_macro2::TokenStream,
previous_type: &TypePath,
prev_param: &Pat,
fn_body_stmts: &[syn::Stmt],
is_async_fn: bool,
is_unit_return: bool,
) -> proc_macro2::TokenStream {
let immut_resource_stmts = generate_immut_resource_bindings(resources.iter(), program_type);
let mut_resources: Vec<_> = resources.iter().filter(|r| r.is_mut).collect();
let wrapped_body = if is_async_fn && !mut_resources.is_empty() {
wrap_body_with_mut_resources_async(fn_body_stmts, &mut_resources, program_type)
} else {
wrap_body_with_mut_resources(fn_body_stmts, &mut_resources, program_type, is_unit_return)
};
let proc_body = if is_unit_return {
let body_with_ending = if has_resources {
quote! {
#(#immut_resource_stmts)*
#wrapped_body;
<crate::ResultEmpty as ::mingling::Groupped::<crate::ThisProgram>>
::to_chain(crate::ResultEmpty)
}
} else {
quote! {
#wrapped_body;
<crate::ResultEmpty as ::mingling::Groupped::<crate::ThisProgram>>
::to_chain(crate::ResultEmpty)
}
};
quote! { #body_with_ending }
} else {
let body = if has_resources {
quote! {
#(#immut_resource_stmts)*
#wrapped_body
}
} else {
quote! { #wrapped_body }
};
// Use a let-binding with explicit type annotation so that the inner
// body's `.into()` calls resolve correctly (same as the original function).
quote! {
let __chain_result: ::mingling::ChainProcess<#program_type> = { #body };
__chain_result
}
};
#[cfg(feature = "async")]
{
quote! {
async fn proc(#prev_param: #previous_type) -> ::mingling::ChainProcess<#program_type> {
#proc_body
}
}
}
#[cfg(not(feature = "async"))]
{
quote! {
fn proc(#prev_param: #previous_type) -> ::mingling::ChainProcess<#program_type> {
#proc_body
}
}
}
}
/// Assembles the final expanded output: hidden struct, `register_chain!` invocation,
/// `Chain` impl with the `proc` method, and the preserved original function.
fn generate_struct_and_impl(
fn_attrs: &[syn::Attribute],
vis: &syn::Visibility,
struct_name: &Ident,
previous_type: &TypePath,
previous_type_str: &proc_macro2::TokenStream,
program_type: &proc_macro2::TokenStream,
proc_fn: &proc_macro2::TokenStream,
original_fn: &proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
quote! {
#(#fn_attrs)*
#[doc(hidden)]
#[allow(non_camel_case_types)]
#vis struct #struct_name;
::mingling::macros::register_chain!(#previous_type_str, #struct_name);
impl ::mingling::Chain<#program_type> for #struct_name {
type Previous = #previous_type;
#proc_fn
}
// Keep the original function unchanged
#original_fn
}
}
/// Ensures the function is not async when the `async` feature is disabled.
#[cfg(not(feature = "async"))]
fn reject_async(sig: &Signature) -> Result<(), proc_macro2::TokenStream> {
if sig.asyncness.is_some() {
return Err(syn::Error::new(
sig.span(),
"Chain function cannot be async when async feature is disabled",
)
.to_compile_error());
}
Ok(())
}
pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream {
// Reject non-empty attribute arguments; #[chain] must be bare
if !attr.is_empty() {
return syn::Error::new(
attr.into_iter().next().unwrap().span().into(),
"#[chain] does not accept arguments",
)
.to_compile_error()
.into();
}
// Parse the function item
let input_fn = parse_macro_input!(item as ItemFn);
// Handle async feature gate
#[cfg(feature = "async")]
let is_async_fn = input_fn.sig.asyncness.is_some();
#[cfg(not(feature = "async"))]
{
if let Err(err) = reject_async(&input_fn.sig) {
return err.into();
}
}
// Check if return type is unit
let is_unit_return = is_unit_return_type(&input_fn.sig);
// Validate return type
if let Err(err) = validate_return_type(&input_fn.sig) {
return err.into();
}
// Extract the previous type, parameter name, and resource injection params
let (prev_param, previous_type, resources) = match extract_args_info(&input_fn.sig) {
Ok(info) => info,
Err(e) => return e.to_compile_error().into(),
};
// Prepare building blocks
let fn_body = &input_fn.block;
let mut fn_attrs = input_fn.attrs.clone();
fn_attrs.retain(|attr| !attr.path().is_ident("chain"));
let vis = &input_fn.vis;
let fn_name = &input_fn.sig.ident;
let has_resources = !resources.is_empty();
// Generate struct name
let internal_name = format!(
"__internal_chain_{}",
just_fmt::snake_case!(fn_name.to_string())
);
let struct_name = Ident::new(&internal_name, fn_name.span());
// Always use the default crate-defined program path
let program_type = crate::default_program_path();
// Generate the `proc` function for the Chain impl
let proc_fn = generate_proc_fn(
has_resources,
&resources,
&program_type,
&previous_type,
&prev_param,
&fn_body.stmts,
#[cfg(feature = "async")]
is_async_fn,
#[cfg(not(feature = "async"))]
false,
is_unit_return,
);
// Preserve the original function untouched, with dead_code allowed
// since it may only be called through the Chain trait dispatch.
// Note: do NOT add `#vis` here — `input_fn` (ItemFn) already contains its own visibility.
let original_fn = quote! {
#[allow(dead_code)]
#(#fn_attrs)*
#input_fn
};
// Assemble the final output
let previous_type_str = quote! { #previous_type };
let expanded = generate_struct_and_impl(
&fn_attrs,
vis,
&struct_name,
&previous_type,
&previous_type_str,
&program_type,
&proc_fn,
&original_fn,
);
expanded.into()
}
/// Builds a match arm for chain mapping
pub fn build_chain_arm(struct_name: &Ident, previous_type: &TypePath) -> proc_macro2::TokenStream {
let enum_variant = &previous_type.path.segments.last().unwrap().ident;
quote! {
#struct_name => #enum_variant,
}
}
/// Builds a match arm for chain existence check
pub fn build_chain_exist_arm(previous_type: &TypePath) -> proc_macro2::TokenStream {
let enum_variant = &previous_type.path.segments.last().unwrap().ident;
quote! {
Self::#enum_variant => true,
}
}
pub fn register_chain(input: TokenStream) -> TokenStream {
// Parse the input as a comma-separated list of arguments
let input_parsed = syn::parse_macro_input!(input with syn::punctuated::Punctuated<syn::Expr, syn::Token![,]>::parse_terminated);
// Check that there are exactly two elements
if input_parsed.len() != 2 {
return syn::Error::new(
input_parsed.span(),
"Expected exactly two comma-separated arguments: `PreviousType, StructName`",
)
.to_compile_error()
.into();
}
// Extract the two elements
let previous_type_expr = &input_parsed[0];
let struct_name_expr = &input_parsed[1];
// Convert expressions to TypePath and Ident
let previous_type = match syn::parse2::<TypePath>(previous_type_expr.to_token_stream()) {
Ok(ty) => ty,
Err(e) => return e.to_compile_error().into(),
};
let struct_name = match syn::parse2::<syn::Ident>(struct_name_expr.to_token_stream()) {
Ok(ident) => ident,
Err(e) => return e.to_compile_error().into(),
};
// Record the chain mapping: previous_type => struct_name
let chain_entry = build_chain_arm(&struct_name, &previous_type);
// Record the chain existence check
let chain_exist_entry = build_chain_exist_arm(&previous_type);
let mut chains = crate::get_global_set(&crate::CHAINS).lock().unwrap();
let mut chain_exist = crate::get_global_set(&crate::CHAINS_EXIST).lock().unwrap();
let chain_entry_str = chain_entry.to_string();
let chain_exist_entry_str = chain_exist_entry.to_string();
// Check for duplicate variant before inserting
let variant_name = previous_type
.path
.segments
.last()
.unwrap()
.ident
.to_string();
if let Err(err) = crate::check_duplicate_variant(
&chains,
&chain_entry_str,
&variant_name,
"chain",
previous_type.span(),
) {
return err.into();
}
chains.insert(chain_entry_str);
chain_exist.insert(chain_exist_entry_str);
quote! {}.into()
}
|