masterror-derive 0.11.3

Derive macros for masterror
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
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
//
// SPDX-License-Identifier: MIT

//! Display implementation generation for enum error types.
//!
//! This module handles the generation of `Display` trait implementations for
//! enum-based error types. It creates a match expression with an arm for each
//! variant, supporting transparent delegation, template-based formatting, and
//! custom formatter functions per variant.

use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::Error;

use super::{
    format_args::FormatArgumentsEnv,
    formatter::needs_pointer_value,
    placeholder::ResolvedPlaceholderExpr,
    struct_impl::{binding_ident, formatter_path_call},
    template::render_template
};
use crate::{
    input::{
        DisplaySpec, ErrorInput, Field, Fields, FormatArgsSpec, VariantData, placeholder_error
    },
    lint::lifetime_lint_allows,
    template_support::{DisplayTemplate, TemplateIdentifierSpec}
};

/// Generates the Display trait implementation for an enum error type.
///
/// Creates a Display implementation with a match expression that handles
/// each variant according to its display specification.
///
/// # Arguments
///
/// * `input` - The error type input with generics and metadata
/// * `variants` - The enum variants with their display specifications
///
/// # Returns
///
/// Token stream containing the complete Display trait implementation
pub fn expand_enum(input: &ErrorInput, variants: &[VariantData]) -> Result<TokenStream, Error> {
    let mut arms = Vec::new();
    for variant in variants {
        arms.push(render_variant(variant)?);
    }
    let ident = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    let lint_allows = lifetime_lint_allows(&input.generics);
    Ok(quote! {
        #lint_allows
        impl #impl_generics core::fmt::Display for #ident #ty_generics #where_clause {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                match self {
                    #(#arms),*
                }
            }
        }
    })
}

/// Renders a single match arm for an enum variant.
///
/// Dispatches to the appropriate rendering function based on the variant's
/// display specification (transparent, template, or formatter path).
///
/// # Arguments
///
/// * `variant` - The variant data with display specification
///
/// # Returns
///
/// Token stream containing the match arm for this variant
pub fn render_variant(variant: &VariantData) -> Result<TokenStream, Error> {
    match &variant.display {
        DisplaySpec::Transparent {
            ..
        } => render_variant_transparent(variant),
        DisplaySpec::Template(template) => render_variant_template(variant, template, None),
        DisplaySpec::TemplateWithArgs {
            template,
            args
        } => render_variant_template(variant, template, Some(args)),
        DisplaySpec::FormatterPath {
            path, ..
        } => render_variant_formatter_path(variant, path)
    }
}

/// Renders a transparent display match arm for a variant.
///
/// For transparent variants, the Display implementation delegates to the
/// single field. Returns an error if the variant doesn't have exactly one
/// field.
///
/// # Arguments
///
/// * `variant` - The variant data
///
/// # Returns
///
/// Token stream containing the match arm with Display delegation
pub fn render_variant_transparent(variant: &VariantData) -> Result<TokenStream, Error> {
    let variant_ident = &variant.ident;
    match &variant.fields {
        Fields::Unit => Err(Error::new(
            variant.span,
            "#[error(transparent)] requires exactly one field"
        )),
        Fields::Named(fields) | Fields::Unnamed(fields) => {
            if fields.len() != 1 {
                return Err(Error::new(
                    variant.span,
                    "#[error(transparent)] requires exactly one field"
                ));
            }
            let binding = binding_ident(&fields[0]);
            let pattern = match &variant.fields {
                Fields::Named(_) => {
                    let field_ident = fields[0].ident.clone().expect("named field");
                    quote!(Self::#variant_ident { #field_ident: #binding })
                }
                Fields::Unnamed(_) => {
                    quote!(Self::#variant_ident(#binding))
                }
                Fields::Unit => unreachable!()
            };
            Ok(quote! {
                #pattern => core::fmt::Display::fmt(#binding, f)
            })
        }
    }
}

/// Renders a match arm for a variant using a custom formatter function.
///
/// # Arguments
///
/// * `variant` - The variant data
/// * `path` - The path to the custom formatter function
///
/// # Returns
///
/// Token stream containing the match arm with formatter function call
pub fn render_variant_formatter_path(
    variant: &VariantData,
    path: &syn::ExprPath
) -> Result<TokenStream, Error> {
    let variant_ident = &variant.ident;
    match &variant.fields {
        Fields::Unit => {
            let call = formatter_path_call(path, Vec::new());
            Ok(quote! {
                Self::#variant_ident => {
                    #call
                }
            })
        }
        Fields::Unnamed(fields) => {
            let bindings: Vec<_> = fields.iter().map(binding_ident).collect();
            let pattern = quote!(Self::#variant_ident(#(#bindings),*));
            let call = formatter_path_call(path, variant_formatter_arguments(&bindings));
            Ok(quote! {
                #pattern => {
                    #call
                }
            })
        }
        Fields::Named(fields) => {
            let bindings: Vec<_> = fields.iter().map(binding_ident).collect();
            let pattern = quote!(Self::#variant_ident { #(#bindings),* });
            let call = formatter_path_call(path, variant_formatter_arguments(&bindings));
            Ok(quote! {
                #pattern => {
                    #call
                }
            })
        }
    }
}

/// Generates argument expressions for variant formatter function calls.
///
/// Creates expressions from variant field bindings to pass as arguments
/// to a custom formatter function.
///
/// # Arguments
///
/// * `bindings` - The binding identifiers from the variant pattern
///
/// # Returns
///
/// Vector of token streams representing the binding references
pub fn variant_formatter_arguments(bindings: &[Ident]) -> Vec<TokenStream> {
    bindings.iter().map(|binding| quote!(#binding)).collect()
}

/// Renders a match arm for a variant using a template.
///
/// Processes the template with the variant's fields and format arguments,
/// generating the appropriate formatting code.
///
/// # Arguments
///
/// * `variant` - The variant data
/// * `template` - The display template to render
/// * `format_args` - Optional format arguments specification
///
/// # Returns
///
/// Token stream containing the match arm with template rendering
pub fn render_variant_template(
    variant: &VariantData,
    template: &DisplayTemplate,
    format_args: Option<&FormatArgsSpec>
) -> Result<TokenStream, Error> {
    let variant_ident = &variant.ident;
    match &variant.fields {
        Fields::Unit => {
            let mut env = format_args
                .map(|args| FormatArgumentsEnv::new_variant(args, &variant.fields, &[]));
            let preludes = env
                .as_ref()
                .map(|env| env.prelude_tokens())
                .unwrap_or_default();
            let format_arguments = if let Some(env) = env.as_ref() {
                env.argument_tokens()?
            } else {
                Vec::new()
            };
            let span = variant.span;
            let body = render_template(template, preludes, format_arguments, |placeholder| {
                if let Some(env) = env.as_mut()
                    && let Some(resolved) = env.resolve_placeholder(placeholder)?
                {
                    return Ok(resolved);
                }
                Err(Error::new(span, "unit variants cannot reference fields"))
            })?;
            Ok(quote! {
                Self::#variant_ident => {
                    #body
                }
            })
        }
        Fields::Unnamed(fields) => {
            let bindings: Vec<_> = fields.iter().map(binding_ident).collect();
            let mut env = format_args
                .map(|args| FormatArgumentsEnv::new_variant(args, &variant.fields, &bindings));
            let pattern = quote!(Self::#variant_ident(#(#bindings),*));
            let preludes = env
                .as_ref()
                .map(|env| env.prelude_tokens())
                .unwrap_or_default();
            let format_arguments = if let Some(env) = env.as_ref() {
                env.argument_tokens()?
            } else {
                Vec::new()
            };
            let body = render_template(template, preludes, format_arguments, |placeholder| {
                variant_tuple_placeholder(&bindings, placeholder, env.as_mut())
            })?;
            Ok(quote! {
                #pattern => {
                    #body
                }
            })
        }
        Fields::Named(fields) => {
            let bindings: Vec<_> = fields
                .iter()
                .map(|field| field.ident.clone().expect("named field"))
                .collect();
            let mut env = format_args
                .map(|args| FormatArgumentsEnv::new_variant(args, &variant.fields, &bindings));
            let pattern = quote!(Self::#variant_ident { #(#bindings),* });
            let preludes = env
                .as_ref()
                .map(|env| env.prelude_tokens())
                .unwrap_or_default();
            let format_arguments = if let Some(env) = env.as_ref() {
                env.argument_tokens()?
            } else {
                Vec::new()
            };
            let body = render_template(template, preludes, format_arguments, |placeholder| {
                variant_named_placeholder(fields, &bindings, placeholder, env.as_mut())
            })?;
            Ok(quote! {
                #pattern => {
                    #body
                }
            })
        }
    }
}

/// Resolves a placeholder for a tuple variant.
///
/// For tuple variants, placeholders can reference fields by position or use
/// format arguments. Named field access is not supported for tuple variants.
///
/// # Arguments
///
/// * `bindings` - The binding identifiers from the variant pattern
/// * `placeholder` - The placeholder specification to resolve
/// * `env` - Optional format arguments environment
///
/// # Returns
///
/// Resolved placeholder expression, or an error if resolution fails
pub fn variant_tuple_placeholder(
    bindings: &[Ident],
    placeholder: &crate::template_support::TemplatePlaceholderSpec,
    env: Option<&mut FormatArgumentsEnv<'_>>
) -> Result<ResolvedPlaceholderExpr, Error> {
    if matches!(
        &placeholder.identifier,
        TemplateIdentifierSpec::Named(name) if name == "self"
    ) {
        return Ok(ResolvedPlaceholderExpr::with(
            quote!(self),
            needs_pointer_value(&placeholder.formatter)
        ));
    }
    if let Some(env) = env
        && let Some(resolved) = env.resolve_placeholder(placeholder)?
    {
        return Ok(resolved);
    }
    match &placeholder.identifier {
        TemplateIdentifierSpec::Named(_) => {
            Err(placeholder_error(placeholder.span, &placeholder.identifier))
        }
        TemplateIdentifierSpec::Positional(index) => bindings
            .get(*index)
            .map(|binding| {
                ResolvedPlaceholderExpr::with(
                    quote!(#binding),
                    needs_pointer_value(&placeholder.formatter)
                )
            })
            .ok_or_else(|| placeholder_error(placeholder.span, &placeholder.identifier)),
        TemplateIdentifierSpec::Implicit(index) => bindings
            .get(*index)
            .map(|binding| {
                ResolvedPlaceholderExpr::with(
                    quote!(#binding),
                    needs_pointer_value(&placeholder.formatter)
                )
            })
            .ok_or_else(|| placeholder_error(placeholder.span, &placeholder.identifier))
    }
}

/// Resolves a placeholder for a named variant.
///
/// For named variants, placeholders can reference fields by name or use
/// format arguments. Positional access is not supported for named variants.
///
/// # Arguments
///
/// * `fields` - The variant's fields for name lookup
/// * `bindings` - The binding identifiers from the variant pattern
/// * `placeholder` - The placeholder specification to resolve
/// * `env` - Optional format arguments environment
///
/// # Returns
///
/// Resolved placeholder expression, or an error if resolution fails
pub fn variant_named_placeholder(
    fields: &[Field],
    bindings: &[Ident],
    placeholder: &crate::template_support::TemplatePlaceholderSpec,
    env: Option<&mut FormatArgumentsEnv<'_>>
) -> Result<ResolvedPlaceholderExpr, Error> {
    if matches!(
        &placeholder.identifier,
        TemplateIdentifierSpec::Named(name) if name == "self"
    ) {
        return Ok(ResolvedPlaceholderExpr::with(
            quote!(self),
            needs_pointer_value(&placeholder.formatter)
        ));
    }
    if let Some(env) = env
        && let Some(resolved) = env.resolve_placeholder(placeholder)?
    {
        return Ok(resolved);
    }
    match &placeholder.identifier {
        TemplateIdentifierSpec::Named(name) => {
            if let Some(index) = fields
                .iter()
                .position(|field| field.ident.as_ref().is_some_and(|ident| ident == name))
            {
                let binding = &bindings[index];
                Ok(ResolvedPlaceholderExpr::with(
                    quote!(#binding),
                    needs_pointer_value(&placeholder.formatter)
                ))
            } else {
                Err(placeholder_error(placeholder.span, &placeholder.identifier))
            }
        }
        TemplateIdentifierSpec::Positional(index) => Err(placeholder_error(
            placeholder.span,
            &TemplateIdentifierSpec::Positional(*index)
        )),
        TemplateIdentifierSpec::Implicit(index) => Err(placeholder_error(
            placeholder.span,
            &TemplateIdentifierSpec::Implicit(*index)
        ))
    }
}

#[cfg(test)]
mod tests {
    use proc_macro2::Span;
    use quote::format_ident;
    use syn::{Member, parse_quote};

    use super::*;
    use crate::{
        input::{ErrorData, Field, FieldAttrs, FormatArgsSpec},
        template_support::{DisplayTemplate, TemplateSegmentSpec}
    };

    fn make_test_field(name: &str, ty: syn::Type, index: usize) -> Field {
        Field {
            ident: Some(format_ident!("{}", name)),
            member: Member::Named(format_ident!("{}", name)),
            ty,
            index,
            attrs: FieldAttrs::default(),
            span: Span::call_site()
        }
    }

    fn make_test_unnamed_field(ty: syn::Type, index: usize) -> Field {
        Field {
            ident: None,
            member: Member::Unnamed(syn::Index {
                index: index as u32,
                span:  Span::call_site()
            }),
            ty,
            index,
            attrs: FieldAttrs::default(),
            span: Span::call_site()
        }
    }

    fn make_variant_data(ident: &str, fields: Fields, display: DisplaySpec) -> VariantData {
        VariantData {
            ident: format_ident!("{}", ident),
            fields,
            display,
            format_args: FormatArgsSpec::default(),
            app_error: None,
            masterror: None,
            span: Span::call_site()
        }
    }

    fn make_error_input(ident: &str) -> ErrorInput {
        ErrorInput {
            ident:    format_ident!("{}", ident),
            generics: Default::default(),
            data:     ErrorData::Enum(vec![])
        }
    }

    #[test]
    fn test_expand_enum_empty_variants() {
        let input = make_error_input("MyError");
        let variants = vec![];
        let result = expand_enum(&input, &variants);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("impl"));
        assert!(output.contains("core :: fmt :: Display"));
        assert!(output.contains("MyError"));
    }

    #[test]
    fn test_expand_enum_single_variant() {
        let input = make_error_input("MyError");
        let variant = make_variant_data(
            "Variant1",
            Fields::Unit,
            DisplaySpec::Template(DisplayTemplate {
                segments: vec![TemplateSegmentSpec::Literal("error".to_string())]
            })
        );
        let result = expand_enum(&input, &[variant]);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("MyError"));
        assert!(output.contains("match self"));
    }

    #[test]
    fn test_expand_enum_multiple_variants() {
        let input = make_error_input("MyError");
        let variant1 = make_variant_data(
            "Variant1",
            Fields::Unit,
            DisplaySpec::Template(DisplayTemplate {
                segments: vec![TemplateSegmentSpec::Literal("error1".to_string())]
            })
        );
        let variant2 = make_variant_data(
            "Variant2",
            Fields::Unit,
            DisplaySpec::Template(DisplayTemplate {
                segments: vec![TemplateSegmentSpec::Literal("error2".to_string())]
            })
        );
        let result = expand_enum(&input, &[variant1, variant2]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_render_variant_transparent_unit() {
        let variant = make_variant_data(
            "MyVariant",
            Fields::Unit,
            DisplaySpec::Transparent {
                attribute: Box::new(parse_quote!(#[error(transparent)]))
            }
        );
        let result = render_variant_transparent(&variant);
        assert!(result.is_err());
    }

    #[test]
    fn test_render_variant_transparent_single_unnamed_field() {
        let field = make_test_unnamed_field(parse_quote!(String), 0);
        let variant = make_variant_data(
            "MyVariant",
            Fields::Unnamed(vec![field]),
            DisplaySpec::Transparent {
                attribute: Box::new(parse_quote!(#[error(transparent)]))
            }
        );
        let result = render_variant_transparent(&variant);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("Self :: MyVariant"));
        assert!(output.contains("core :: fmt :: Display :: fmt"));
    }

    #[test]
    fn test_render_variant_transparent_single_named_field() {
        let field = make_test_field("inner", parse_quote!(String), 0);
        let variant = make_variant_data(
            "MyVariant",
            Fields::Named(vec![field]),
            DisplaySpec::Transparent {
                attribute: Box::new(parse_quote!(#[error(transparent)]))
            }
        );
        let result = render_variant_transparent(&variant);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("Self :: MyVariant"));
        assert!(output.contains("inner"));
    }

    #[test]
    fn test_render_variant_transparent_multiple_fields() {
        let field1 = make_test_field("inner1", parse_quote!(String), 0);
        let field2 = make_test_field("inner2", parse_quote!(i32), 1);
        let variant = make_variant_data(
            "MyVariant",
            Fields::Named(vec![field1, field2]),
            DisplaySpec::Transparent {
                attribute: Box::new(parse_quote!(#[error(transparent)]))
            }
        );
        let result = render_variant_transparent(&variant);
        assert!(result.is_err());
    }

    #[test]
    fn test_render_variant_formatter_path_unit() {
        let variant = make_variant_data(
            "MyVariant",
            Fields::Unit,
            DisplaySpec::FormatterPath {
                path: parse_quote!(my_formatter),
                args: FormatArgsSpec::default()
            }
        );
        let path: syn::ExprPath = parse_quote!(my_formatter);
        let result = render_variant_formatter_path(&variant, &path);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("Self :: MyVariant"));
        assert!(output.contains("my_formatter"));
    }

    #[test]
    fn test_render_variant_formatter_path_unnamed_fields() {
        let field1 = make_test_unnamed_field(parse_quote!(String), 0);
        let field2 = make_test_unnamed_field(parse_quote!(i32), 1);
        let variant = make_variant_data(
            "MyVariant",
            Fields::Unnamed(vec![field1, field2]),
            DisplaySpec::FormatterPath {
                path: parse_quote!(my_formatter),
                args: FormatArgsSpec::default()
            }
        );
        let path: syn::ExprPath = parse_quote!(my_formatter);
        let result = render_variant_formatter_path(&variant, &path);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("Self :: MyVariant"));
        assert!(output.contains("__field0"));
        assert!(output.contains("__field1"));
    }

    #[test]
    fn test_render_variant_formatter_path_named_fields() {
        let field1 = make_test_field("name", parse_quote!(String), 0);
        let field2 = make_test_field("value", parse_quote!(i32), 1);
        let variant = make_variant_data(
            "MyVariant",
            Fields::Named(vec![field1, field2]),
            DisplaySpec::FormatterPath {
                path: parse_quote!(my_formatter),
                args: FormatArgsSpec::default()
            }
        );
        let path: syn::ExprPath = parse_quote!(my_formatter);
        let result = render_variant_formatter_path(&variant, &path);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("Self :: MyVariant"));
        assert!(output.contains("name"));
        assert!(output.contains("value"));
    }

    #[test]
    fn test_variant_formatter_arguments_empty() {
        let bindings = vec![];
        let result = variant_formatter_arguments(&bindings);
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_variant_formatter_arguments_single() {
        let bindings = vec![format_ident!("field0")];
        let result = variant_formatter_arguments(&bindings);
        assert_eq!(result.len(), 1);
        assert!(result[0].to_string().contains("field0"));
    }

    #[test]
    fn test_variant_formatter_arguments_multiple() {
        let bindings = vec![format_ident!("field0"), format_ident!("field1")];
        let result = variant_formatter_arguments(&bindings);
        assert_eq!(result.len(), 2);
        assert!(result[0].to_string().contains("field0"));
        assert!(result[1].to_string().contains("field1"));
    }

    #[test]
    fn test_render_variant_template_unit() {
        let variant = make_variant_data(
            "MyVariant",
            Fields::Unit,
            DisplaySpec::Template(DisplayTemplate {
                segments: vec![TemplateSegmentSpec::Literal("unit variant".to_string())]
            })
        );
        let template = DisplayTemplate {
            segments: vec![TemplateSegmentSpec::Literal("unit variant".to_string())]
        };
        let result = render_variant_template(&variant, &template, None);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("Self :: MyVariant"));
        assert!(output.contains("unit variant"));
    }

    #[test]
    fn test_render_variant_template_unnamed_fields() {
        let field = make_test_unnamed_field(parse_quote!(String), 0);
        let variant = make_variant_data(
            "MyVariant",
            Fields::Unnamed(vec![field]),
            DisplaySpec::Template(DisplayTemplate {
                segments: vec![TemplateSegmentSpec::Literal("error".to_string())]
            })
        );
        let template = DisplayTemplate {
            segments: vec![TemplateSegmentSpec::Literal("error".to_string())]
        };
        let result = render_variant_template(&variant, &template, None);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("Self :: MyVariant"));
        assert!(output.contains("__field0"));
    }

    #[test]
    fn test_render_variant_template_named_fields() {
        let field = make_test_field("message", parse_quote!(String), 0);
        let variant = make_variant_data(
            "MyVariant",
            Fields::Named(vec![field]),
            DisplaySpec::Template(DisplayTemplate {
                segments: vec![TemplateSegmentSpec::Literal("error".to_string())]
            })
        );
        let template = DisplayTemplate {
            segments: vec![TemplateSegmentSpec::Literal("error".to_string())]
        };
        let result = render_variant_template(&variant, &template, None);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        let output = tokens.to_string();
        assert!(output.contains("Self :: MyVariant"));
        assert!(output.contains("message"));
    }

    #[test]
    fn test_variant_tuple_placeholder_self() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let bindings = vec![format_ident!("field0")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Named("self".to_string()),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_tuple_placeholder(&bindings, &placeholder, None);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.expr.to_string().contains("self"));
    }

    #[test]
    fn test_variant_tuple_placeholder_positional() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let bindings = vec![format_ident!("field0"), format_ident!("field1")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Positional(1),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_tuple_placeholder(&bindings, &placeholder, None);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.expr.to_string().contains("field1"));
    }

    #[test]
    fn test_variant_tuple_placeholder_positional_out_of_bounds() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let bindings = vec![format_ident!("field0")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Positional(5),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_tuple_placeholder(&bindings, &placeholder, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_variant_tuple_placeholder_implicit() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let bindings = vec![format_ident!("field0")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Implicit(0),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_tuple_placeholder(&bindings, &placeholder, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_variant_tuple_placeholder_named_error() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let bindings = vec![format_ident!("field0")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Named("unknown".to_string()),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_tuple_placeholder(&bindings, &placeholder, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_variant_named_placeholder_self() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let field = make_test_field("message", parse_quote!(String), 0);
        let fields = vec![field];
        let bindings = vec![format_ident!("message")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Named("self".to_string()),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_named_placeholder(&fields, &bindings, &placeholder, None);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.expr.to_string().contains("self"));
    }

    #[test]
    fn test_variant_named_placeholder_field_name() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let field = make_test_field("message", parse_quote!(String), 0);
        let fields = vec![field];
        let bindings = vec![format_ident!("message")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Named("message".to_string()),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_named_placeholder(&fields, &bindings, &placeholder, None);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.expr.to_string().contains("message"));
    }

    #[test]
    fn test_variant_named_placeholder_unknown_field() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let field = make_test_field("message", parse_quote!(String), 0);
        let fields = vec![field];
        let bindings = vec![format_ident!("message")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Named("unknown".to_string()),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_named_placeholder(&fields, &bindings, &placeholder, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_variant_named_placeholder_positional_error() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let field = make_test_field("message", parse_quote!(String), 0);
        let fields = vec![field];
        let bindings = vec![format_ident!("message")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Positional(0),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_named_placeholder(&fields, &bindings, &placeholder, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_variant_named_placeholder_implicit_error() {
        use masterror_template::template::TemplateFormatter;

        use crate::template_support::{TemplateIdentifierSpec, TemplatePlaceholderSpec};
        let field = make_test_field("message", parse_quote!(String), 0);
        let fields = vec![field];
        let bindings = vec![format_ident!("message")];
        let placeholder = TemplatePlaceholderSpec {
            identifier: TemplateIdentifierSpec::Implicit(0),
            formatter:  TemplateFormatter::Display {
                spec: None
            },
            span:       Span::call_site()
        };
        let result = variant_named_placeholder(&fields, &bindings, &placeholder, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_render_variant_with_all_display_specs() {
        let variant_transparent = make_variant_data(
            "Transparent",
            Fields::Unnamed(vec![make_test_unnamed_field(parse_quote!(String), 0)]),
            DisplaySpec::Transparent {
                attribute: Box::new(parse_quote!(#[error(transparent)]))
            }
        );
        let result = render_variant(&variant_transparent);
        assert!(result.is_ok());
        let variant_template = make_variant_data(
            "Template",
            Fields::Unit,
            DisplaySpec::Template(DisplayTemplate {
                segments: vec![TemplateSegmentSpec::Literal("error".to_string())]
            })
        );
        let result = render_variant(&variant_template);
        assert!(result.is_ok());
        let variant_formatter = make_variant_data(
            "Formatter",
            Fields::Unit,
            DisplaySpec::FormatterPath {
                path: parse_quote!(my_formatter),
                args: FormatArgsSpec::default()
            }
        );
        let result = render_variant(&variant_formatter);
        assert!(result.is_ok());
    }
}