dioxus-provider-macros 0.2.1

Procedural macros for dioxus-provider - declarative data fetching and caching for Dioxus applications
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
#![allow(unused_variables)] // Variables used in quote! macros aren't detected by compiler

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use std::time::Duration;
use syn::{
    FnArg, ItemFn, LitStr, Pat, PatType, Result, ReturnType, Token, Type, parse::Parse,
    parse::ParseStream, parse_macro_input,
};

/// Attribute arguments for the provider macro
#[derive(Default)]
struct ProviderArgs {
    interval: Option<Duration>,
    cache_expiration: Option<Duration>,
    stale_time: Option<Duration>,
    compose: Vec<syn::Ident>, // List of provider functions to compose
}

/// Attribute arguments for the mutation macro
#[derive(Default)]
struct MutationArgs {
    invalidates: Vec<syn::Ident>, // List of provider functions to invalidate
    optimistic: Option<syn::ExprClosure>, // Optimistic closure applied to cached data
}

impl Parse for ProviderArgs {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut args = ProviderArgs::default();

        while !input.is_empty() {
            let ident: syn::Ident = input.parse()?;
            input.parse::<Token![=]>()?;

            match ident.to_string().as_str() {
                "interval" => {
                    let lit: LitStr = input.parse()?;
                    let duration_str = lit.value();
                    let duration = humantime::parse_duration(&duration_str).map_err(|e| {
                        syn::Error::new_spanned(lit, format!("Invalid duration format: {e}"))
                    })?;
                    args.interval = Some(duration);
                }
                "cache_expiration" => {
                    let lit: LitStr = input.parse()?;
                    let duration_str = lit.value();
                    let duration = humantime::parse_duration(&duration_str).map_err(|e| {
                        syn::Error::new_spanned(lit, format!("Invalid duration format: {e}"))
                    })?;
                    args.cache_expiration = Some(duration);
                }
                "stale_time" => {
                    let lit: LitStr = input.parse()?;
                    let duration_str = lit.value();
                    let duration = humantime::parse_duration(&duration_str).map_err(|e| {
                        syn::Error::new_spanned(lit, format!("Invalid duration format: {e}"))
                    })?;
                    args.stale_time = Some(duration);
                }
                "compose" => {
                    // Parse compose list: compose = [provider1, provider2, ...]
                    let content;
                    syn::bracketed!(content in input);
                    let providers = content.parse_terminated(syn::Ident::parse, Token![,])?;
                    args.compose = providers.into_iter().collect();
                }
                _ => return Err(syn::Error::new_spanned(ident, "Unknown argument")),
            }

            if input.peek(Token![,]) {
                input.parse::<Token![,]>()?;
            }
        }

        Ok(args)
    }
}

impl Parse for MutationArgs {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut args = MutationArgs::default();

        while !input.is_empty() {
            let ident: syn::Ident = input.parse()?;
            input.parse::<Token![=]>()?;

            match ident.to_string().as_str() {
                "invalidates" => {
                    // Parse invalidation list: invalidates = [provider1, provider2, ...]
                    let content;
                    syn::bracketed!(content in input);
                    let providers = content.parse_terminated(syn::Ident::parse, Token![,])?;
                    args.invalidates = providers.into_iter().collect();
                }
                "optimistic" => {
                    let expr: syn::ExprClosure = input.parse()?;
                    args.optimistic = Some(expr);
                }
                _ => return Err(syn::Error::new_spanned(ident, "Unknown argument")),
            }

            if input.peek(Token![,]) {
                input.parse::<Token![,]>()?;
            }
        }

        Ok(args)
    }
}

/// Provider macro for creating cached, composable data providers
///
/// This macro converts an async function into a Provider implementation with
/// automatic caching, composition, and other advanced features.
///
/// # Supported Arguments
/// - `interval = "30s"` - Background refresh interval
/// - `cache_expiration = "5min"` - Cache expiration time  
/// - `stale_time = "1min"` - Time before data is considered stale
/// - `compose = [provider1, provider2, ...]` - Compose multiple providers in parallel
///
/// # Composition Requirements
/// When using `compose = [...]`, the following requirements must be met:
///
/// ## Parameter Clone Requirements
/// **All function parameters MUST implement `Clone`** when using composition.
/// Parameters are cloned inside async blocks to enable parallel execution.
///
/// ```rust
/// // ✅ Good - u32 implements Clone
/// #[provider(compose = [fetch_permissions])]
/// async fn fetch_user_profile(user_id: u32) -> Result<Profile, Error> {
///     // fetch_permissions_result is available here
/// }
///
/// // ❌ Bad - non-Clone parameter
/// #[provider(compose = [fetch_permissions])]
/// async fn fetch_user_profile(config: NonCloneConfig) -> Result<Profile, Error> {
///     // This will cause a compile error
/// }
///
/// // ✅ Solution - Add #[derive(Clone)] to your types
/// #[derive(Clone)]
/// struct UserConfig { /* fields */ }
///
/// #[provider(compose = [fetch_permissions])]
/// async fn fetch_user_profile(config: UserConfig) -> Result<Profile, Error> {
///     // Now this works
/// }
/// ```
///
/// ## Provider Existence Validation
/// All providers listed in `compose = [...]` must:
/// - Be valid Rust identifiers
/// - Exist in the current scope when the macro is expanded
/// - Have compatible signatures (same parameter types)
///
/// The macro generates compile-time calls to verify provider existence and
/// provides clear error messages if providers are not found.
///
/// # Examples
/// ```rust
/// #[provider(cache_expiration = "5min")]
/// async fn fetch_user(id: u32) -> Result<User, String> {
///     // Implementation
/// }
///
/// #[provider(compose = [fetch_user, fetch_settings], cache_expiration = "3min")]
/// async fn fetch_full_profile(user_id: u32) -> Result<FullProfile, String> {
///     // Composed results automatically available as variables:
///     // - __dioxus_composed_fetch_user_result: Result<User, String>
///     // - __dioxus_composed_fetch_settings_result: Result<Settings, String>
///     let user = __dioxus_composed_fetch_user_result?;
///     let settings = __dioxus_composed_fetch_settings_result?;
///     Ok(FullProfile { user, settings })
/// }
/// ```
///
/// # Compilation Errors
/// The macro provides clear error messages for common issues:
/// - **Clone not implemented**: "Parameter type 'TypeName' must implement Clone for composition"
/// - **Provider not found**: "Composed provider 'provider_name' not found in current scope"
/// - **Signature mismatch**: "Composed provider 'provider_name' has incompatible signature"
#[proc_macro_attribute]
pub fn provider(args: TokenStream, input: TokenStream) -> TokenStream {
    let provider_args = if args.is_empty() {
        ProviderArgs::default()
    } else {
        match syn::parse(args) {
            Ok(args) => args,
            Err(err) => return err.to_compile_error().into(),
        }
    };

    let input_fn = parse_macro_input!(input as ItemFn);

    let result = generate_provider(input_fn, provider_args);

    match result {
        Ok(tokens) => tokens.into(),
        Err(err) => err.to_compile_error().into(),
    }
}

/// Mutation macro for creating data mutations with cache invalidation
///
/// This macro converts an async function into a Mutation implementation that can
/// invalidate related provider caches when executed.
///
/// # Supported Arguments
/// - `invalidates = [provider1, provider2, ...]` - Providers to invalidate after mutation
/// - `optimistic = |data, ...args| { ... }` - Optimistic update closure (requires MutationContext)
///
/// ## Optimistic Updates
/// The optimistic closure receives:
/// - First param: `&mut Data` - mutable reference to current cached data
/// - Remaining params: references to mutation inputs
///
/// Examples:
/// - No args: `optimistic = |data: &mut Vec<Item>| { data.clear() }`
/// - One arg: `optimistic = |data: &mut Vec<Item>, id: &u64| { data.retain(|i| i.id != *id) }`
/// - Multi-arg: `optimistic = |data: &mut Item, name: &String, status: &bool| { data.name = name.clone(); data.active = *status; }`
///
/// ## Return Values
/// Mutation return values serve multiple purposes:
/// - Update `MutationState` for UI feedback (Success/Error)
/// - With optimistic updates: replace cache with server response (avoids refetch)
/// - Without optimistic: cache is invalidated and providers refetch automatically
///
/// # Examples
/// ```rust
/// // Simple mutation with cache invalidation
/// #[mutation(invalidates = [fetch_user, fetch_user_list])]
/// async fn update_user(user: User) -> Result<User, String> {
///     // Update user implementation
///     // Will automatically invalidate fetch_user and fetch_user_list caches
/// }
///
/// // Optimistic mutation with single argument
/// #[mutation(
///     invalidates = [load_items],
///     optimistic = |items: &mut Vec<Item>, id: &u64| {
///         items.retain(|i| i.id != *id)
///     }
/// )]
/// async fn delete_item(
///     id: u64,
///     ctx: MutationContext<Vec<Item>, Error>,
/// ) -> Result<Vec<Item>, Error> {
///     ctx.map_current(|items| items.retain(|i| i.id != id))
///         .ok_or(Error::NoData)
/// }
///
/// // Optimistic mutation with multiple arguments
/// #[mutation(
///     invalidates = [load_items],
///     optimistic = |items: &mut Vec<Item>, id: &u64, name: &String| {
///         if let Some(item) = items.iter_mut().find(|i| i.id == *id) {
///             item.name = name.clone();
///         }
///     }
/// )]
/// async fn update_item(
///     id: u64,
///     name: String,
///     ctx: MutationContext<Vec<Item>, Error>,
/// ) -> Result<Vec<Item>, Error> {
///     ctx.map_current(|items| {
///         if let Some(item) = items.iter_mut().find(|i| i.id == id) {
///             item.name = name;
///         }
///     }).ok_or(Error::NoData)
/// }
/// ```
#[proc_macro_attribute]
pub fn mutation(args: TokenStream, input: TokenStream) -> TokenStream {
    let mutation_args = if args.is_empty() {
        MutationArgs::default()
    } else {
        match syn::parse(args) {
            Ok(args) => args,
            Err(err) => return err.to_compile_error().into(),
        }
    };

    let input_fn = parse_macro_input!(input as ItemFn);

    let result = generate_mutation(input_fn, mutation_args);

    match result {
        Ok(tokens) => tokens.into(),
        Err(err) => err.to_compile_error().into(),
    }
}

fn generate_provider(input_fn: ItemFn, provider_args: ProviderArgs) -> Result<TokenStream2> {
    let info = extract_provider_info(&input_fn)?;

    let ProviderInfo {
        fn_vis,
        fn_block,
        output_type,
        error_type,
        struct_name,
        ..
    } = &info;

    // Extract parameters once
    let params = extract_all_params(&input_fn)?;

    // Validate composition requirements if compose is used
    if !provider_args.compose.is_empty() {
        validate_composition_requirements(&provider_args.compose, &params)?;
    }

    // Generate enhanced function body with dependency injection and composition
    let enhanced_fn_block =
        generate_enhanced_function_body(&provider_args.compose, &params, fn_block);

    // Generate interval and cache expiration implementations
    let interval_impl = generate_interval_impl(&provider_args);
    let cache_expiration_impl = generate_cache_expiration_impl(&provider_args);
    let stale_time_impl = generate_stale_time_impl(&provider_args);

    // Generate common struct and const
    let common_struct = generate_common_struct_and_const(&info);

    // Determine parameter type and implementation based on function parameters
    if params.is_empty() {
        // No parameters - Provider<()>
        Ok(quote! {
            #common_struct

            impl #struct_name {
                #fn_vis async fn call() -> Result<#output_type, #error_type> {
                    #enhanced_fn_block
                }
            }

            impl ::dioxus_provider::hooks::Provider<()> for #struct_name {
                type Output = #output_type;
                type Error = #error_type;

                fn run(&self, _param: ()) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send {
                    Self::call()
                }

                #interval_impl
                #cache_expiration_impl
                #stale_time_impl
            }
        })
    } else if params.len() == 1 {
        // Single parameter - Provider<ParamType>
        let param = &params[0];
        let param_name = &param.name;
        let param_type = &param.ty;

        Ok(quote! {
            #common_struct

            impl #struct_name {
                #fn_vis async fn call(#param_name: #param_type) -> Result<#output_type, #error_type> {
                    #enhanced_fn_block
                }
            }

            impl ::dioxus_provider::hooks::Provider<#param_type> for #struct_name {
                type Output = #output_type;
                type Error = #error_type;

                fn run(&self, #param_name: #param_type) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send {
                    Self::call(#param_name)
                }

                #interval_impl
                #cache_expiration_impl
                #stale_time_impl
            }
        })
    } else {
        // Multiple parameters - Provider<(Param1, Param2, ...)>
        let param_names: Vec<_> = params.iter().map(|p| &p.name).collect();
        let param_types: Vec<_> = params.iter().map(|p| &p.ty).collect();
        let tuple_type = quote! { (#(#param_types,)*) };

        Ok(quote! {
            #common_struct

            impl #struct_name {
                #fn_vis async fn call(#(#param_names: #param_types,)*) -> Result<#output_type, #error_type> {
                    #enhanced_fn_block
                }
            }

            impl ::dioxus_provider::hooks::Provider<#tuple_type> for #struct_name {
                type Output = #output_type;
                type Error = #error_type;

                fn run(&self, params: #tuple_type) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send {
                    let (#(#param_names,)*) = params;
                    Self::call(#(#param_names,)*)
                }

                #interval_impl
                #cache_expiration_impl
                #stale_time_impl
            }
        })
    }
}

fn generate_mutation(input_fn: ItemFn, mutation_args: MutationArgs) -> Result<TokenStream2> {
    let info = extract_provider_info(&input_fn)?;

    let ProviderInfo {
        fn_vis,
        fn_block,
        output_type,
        error_type,
        struct_name,
        fn_name: _fn_name,
        ..
    } = &info;

    let enhanced_fn_block = generate_enhanced_function_body(&[], &[], fn_block);
    let invalidation_impl = generate_invalidation_impl(&mutation_args);
    let common_struct = generate_common_struct_and_const(&info);

    let raw_params = extract_all_params(&input_fn)?;
    let has_optimistic = mutation_args.optimistic.is_some();
    let (input_params, context_param, data_param) =
        split_mutation_params(raw_params.clone(), output_type, has_optimistic)?;

    // Detect auto-apply mode: optimistic is present and there's a data parameter
    let is_auto_apply = has_optimistic && data_param.is_some();

    // Build call parameters based on the original function signature
    let call_params: Vec<_> = raw_params
        .iter()
        .map(|p| {
            let name = &p.name;
            if let Some(ctx) = &context_param && ctx.name == p.name {
                let data_ty = &ctx.data_ty;
                let error_ty = &ctx.error_ty;
                quote! { #name: ::dioxus_provider::mutation::MutationContext<'_, #data_ty, #error_ty> }
            } else {
                let ty = &p.ty;
                quote! { #name: #ty }
            }
        })
        .collect();

    let call_signature = quote! { #fn_vis async fn call(#(#call_params),*) -> Result<#output_type, #error_type> {
        #enhanced_fn_block
    } };

    let input_count = input_params.len();
    let input_type = build_input_type(&input_params);

    let data_param_name = data_param.as_ref().map(|p| &p.name);

    let call_args_builder = |ctx_ident: Option<&syn::Ident>,
                             auto_apply_data_expr: Option<TokenStream2>|
     -> Vec<TokenStream2> {
        raw_params
            .iter()
            .map(|param| {
                // If this is the context parameter, use the ctx_ident
                if let Some(ctx) = ctx_ident {
                    if param.name == *ctx {
                        return quote! { #ctx };
                    }
                }
                // If this is the data parameter and we have auto-applied data, use that
                if let Some(data_name) = data_param_name {
                    if param.name == *data_name {
                        if let Some(ref data_expr) = auto_apply_data_expr {
                            return data_expr.clone();
                        }
                    }
                }
                // Otherwise, use the parameter name as-is
                let name = &param.name;
                quote! { #name }
            })
            .collect()
    };

    let context_ident = context_param.as_ref().map(|ctx| ctx.name.clone());
    let context_data_ty = context_param.as_ref().map(|ctx| ctx.data_ty.clone());
    let context_error_ty = context_param.as_ref().map(|ctx| ctx.error_ty.clone());

    let optimistic_impl = if let Some(optimistic_expr) = &mutation_args.optimistic {
        // Generate the call to optimistic closure based on param count
        let optimistic_call = match input_params.len() {
            0 => quote! { (#optimistic_expr)(&mut updated) },
            1 => quote! { (#optimistic_expr)(&mut updated, input) },
            _ => {
                let names: Vec<_> = input_params.iter().map(|p| &p.name).collect();
                quote! {
                    let (#(ref #names,)*) = *input;
                    (#optimistic_expr)(&mut updated, #(#names,)*)
                }
            }
        };

        quote! {
            fn optimistic_updates_with_current(
                &self,
                input: &#input_type,
                current_data: Option<&Result<Self::Output, Self::Error>>,
            ) -> Vec<(String, Result<Self::Output, Self::Error>)> {
                let keys = self.invalidates();
                if keys.is_empty() {
                    return Vec::new();
                }

                if let Some(Ok(current)) = current_data {
                    let mut updated = current.clone();
                    #optimistic_call;

                    let mut results = Vec::with_capacity(keys.len());
                    for key in keys {
                        results.push((key, Ok(updated.clone())));
                    }
                    results
                } else {
                    Vec::new()
                }
            }
        }
    } else {
        quote! {}
    };

    let (mutate_signature, mutate_body) = {
        let (signature, mut prelude) = match input_count {
            0 => (
                quote! { fn mutate(&self, _input: ()) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send },
                Vec::<TokenStream2>::new(),
            ),
            1 => {
                let param = &input_params[0];
                let name = &param.name;
                let ty = &param.ty;
                (
                    quote! { fn mutate(&self, #name: #ty) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send },
                    Vec::<TokenStream2>::new(),
                )
            }
            _ => {
                let names: Vec<_> = input_params.iter().map(|p| &p.name).collect();
                (
                    quote! { fn mutate(&self, input: #input_type) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send },
                    vec![quote! { let (#(#names),*) = input; }],
                )
            }
        };

        // Create MutationContext if needed in manual mode
        if !is_auto_apply {
            if let (Some(ctx_ident), Some(data_ty), Some(err_ty)) = (
                context_ident.as_ref(),
                context_data_ty.as_ref(),
                context_error_ty.as_ref(),
            ) {
                prelude.push(quote! { let #ctx_ident = ::dioxus_provider::mutation::MutationContext::<'static, #data_ty, #err_ty>::new(None); });
            }
        }

        let call_args = if is_auto_apply {
            // Auto-apply mode: provide default data (rarely called - should use mutate_with_current)
            call_args_builder(None, Some(quote! { Default::default() }))
        } else {
            // Manual mode: use context if present
            call_args_builder(context_ident.as_ref(), None)
        };

        let call_expr = quote! { Self::call(#(#call_args),*) };
        let body = quote! { async move { #(#prelude)* #call_expr.await } };
        (signature, body)
    };

    let (mutate_with_current_signature, mutate_with_current_body) = {
        let (signature, mut prelude) = match input_count {
            0 => (
                quote! { fn mutate_with_current(
                    &self,
                    _input: (),
                    current_data: Option<&Result<Self::Output, Self::Error>>,
                ) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send },
                Vec::<TokenStream2>::new(),
            ),
            1 => {
                let param = &input_params[0];
                let name = &param.name;
                let ty = &param.ty;
                (
                    quote! { fn mutate_with_current(
                        &self,
                        #name: #ty,
                        current_data: Option<&Result<Self::Output, Self::Error>>,
                    ) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send },
                    Vec::<TokenStream2>::new(),
                )
            }
            _ => {
                let names: Vec<_> = input_params.iter().map(|p| &p.name).collect();
                (
                    quote! { fn mutate_with_current(
                        &self,
                        input: #input_type,
                        current_data: Option<&Result<Self::Output, Self::Error>>,
                    ) -> impl ::std::future::Future<Output = Result<Self::Output, Self::Error>> + Send },
                    vec![quote! { let (#(#names),*) = input; }],
                )
            }
        };

        let call_args = if is_auto_apply {
            // Auto-apply mode: use current_data directly (already has optimistic update applied by runtime)
            // DO NOT re-apply the optimistic closure here - that would cause double-application!
            prelude.push(quote! {
                let __auto_apply_data = if let Some(Ok(current)) = current_data {
                    current.clone()
                } else {
                    // If no current data, use default
                    Default::default()
                };
            });

            call_args_builder(None, Some(quote! { __auto_apply_data }))
        } else {
            // Manual mode: create MutationContext from current_data
            if let Some(ctx_ident) = context_ident.as_ref() {
                prelude.push(quote! { let #ctx_ident = ::dioxus_provider::mutation::MutationContext::new(current_data); });
            }
            call_args_builder(context_ident.as_ref(), None)
        };

        let call_expr = quote! { Self::call(#(#call_args),*) };
        let body = quote! { async move { #(#prelude)* #call_expr.await } };
        (signature, body)
    };

    let has_optimistic_impl = if has_optimistic {
        quote! {
            fn has_optimistic(&self) -> bool {
                true
            }
        }
    } else {
        quote! {}
    };

    let mutation_impl = quote! {
        impl ::dioxus_provider::mutation::Mutation<#input_type> for #struct_name {
            type Output = #output_type;
            type Error = #error_type;

            #mutate_signature {
                #mutate_body
            }

            #mutate_with_current_signature {
                #mutate_with_current_body
            }

            #optimistic_impl

            #invalidation_impl

            #has_optimistic_impl
        }
    };

    Ok(quote! {
        #common_struct

        impl #struct_name {
            #call_signature
        }

        #mutation_impl
    })
}

/// Generate duration implementation for provider methods
fn generate_duration_impl(method_name: &str, duration: Option<Duration>) -> TokenStream2 {
    if let Some(duration) = duration {
        let duration_secs = duration.as_secs();
        let method_ident = syn::Ident::new(method_name, proc_macro2::Span::call_site());

        quote! {
            fn #method_ident(&self) -> Option<::std::time::Duration> {
                Some(::std::time::Duration::from_secs(#duration_secs))
            }
        }
    } else {
        quote! {}
    }
}

/// Generate interval implementation
fn generate_interval_impl(provider_args: &ProviderArgs) -> TokenStream2 {
    generate_duration_impl("interval", provider_args.interval)
}

/// Generate cache expiration implementation
fn generate_cache_expiration_impl(provider_args: &ProviderArgs) -> TokenStream2 {
    generate_duration_impl("cache_expiration", provider_args.cache_expiration)
}

/// Generate stale time implementation
fn generate_stale_time_impl(provider_args: &ProviderArgs) -> TokenStream2 {
    generate_duration_impl("stale_time", provider_args.stale_time)
}

/// Generate invalidation implementation for mutations
fn generate_invalidation_impl(mutation_args: &MutationArgs) -> TokenStream2 {
    if mutation_args.invalidates.is_empty() {
        quote! {}
    } else {
        let provider_calls: Vec<_> = mutation_args
            .invalidates
            .iter()
            .map(|provider_fn| {
                quote! {
                    ::dioxus_provider::mutation::provider_cache_key_simple(#provider_fn())
                }
            })
            .collect();

        quote! {
            fn invalidates(&self) -> Vec<String> {
                vec![#(#provider_calls,)*]
            }
        }
    }
}

/// Information extracted from the provider function
struct ProviderInfo {
    fn_vis: syn::Visibility,
    fn_attrs: Vec<syn::Attribute>,
    fn_block: Box<syn::Block>,
    output_type: Type,
    error_type: Type,
    struct_name: syn::Ident,
    fn_name: syn::Ident,
}

/// Information about a function parameter
#[derive(Clone)]
struct ParamInfo {
    name: syn::Ident,
    ty: Type,
}

#[derive(Clone)]
struct ContextInfo {
    name: syn::Ident,
    data_ty: Type,
    error_ty: Type,
}

fn parse_context_type(ty: &Type) -> Option<(Type, Type)> {
    if let Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.last() {
            if segment.ident == "MutationContext" {
                if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                    if args.args.len() == 2 {
                        let mut iter = args.args.iter();
                        let data_ty = match iter.next()? {
                            syn::GenericArgument::Type(ty) => ty.clone(),
                            _ => return None,
                        };
                        let error_ty = match iter.next()? {
                            syn::GenericArgument::Type(ty) => ty.clone(),
                            _ => return None,
                        };
                        return Some((data_ty, error_ty));
                    }
                }
            }
        }
    }
    None
}

#[allow(dead_code)]
fn split_params(params: Vec<ParamInfo>) -> Result<(Vec<ParamInfo>, Option<ContextInfo>)> {
    let mut input_params = Vec::new();
    let mut context_param = None;

    for param in params {
        if let Some((data_ty, error_ty)) = parse_context_type(&param.ty) {
            if context_param.is_some() {
                return Err(syn::Error::new_spanned(
                    param.ty,
                    "Only one MutationContext parameter is allowed",
                ));
            }
            context_param = Some(ContextInfo {
                name: param.name,
                data_ty,
                error_ty,
            });
        } else {
            input_params.push(param);
        }
    }

    Ok((input_params, context_param))
}

/// Split mutation parameters into input params, context param, and auto-apply data param
fn split_mutation_params(
    params: Vec<ParamInfo>,
    output_type: &Type,
    has_optimistic: bool,
) -> Result<(Vec<ParamInfo>, Option<ContextInfo>, Option<ParamInfo>)> {
    let mut input_params = Vec::new();
    let mut context_param = None;
    let mut data_param = None;

    for param in params {
        if let Some((data_ty, error_ty)) = parse_context_type(&param.ty) {
            if context_param.is_some() {
                return Err(syn::Error::new_spanned(
                    param.ty,
                    "Only one MutationContext parameter is allowed",
                ));
            }
            context_param = Some(ContextInfo {
                name: param.name,
                data_ty,
                error_ty,
            });
        } else {
            input_params.push(param);
        }
    }

    // In auto-apply mode (has optimistic but no context), the last param might be the data param
    if has_optimistic && context_param.is_none() && !input_params.is_empty() {
        // Check if the last parameter's type matches the output type
        if let Some(last_param) = input_params.last() {
            if types_equal(&last_param.ty, output_type) {
                data_param = input_params.pop();
            }
        }
    }

    Ok((input_params, context_param, data_param))
}

/// Compare two types for structural equality
fn types_equal(ty1: &Type, ty2: &Type) -> bool {
    ty1 == ty2
}

/// Extract provider information from the input function
fn extract_provider_info(input_fn: &ItemFn) -> Result<ProviderInfo> {
    let fn_name = input_fn.sig.ident.clone();
    let fn_vis = input_fn.vis.clone();
    let fn_attrs = input_fn.attrs.clone();
    let fn_block = input_fn.block.clone();

    let (output_type, error_type) = extract_result_types(&input_fn.sig.output)?;
    let struct_name = syn::Ident::new(
        &to_pascal_case(&fn_name.to_string()),
        proc_macro2::Span::call_site(),
    );

    Ok(ProviderInfo {
        fn_vis,
        fn_attrs,
        fn_block,
        output_type,
        error_type,
        struct_name,
        fn_name,
    })
}

/// Generate common struct and const for the provider
fn generate_common_struct_and_const(info: &ProviderInfo) -> TokenStream2 {
    let struct_name = &info.struct_name;
    let fn_attrs = &info.fn_attrs;
    let fn_name = &info.fn_name;

    quote! {
        #[derive(Clone, PartialEq)]
        #(#fn_attrs)*
        pub struct #struct_name;

        impl Default for #struct_name {
            fn default() -> Self {
                Self
            }
        }

        // Generate a function that returns an instance of the struct
        pub fn #fn_name() -> #struct_name {
            #struct_name
        }
    }
}

/// Extract all parameters from the function signature
fn extract_all_params(input_fn: &ItemFn) -> Result<Vec<ParamInfo>> {
    let mut params = Vec::new();

    for input in &input_fn.sig.inputs {
        match input {
            FnArg::Typed(PatType { pat, ty, .. }) => {
                if let Pat::Ident(pat_ident) = &**pat {
                    params.push(ParamInfo {
                        name: pat_ident.ident.clone(),
                        ty: (**ty).clone(),
                    });
                } else {
                    return Err(syn::Error::new_spanned(
                        pat,
                        "Only simple parameter names are supported",
                    ));
                }
            }
            FnArg::Receiver(_) => {
                return Err(syn::Error::new_spanned(
                    input,
                    "Methods with self parameter are not supported",
                ));
            }
        }
    }

    Ok(params)
}

/// Build the input type: () for 0 params, T for 1 param, (T1, T2, ...) for N params
fn build_input_type(params: &[ParamInfo]) -> TokenStream2 {
    match params.len() {
        0 => quote! { () },
        1 => {
            let ty = &params[0].ty;
            quote! { #ty }
        }
        _ => {
            let types: Vec<_> = params.iter().map(|p| &p.ty).collect();
            quote! { (#(#types,)*) }
        }
    }
}

/// Extract result types from the function return type
fn extract_result_types(return_type: &ReturnType) -> Result<(Type, Type)> {
    match return_type {
        ReturnType::Default => Err(syn::Error::new_spanned(
            return_type,
            "Provider functions must return Result<T, E>",
        )),
        ReturnType::Type(_, ty) => {
            if let Type::Path(type_path) = &**ty {
                if let Some(segment) = type_path.path.segments.last() {
                    if segment.ident == "Result" {
                        if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                            if args.args.len() == 2 {
                                let mut args_iter = args.args.iter();

                                let output_type = match args_iter.next().unwrap() {
                                    syn::GenericArgument::Type(ty) => ty.clone(),
                                    _ => {
                                        return Err(syn::Error::new_spanned(
                                            args,
                                            "Result must have type arguments",
                                        ));
                                    }
                                };

                                let error_type = match args_iter.next().unwrap() {
                                    syn::GenericArgument::Type(ty) => ty.clone(),
                                    _ => {
                                        return Err(syn::Error::new_spanned(
                                            args,
                                            "Result must have type arguments",
                                        ));
                                    }
                                };

                                return Ok((output_type, error_type));
                            }
                        }
                    }
                }
            }

            Err(syn::Error::new_spanned(
                return_type,
                "Provider functions must return Result<T, E>",
            ))
        }
    }
}

/// Convert a string to PascalCase
fn to_pascal_case(s: &str) -> String {
    let mut result = String::new();
    let mut capitalize_next = true;

    for c in s.chars() {
        if c == '_' {
            capitalize_next = true;
        } else if capitalize_next {
            result.push(c.to_ascii_uppercase());
            capitalize_next = false;
        } else {
            result.push(c);
        }
    }

    result
}

/// Validate composition requirements for compose providers
fn validate_composition_requirements(
    compose_providers: &[syn::Ident],
    params: &[ParamInfo],
) -> Result<()> {
    // Validate that all parameters implement Clone when composition is used
    if !params.is_empty() {
        validate_clone_requirements(params)?;
    }

    // Validate that composed providers exist (generates compile-time checks)
    validate_provider_existence(compose_providers)?;

    Ok(())
}

/// Validate that all parameters implement Clone for composition
fn validate_clone_requirements(params: &[ParamInfo]) -> Result<()> {
    for param in params {
        let param_type = &param.ty;
        let param_name = &param.name;

        // Generate a compile-time assertion that the type implements Clone
        // This will be added to the generated code to provide clear error messages
        let _clone_check = quote! {
            const _: fn() = || {
                fn assert_clone<T: Clone>() {}
                assert_clone::<#param_type>();
            };
        };

        // Note: The actual Clone validation happens at compile-time when the generated
        // code tries to clone the parameters. The error message will be improved by
        // the explicit clone calls we generate in generate_composition_statements_with_validation.
    }

    Ok(())
}

/// Validate that composed providers exist by generating compile-time checks
fn validate_provider_existence(compose_providers: &[syn::Ident]) -> Result<()> {
    // We can't fully validate provider existence at macro expansion time,
    // but we can generate code that will provide better error messages
    // if the providers don't exist or have incompatible signatures.

    for provider in compose_providers {
        // Generate a compile-time check that will give a clear error if the provider doesn't exist
        let _existence_check = quote! {
            const _: fn() = || {
                // This will cause a compile error with a clear message if the provider doesn't exist
                let _ = #provider;
            };
        };
    }

    Ok(())
}

/// Generate enhanced function body with composition
fn generate_enhanced_function_body(
    compose_providers: &[syn::Ident],
    params: &[ParamInfo],
    original_block: &syn::Block,
) -> syn::Block {
    let mut statements = Vec::new();

    // Add composition statements
    if !compose_providers.is_empty() {
        let composition_statements = generate_composition_statements(compose_providers, params);
        statements.extend(composition_statements);
    }

    // Add original function body statements
    statements.extend(original_block.stmts.clone());

    syn::Block {
        brace_token: original_block.brace_token,
        stmts: statements,
    }
}

/// Generate composition statements that can be directly added to a statement list
fn generate_composition_statements(
    compose_providers: &[syn::Ident],
    params: &[ParamInfo],
) -> Vec<syn::Stmt> {
    if compose_providers.is_empty() {
        return vec![];
    }

    let mut statements = Vec::new();

    // Add compile-time validation checks for better error messages
    statements.extend(generate_validation_statements(compose_providers, params));

    // Generate variable names for composed results with unique prefix to avoid collisions
    let result_vars: Vec<_> = compose_providers
        .iter()
        .map(|provider| {
            syn::Ident::new(
                &format!("__dioxus_composed_{provider}_result"),
                proc_macro2::Span::call_site(),
            )
        })
        .collect();

    // Generate provider calls based on parameter count
    if params.is_empty() {
        // No parameters - call providers with ()
        let provider_calls: Vec<_> = compose_providers
            .iter()
            .map(|provider| {
                quote! {
                    async { #provider().run(()).await }
                }
            })
            .collect();

        let join_stmt: syn::Stmt = syn::parse_quote! {
            let (#(#result_vars,)*) = ::futures::join!(
                #(#provider_calls,)*
            );
        };
        statements.push(join_stmt);
    } else if params.len() == 1 {
        // Single parameter - clone it inside each async block
        let param_name = &params[0].name;
        let param_type = &params[0].ty;

        let provider_calls: Vec<_> = compose_providers
            .iter()
            .map(|provider| {
                quote! {
                    async {
                        // Explicit clone with helpful error context
                        let param: #param_type = #param_name.clone();
                        #provider().run(param).await
                    }
                }
            })
            .collect();

        let join_stmt: syn::Stmt = syn::parse_quote! {
            let (#(#result_vars,)*) = ::futures::join!(
                #(#provider_calls,)*
            );
        };
        statements.push(join_stmt);
    } else {
        // Multiple parameters - clone each parameter inside each async block
        let param_names: Vec<_> = params.iter().map(|p| &p.name).collect();
        let param_types: Vec<_> = params.iter().map(|p| &p.ty).collect();

        let provider_calls: Vec<_> = compose_providers
            .iter()
            .map(|provider| {
                quote! {
                    async {
                        // Explicit clone with helpful error context for each parameter
                        let params: (#(#param_types,)*) = (#(#param_names.clone(),)*);
                        #provider().run(params).await
                    }
                }
            })
            .collect();

        let join_stmt: syn::Stmt = syn::parse_quote! {
            let (#(#result_vars,)*) = ::futures::join!(
                #(#provider_calls,)*
            );
        };
        statements.push(join_stmt);
    }

    statements
}

/// Generate compile-time validation statements for better error messages
fn generate_validation_statements(
    compose_providers: &[syn::Ident],
    params: &[ParamInfo],
) -> Vec<syn::Stmt> {
    let mut statements = Vec::new();

    // Add Clone validation for parameters if composition is used
    if !params.is_empty() {
        for param in params {
            let param_type = &param.ty;
            let param_name = &param.name;

            // Generate a compile-time Clone assertion with helpful error message
            let clone_check: syn::Stmt = syn::parse_quote! {
                const _: () = {
                    fn __dioxus_provider_assert_clone<T: ::std::clone::Clone>() {}
                    fn __dioxus_provider_validate_parameter_clone() {
                        __dioxus_provider_assert_clone::<#param_type>();
                    }
                };
            };
            statements.push(clone_check);
        }
    }

    // Add provider existence validation
    for provider in compose_providers {
        // Generate a compile-time check that the provider exists and is callable
        let existence_check: syn::Stmt = syn::parse_quote! {
            const _: () = {
                fn __dioxus_provider_validate_existence() {
                    // This will cause a clear compile error if the provider doesn't exist
                    let _provider_exists = #provider;
                }
            };
        };
        statements.push(existence_check);
    }

    statements
}