allframe-macros 0.1.28

Procedural macros for AllFrame framework
Documentation
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! `#[tauri_compat]` proc macro implementation
//!
//! Transforms a function with individual Tauri-style parameters into
//! an allframe handler that accepts a generated args struct.

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{parse2, FnArg, Ident, ItemFn, Pat, ReturnType, Type};

/// Heuristic: checks if the last path segment is `StreamSender`.
///
/// Matches `StreamSender`, `StreamSender<T>`, `allframe_core::router::StreamSender`, etc.
/// Same trade-off as `is_state_type`.
fn is_stream_sender_type(ty: &Type) -> bool {
    if let Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.last() {
            return segment.ident == "StreamSender";
        }
    }
    false
}

/// Check if the attribute tokens contain the `streaming` keyword.
fn has_streaming_attr(attr: &TokenStream) -> bool {
    attr.clone()
        .into_iter()
        .any(|tt| matches!(&tt, proc_macro2::TokenTree::Ident(ident) if ident == "streaming"))
}

/// Heuristic: checks if the last path segment is `Option`.
///
/// This matches `Option<T>`, `std::option::Option<T>`, etc. but could
/// false-positive on a custom type named `Option`. This is the same
/// trade-off Tauri's own macros make and is acceptable for the common case.
fn is_option_type(ty: &Type) -> bool {
    if let Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.last() {
            return segment.ident == "Option";
        }
    }
    false
}

/// Heuristic: checks if the last path segment is `State`.
///
/// Matches `State<Arc<S>>`, `allframe_core::router::State<...>`, etc.
/// Same trade-off as `is_option_type` — a custom type named `State` would
/// be misclassified. In practice this is fine because `State` in handler
/// signatures almost always refers to the framework extractor.
fn is_state_type(ty: &Type) -> bool {
    if let Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.last() {
            return segment.ident == "State";
        }
    }
    false
}

/// Convert snake_case to PascalCase
fn to_pascal_case(s: &str) -> String {
    s.split('_')
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(c) => c.to_uppercase().chain(chars).collect(),
            }
        })
        .collect()
}

pub fn tauri_compat_impl(attr: TokenStream, item: TokenStream) -> syn::Result<TokenStream> {
    let streaming = has_streaming_attr(&attr);
    let input_fn: ItemFn = parse2(item)?;

    let fn_name = &input_fn.sig.ident;
    let fn_vis = &input_fn.vis;
    let fn_attrs = &input_fn.attrs;
    let fn_body = &input_fn.block;
    let fn_asyncness = &input_fn.sig.asyncness;

    // Determine return type
    let return_type = match &input_fn.sig.output {
        ReturnType::Default => quote! { () },
        ReturnType::Type(_, ty) => quote! { #ty },
    };

    // Separate params into State extractors, StreamSender, and regular args
    let mut state_params: Vec<(Ident, Type)> = Vec::new();
    let mut stream_param: Option<(Ident, Type)> = None;
    let mut arg_params: Vec<(Ident, Type)> = Vec::new();

    for arg in &input_fn.sig.inputs {
        match arg {
            FnArg::Receiver(_) => {
                return Err(syn::Error::new_spanned(
                    arg,
                    "#[tauri_compat] cannot be applied to methods with self",
                ));
            }
            FnArg::Typed(pat_type) => {
                let ident = match pat_type.pat.as_ref() {
                    Pat::Ident(pat_ident) => pat_ident.ident.clone(),
                    _ => {
                        return Err(syn::Error::new_spanned(
                            &pat_type.pat,
                            "#[tauri_compat] requires simple parameter names",
                        ));
                    }
                };
                let ty = pat_type.ty.as_ref().clone();

                if is_state_type(&ty) {
                    state_params.push((ident, ty));
                } else if streaming && is_stream_sender_type(&ty) {
                    if stream_param.is_some() {
                        return Err(syn::Error::new_spanned(
                            pat_type,
                            "#[tauri_compat(streaming)] supports at most one StreamSender parameter",
                        ));
                    }
                    stream_param = Some((ident, ty));
                } else {
                    arg_params.push((ident, ty));
                }
            }
        }
    }

    // If streaming is set but no StreamSender parameter found, emit a compile error
    if streaming && stream_param.is_none() {
        return Err(syn::Error::new_spanned(
            &input_fn.sig,
            "#[tauri_compat(streaming)] requires a StreamSender parameter",
        ));
    }

    // Generate the args struct name: fn_name -> FnNameArgs
    let struct_name = format_ident!("{}Args", to_pascal_case(&fn_name.to_string()));

    // Build struct fields with serde attributes for Option types.
    // Field visibility matches the function's visibility.
    let struct_fields: Vec<TokenStream> = arg_params
        .iter()
        .map(|(name, ty)| {
            if is_option_type(ty) {
                quote! {
                    #[serde(default)]
                    #fn_vis #name: #ty
                }
            } else {
                quote! { #fn_vis #name: #ty }
            }
        })
        .collect();

    // Generate the args struct (empty struct if no args)
    let args_struct = if arg_params.is_empty() {
        quote! {
            #[derive(serde::Deserialize)]
            #[allow(dead_code)]
            #fn_vis struct #struct_name;
        }
    } else {
        quote! {
            #[derive(serde::Deserialize)]
            #[allow(dead_code)]
            #fn_vis struct #struct_name {
                #(#struct_fields),*
            }
        }
    };

    // Build destructure pattern
    let field_names: Vec<&Ident> = arg_params.iter().map(|(name, _)| name).collect();
    let state_names: Vec<&Ident> = state_params.iter().map(|(name, _)| name).collect();
    let state_types: Vec<&Type> = state_params.iter().map(|(_, ty)| ty).collect();

    // Generate the rewritten function
    let generated_fn = if let Some((stream_ident, stream_ty)) = &stream_param {
        // Streaming mode: StreamSender goes after args struct in signature
        if state_params.is_empty() && arg_params.is_empty() {
            // Only StreamSender, no state, no args
            quote! {
                #(#fn_attrs)*
                #fn_vis #fn_asyncness fn #fn_name(#stream_ident: #stream_ty) -> #return_type
                #fn_body
            }
        } else if state_params.is_empty() {
            // Args + StreamSender, no state
            quote! {
                #(#fn_attrs)*
                #fn_vis #fn_asyncness fn #fn_name(args: #struct_name, #stream_ident: #stream_ty) -> #return_type {
                    let #struct_name { #(#field_names),* } = args;
                    #fn_body
                }
            }
        } else if arg_params.is_empty() {
            // State + StreamSender, no args
            quote! {
                #(#fn_attrs)*
                #fn_vis #fn_asyncness fn #fn_name(#(#state_names: #state_types),*, #stream_ident: #stream_ty) -> #return_type
                #fn_body
            }
        } else {
            // State + Args + StreamSender
            quote! {
                #(#fn_attrs)*
                #fn_vis #fn_asyncness fn #fn_name(#(#state_names: #state_types),*, args: #struct_name, #stream_ident: #stream_ty) -> #return_type {
                    let #struct_name { #(#field_names),* } = args;
                    #fn_body
                }
            }
        }
    } else if state_params.is_empty() && arg_params.is_empty() {
        // No args, no state
        quote! {
            #(#fn_attrs)*
            #fn_vis #fn_asyncness fn #fn_name() -> #return_type
            #fn_body
        }
    } else if state_params.is_empty() {
        // Args only, no state
        quote! {
            #(#fn_attrs)*
            #fn_vis #fn_asyncness fn #fn_name(args: #struct_name) -> #return_type {
                let #struct_name { #(#field_names),* } = args;
                #fn_body
            }
        }
    } else if arg_params.is_empty() {
        // State only, no args
        quote! {
            #(#fn_attrs)*
            #fn_vis #fn_asyncness fn #fn_name(#(#state_names: #state_types),*) -> #return_type
            #fn_body
        }
    } else {
        // Both state and args
        quote! {
            #(#fn_attrs)*
            #fn_vis #fn_asyncness fn #fn_name(#(#state_names: #state_types),*, args: #struct_name) -> #return_type {
                let #struct_name { #(#field_names),* } = args;
                #fn_body
            }
        }
    };

    Ok(quote! {
        #args_struct
        #generated_fn
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_to_pascal_case() {
        assert_eq!(to_pascal_case("greet"), "Greet");
        assert_eq!(to_pascal_case("get_user"), "GetUser");
        assert_eq!(to_pascal_case("create_new_item"), "CreateNewItem");
    }

    #[test]
    fn test_is_stream_sender_type() {
        let ty: Type = syn::parse_str("StreamSender").unwrap();
        assert!(is_stream_sender_type(&ty));

        let ty: Type = syn::parse_str("String").unwrap();
        assert!(!is_stream_sender_type(&ty));
    }

    #[test]
    fn test_has_streaming_attr() {
        let attr: TokenStream = syn::parse_str("streaming").unwrap();
        assert!(has_streaming_attr(&attr));

        let attr: TokenStream = syn::parse_str("").unwrap();
        assert!(!has_streaming_attr(&attr));
    }

    #[test]
    fn test_streaming_basic() {
        let attr: TokenStream = syn::parse_str("streaming").unwrap();
        let item: TokenStream = syn::parse_str(
            r#"
            async fn stream_chat(prompt: String, model: String, tx: StreamSender) -> String {
                format!("{} {}", prompt, model)
            }
            "#,
        )
        .unwrap();

        let result = tauri_compat_impl(attr, item).unwrap();
        let output = result.to_string();

        // Should generate StreamChatArgs with prompt and model (not tx)
        assert!(output.contains("StreamChatArgs"));
        assert!(output.contains("prompt"));
        assert!(output.contains("model"));
        // StreamSender should appear in the function signature, not the struct
        assert!(output.contains("tx : StreamSender"));
        // The args struct should NOT contain StreamSender
        // Count occurrences: StreamSender should only appear in fn sig
        let struct_end = output.find("stream_chat").unwrap();
        let struct_part = &output[..struct_end];
        assert!(!struct_part.contains("StreamSender"));
    }

    #[test]
    fn test_streaming_with_state() {
        let attr: TokenStream = syn::parse_str("streaming").unwrap();
        let item: TokenStream = syn::parse_str(
            r#"
            async fn stream_updates(state: State<AppState>, count: u32, tx: StreamSender) -> String {
                format!("{}", count)
            }
            "#,
        )
        .unwrap();

        let result = tauri_compat_impl(attr, item).unwrap();
        let output = result.to_string();

        assert!(output.contains("StreamUpdatesArgs"));
        assert!(output.contains("count"));
        // State should be first, then args, then StreamSender
        let fn_part = &output[output.find("stream_updates").unwrap()..];
        let state_pos = fn_part.find("state").unwrap();
        let args_pos = fn_part.find("args").unwrap();
        let tx_pos = fn_part.find("tx").unwrap();
        assert!(state_pos < args_pos);
        assert!(args_pos < tx_pos);
    }

    #[test]
    fn test_streaming_no_stream_sender_error() {
        let attr: TokenStream = syn::parse_str("streaming").unwrap();
        let item: TokenStream = syn::parse_str(
            r#"
            async fn bad_stream(prompt: String) -> String {
                prompt
            }
            "#,
        )
        .unwrap();

        let result = tauri_compat_impl(attr, item);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("StreamSender"));
    }

    #[test]
    fn test_streaming_only_stream_sender() {
        let attr: TokenStream = syn::parse_str("streaming").unwrap();
        let item: TokenStream = syn::parse_str(
            r#"
            async fn stream_all(tx: StreamSender) -> String {
                "done".to_string()
            }
            "#,
        )
        .unwrap();

        let result = tauri_compat_impl(attr, item).unwrap();
        let output = result.to_string();

        // Should have an empty args struct and fn taking only StreamSender
        assert!(output.contains("StreamAllArgs"));
        assert!(output.contains("tx : StreamSender"));
    }

    #[test]
    fn test_streaming_state_and_stream_sender_no_args() {
        let attr: TokenStream = syn::parse_str("streaming").unwrap();
        let item: TokenStream = syn::parse_str(
            r#"
            async fn stream_state(state: State<AppState>, tx: StreamSender) -> String {
                "done".to_string()
            }
            "#,
        )
        .unwrap();

        let result = tauri_compat_impl(attr, item).unwrap();
        let output = result.to_string();

        assert!(output.contains("StreamStateArgs"));
        // Function should have state and tx but no args param
        let fn_part = &output[output.find("stream_state").unwrap()..];
        assert!(fn_part.contains("state : State"));
        assert!(fn_part.contains("tx : StreamSender"));
    }

    #[test]
    fn test_non_streaming_unchanged() {
        // Ensure that a non-streaming call still works the same way
        let attr: TokenStream = syn::parse_str("").unwrap();
        let item: TokenStream = syn::parse_str(
            r#"
            async fn greet(name: String) -> String {
                name
            }
            "#,
        )
        .unwrap();

        let result = tauri_compat_impl(attr, item).unwrap();
        let output = result.to_string();

        assert!(output.contains("GreetArgs"));
        assert!(output.contains("name"));
        assert!(!output.contains("StreamSender"));
    }
}