masterror-derive 0.12.0

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

//! Core data structures for parsed error definitions.
//!
//! This module defines all types used to represent parsed error attributes,
//! fields, display specifications, and format arguments from derive macro
//! input.

use proc_macro2::Span;
use syn::{
    AngleBracketedGenericArguments, Attribute, Error, Expr, ExprPath, Field as SynField,
    Fields as SynFields, Ident, LitStr, Token, TypePath, ext::IdentExt, punctuated::Punctuated,
    spanned::Spanned, token::Paren
};

use super::{parse_attr::parse_provide_attribute, utils::is_backtrace_storage};
use crate::template_support::DisplayTemplate;

/// Top-level parsed error type definition.
///
/// Represents either a struct or enum error type with its associated metadata.
#[derive(Debug)]
pub struct ErrorInput {
    pub ident:    Ident,
    pub generics: syn::Generics,
    pub data:     ErrorData
}

/// Error definition data (struct or enum).
#[derive(Debug)]
pub enum ErrorData {
    Struct(Box<StructData>),
    Enum(Vec<VariantData>)
}

/// Parsed struct error data.
#[derive(Debug)]
pub struct StructData {
    pub fields:      Fields,
    pub display:     DisplaySpec,
    #[allow(dead_code)]
    pub format_args: FormatArgsSpec,
    pub app_error:   Option<AppErrorSpec>,
    pub masterror:   Option<MasterrorSpec>
}

/// Parsed enum variant error data.
#[derive(Debug)]
pub struct VariantData {
    pub ident:       Ident,
    pub fields:      Fields,
    pub display:     DisplaySpec,
    #[allow(dead_code)]
    pub format_args: FormatArgsSpec,
    pub app_error:   Option<AppErrorSpec>,
    pub masterror:   Option<MasterrorSpec>,
    pub span:        Span
}

/// AppError attribute specification.
///
/// Configures error kind, code, message exposure and source attachment for
/// app-level errors.
#[derive(Clone, Debug)]
pub struct AppErrorSpec {
    pub kind:           ExprPath,
    pub code:           Option<ExprPath>,
    pub expose_message: bool,
    pub no_source:      bool,
    pub attribute_span: Span
}

/// Masterror attribute specification.
///
/// Configures error code, category, redaction, telemetry, and transport
/// mappings.
#[derive(Clone, Debug)]
pub struct MasterrorSpec {
    pub code:           Expr,
    pub category:       ExprPath,
    pub expose_message: bool,
    pub redact:         RedactSpec,
    pub telemetry:      Vec<Expr>,
    pub map_grpc:       Option<Expr>,
    pub map_problem:    Option<Expr>,
    #[allow(dead_code)]
    pub attribute_span: Span
}

/// Field redaction configuration.
///
/// Specifies whether to redact the message and which fields to redact.
#[derive(Clone, Debug, Default)]
pub struct RedactSpec {
    pub message: bool,
    pub fields:  Vec<FieldRedactionSpec>
}

/// Individual field redaction specification.
#[derive(Clone, Debug)]
pub struct FieldRedactionSpec {
    pub name:   LitStr,
    pub policy: FieldRedactionKind
}

/// Field redaction strategy.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FieldRedactionKind {
    None,
    Redact,
    Hash,
    Last4
}

/// Parsed fields (unit, named, or unnamed).
#[derive(Debug)]
pub enum Fields {
    Unit,
    Named(Vec<Field>),
    Unnamed(Vec<Field>)
}

impl Fields {
    /// Returns the number of fields.
    pub fn len(&self) -> usize {
        match self {
            Self::Unit => 0,
            Self::Named(fields) | Self::Unnamed(fields) => fields.len()
        }
    }

    /// Returns an iterator over fields.
    pub fn iter(&self) -> FieldIter<'_> {
        match self {
            Self::Unit => FieldIter::Empty,
            Self::Named(fields) | Self::Unnamed(fields) => FieldIter::Slice(fields.iter())
        }
    }

    /// Finds a named field by identifier.
    ///
    /// Raw identifiers are matched by their unraw form, so a template
    /// placeholder `{source}` resolves a field named `r#source`.
    pub fn get_named(&self, name: &str) -> Option<&Field> {
        match self {
            Self::Named(fields) => fields.iter().find(|field| {
                field
                    .ident
                    .as_ref()
                    .is_some_and(|ident| ident.unraw() == name)
            }),
            _ => None
        }
    }

    /// Finds an unnamed field by index.
    pub fn get_positional(&self, index: usize) -> Option<&Field> {
        match self {
            Self::Unnamed(fields) => fields.get(index),
            _ => None
        }
    }

    /// Parses fields from syn AST.
    pub fn from_syn(fields: &SynFields, errors: &mut Vec<Error>) -> Self {
        match fields {
            SynFields::Unit => Self::Unit,
            SynFields::Named(named) => {
                let mut items = Vec::new();
                for (index, field) in named.named.iter().enumerate() {
                    items.push(Field::from_syn(field, index, errors));
                }
                Self::Named(items)
            }
            SynFields::Unnamed(unnamed) => {
                let mut items = Vec::new();
                for (index, field) in unnamed.unnamed.iter().enumerate() {
                    items.push(Field::from_syn(field, index, errors));
                }
                Self::Unnamed(items)
            }
        }
    }

    /// Finds the first field with a `#[from]` attribute.
    pub fn first_from_field(&self) -> Option<&Field> {
        self.iter().find(|field| field.attrs.from.is_some())
    }

    /// Finds the backtrace field if present.
    pub fn backtrace_field(&self) -> Option<BacktraceField<'_>> {
        self.iter().find_map(|field| {
            field
                .attrs
                .backtrace_kind()
                .map(|kind| BacktraceField::new(field, kind))
        })
    }
}

/// Backtrace field detection mode.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BacktraceFieldKind {
    Explicit,
    Inferred
}

/// Reference to a field containing backtrace data.
#[derive(Clone, Copy, Debug)]
pub struct BacktraceField<'a> {
    field: &'a Field,
    kind:  BacktraceFieldKind
}

impl<'a> BacktraceField<'a> {
    /// Creates a new backtrace field reference.
    pub fn new(field: &'a Field, kind: BacktraceFieldKind) -> Self {
        Self {
            field,
            kind
        }
    }

    /// Returns the underlying field.
    pub fn field(&self) -> &'a Field {
        self.field
    }

    /// Returns the detection mode.
    pub fn kind(&self) -> BacktraceFieldKind {
        self.kind
    }

    /// Checks if field type can store backtrace.
    pub fn stores_backtrace(&self) -> bool {
        is_backtrace_storage(&self.field.ty)
    }

    /// Returns the field index.
    pub fn index(&self) -> usize {
        self.field.index
    }
}

/// Iterator over fields.
pub enum FieldIter<'a> {
    Empty,
    Slice(std::slice::Iter<'a, Field>)
}

impl<'a> Iterator for FieldIter<'a> {
    type Item = &'a Field;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            FieldIter::Empty => None,
            FieldIter::Slice(iter) => iter.next()
        }
    }
}

/// Parsed field with attributes and metadata.
#[derive(Debug)]
pub struct Field {
    pub ident:  Option<Ident>,
    pub member: syn::Member,
    pub ty:     syn::Type,
    pub attrs:  FieldAttrs,
    pub span:   Span,
    pub index:  usize
}

impl Field {
    /// Parses field from syn AST.
    pub(crate) fn from_syn(field: &SynField, index: usize, errors: &mut Vec<Error>) -> Self {
        let ident = field.ident.clone();
        let member = match &ident {
            Some(name) => syn::Member::Named(name.clone()),
            None => syn::Member::Unnamed(syn::Index::from(index))
        };
        let attrs = FieldAttrs::from_attrs(&field.attrs, ident.as_ref(), &field.ty, errors);
        Self {
            ident,
            member,
            ty: field.ty.clone(),
            attrs,
            span: field.span(),
            index
        }
    }
}

/// Field-level attributes.
#[derive(Debug, Default)]
pub struct FieldAttrs {
    pub from:           Option<Attribute>,
    pub source:         Option<Attribute>,
    pub backtrace:      Option<Attribute>,
    pub provides:       Vec<ProvideSpec>,
    inferred_source:    bool,
    inferred_backtrace: bool
}

/// Provide attribute specification.
#[derive(Debug)]
pub struct ProvideSpec {
    pub reference: Option<TypePath>,
    pub value:     Option<TypePath>
}

impl FieldAttrs {
    /// Parses field attributes from syn AST.
    pub(crate) fn from_attrs(
        attrs: &[Attribute],
        ident: Option<&Ident>,
        ty: &syn::Type,
        errors: &mut Vec<Error>
    ) -> Self {
        use super::utils::{is_backtrace_type, is_option_type, option_inner_type, path_is};
        let mut result = FieldAttrs::default();
        for attr in attrs {
            if path_is(attr, "from") {
                if let Err(err) = attr.meta.require_path_only() {
                    errors.push(err);
                    continue;
                }
                if result.from.is_some() {
                    errors.push(Error::new_spanned(attr, "duplicate #[from] attribute"));
                    continue;
                }
                result.from = Some(attr.clone());
            } else if path_is(attr, "source") {
                if let Err(err) = attr.meta.require_path_only() {
                    errors.push(err);
                    continue;
                }
                if result.source.is_some() {
                    errors.push(Error::new_spanned(attr, "duplicate #[source] attribute"));
                    continue;
                }
                result.source = Some(attr.clone());
            } else if path_is(attr, "backtrace") {
                if let Err(err) = attr.meta.require_path_only() {
                    errors.push(err);
                    continue;
                }
                if result.backtrace.is_some() {
                    errors.push(Error::new_spanned(attr, "duplicate #[backtrace] attribute"));
                    continue;
                }
                result.backtrace = Some(attr.clone());
            } else if path_is(attr, "provide") {
                match parse_provide_attribute(attr) {
                    Ok(spec) => result.provides.push(spec),
                    Err(err) => errors.push(err)
                }
            }
        }
        if result.source.is_none()
            && let Some(attr) = &result.from
        {
            result.source = Some(attr.clone());
        }
        if result.source.is_none() && ident.is_some_and(|ident| ident == "source") {
            result.inferred_source = true;
        }
        if result.backtrace.is_none() {
            if is_option_type(ty) {
                if option_inner_type(ty).is_some_and(is_backtrace_type) {
                    result.inferred_backtrace = true;
                }
            } else if is_backtrace_type(ty) {
                result.inferred_backtrace = true;
            }
        }
        result
    }

    /// Checks if field has source attribute.
    pub fn has_source(&self) -> bool {
        self.source.is_some() || self.inferred_source
    }

    /// Checks if field has backtrace attribute.
    pub fn has_backtrace(&self) -> bool {
        self.backtrace.is_some() || self.inferred_backtrace
    }

    /// Returns backtrace detection kind.
    pub fn backtrace_kind(&self) -> Option<BacktraceFieldKind> {
        if self.backtrace.is_some() {
            Some(BacktraceFieldKind::Explicit)
        } else if self.inferred_backtrace {
            Some(BacktraceFieldKind::Inferred)
        } else {
            None
        }
    }

    /// Returns source attribute if present.
    pub fn source_attribute(&self) -> Option<&Attribute> {
        self.source.as_ref()
    }

    /// Returns backtrace attribute if present.
    pub fn backtrace_attribute(&self) -> Option<&Attribute> {
        self.backtrace.as_ref()
    }
}

/// Display specification for error messages.
#[derive(Debug)]
pub enum DisplaySpec {
    Transparent {
        attribute: Box<Attribute>
    },
    Template(DisplayTemplate),
    #[allow(dead_code)]
    TemplateWithArgs {
        template: DisplayTemplate,
        args:     FormatArgsSpec
    },
    #[allow(dead_code)]
    FormatterPath {
        path: ExprPath,
        args: FormatArgsSpec
    }
}

/// Format arguments specification.
#[allow(dead_code)]
#[derive(Debug, Default)]
pub struct FormatArgsSpec {
    pub args: Vec<FormatArg>
}

/// Single format argument.
#[allow(dead_code)]
#[derive(Debug)]
pub struct FormatArg {
    pub value: FormatArgValue,
    pub kind:  FormatBindingKind,
    pub span:  Span
}

/// Format argument value (expression or shorthand).
#[allow(dead_code)]
#[derive(Debug)]
pub enum FormatArgValue {
    Expr(Expr),
    Shorthand(FormatArgShorthand)
}

/// Format argument shorthand notation.
#[allow(dead_code)]
#[derive(Debug)]
pub enum FormatArgShorthand {
    Projection(FormatArgProjection)
}

/// Field projection for format arguments.
#[derive(Debug)]
pub struct FormatArgProjection {
    pub segments: Vec<FormatArgProjectionSegment>,
    pub span:     Span
}

/// Projection segment (field access, indexing, method call).
#[derive(Debug)]
pub enum FormatArgProjectionSegment {
    Field(Ident),
    Index { index: usize, span: Span },
    MethodCall(FormatArgProjectionMethodCall)
}

impl FormatArgProjectionSegment {
    /// Returns the span of this segment.
    pub(crate) fn span(&self) -> Span {
        match self {
            Self::Field(ident) => ident.span(),
            Self::Index {
                span, ..
            } => *span,
            Self::MethodCall(call) => call.span
        }
    }
}

/// Method call in projection chain.
#[derive(Debug)]
pub struct FormatArgProjectionMethodCall {
    pub method:    Ident,
    pub turbofish: Option<FormatArgMethodTurbofish>,
    pub args:      Punctuated<Expr, Token![,]>,
    pub span:      Span
}

/// Turbofish syntax for method calls.
#[derive(Debug)]
pub struct FormatArgMethodTurbofish {
    pub colon2_token: Token![::],
    pub generics:     AngleBracketedGenericArguments
}

/// Method call suffix type alias.
pub(crate) type MethodCallSuffix = Option<(
    Option<FormatArgMethodTurbofish>,
    Paren,
    Punctuated<Expr, Token![,]>
)>;

/// Format argument binding kind.
#[allow(dead_code)]
#[derive(Debug)]
pub enum FormatBindingKind {
    Named(Ident),
    Positional(usize),
    Implicit(usize)
}

#[cfg(test)]
mod tests {
    use syn::parse_quote;

    use super::*;

    #[test]
    fn fields_len_unit() {
        let fields = Fields::Unit;
        assert_eq!(fields.len(), 0);
    }

    #[test]
    fn fields_len_named() {
        let fields: syn::FieldsNamed = parse_quote! { { x: i32, y: String } };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        assert_eq!(parsed.len(), 2);
        assert!(errors.is_empty());
    }

    #[test]
    fn fields_len_unnamed() {
        let fields: syn::FieldsUnnamed = parse_quote! { (i32, String) };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Unnamed(fields), &mut errors);
        assert_eq!(parsed.len(), 2);
        assert!(errors.is_empty());
    }

    #[test]
    fn fields_iter_empty() {
        let fields = Fields::Unit;
        let mut iter = fields.iter();
        assert!(iter.next().is_none());
    }

    #[test]
    fn fields_iter_named() {
        let fields: syn::FieldsNamed = parse_quote! { { x: i32 } };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        let count = parsed.iter().count();
        assert_eq!(count, 1);
    }

    #[test]
    fn fields_get_named_found() {
        let fields: syn::FieldsNamed = parse_quote! { { x: i32, y: String } };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        assert!(parsed.get_named("x").is_some());
        assert!(parsed.get_named("y").is_some());
    }

    #[test]
    fn fields_get_named_raw_ident_unraw_match() {
        let fields: syn::FieldsNamed = parse_quote! { { r#source: String } };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        assert!(parsed.get_named("source").is_some());
    }

    #[test]
    fn fields_get_named_not_found() {
        let fields: syn::FieldsNamed = parse_quote! { { x: i32 } };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        assert!(parsed.get_named("z").is_none());
    }

    #[test]
    fn fields_get_named_on_unit() {
        let fields = Fields::Unit;
        assert!(fields.get_named("x").is_none());
    }

    #[test]
    fn fields_get_named_on_unnamed() {
        let fields: syn::FieldsUnnamed = parse_quote! { (i32) };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Unnamed(fields), &mut errors);
        assert!(parsed.get_named("x").is_none());
    }

    #[test]
    fn fields_get_positional_found() {
        let fields: syn::FieldsUnnamed = parse_quote! { (i32, String) };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Unnamed(fields), &mut errors);
        assert!(parsed.get_positional(0).is_some());
        assert!(parsed.get_positional(1).is_some());
    }

    #[test]
    fn fields_get_positional_not_found() {
        let fields: syn::FieldsUnnamed = parse_quote! { (i32) };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Unnamed(fields), &mut errors);
        assert!(parsed.get_positional(5).is_none());
    }

    #[test]
    fn fields_get_positional_on_unit() {
        let fields = Fields::Unit;
        assert!(fields.get_positional(0).is_none());
    }

    #[test]
    fn fields_get_positional_on_named() {
        let fields: syn::FieldsNamed = parse_quote! { { x: i32 } };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        assert!(parsed.get_positional(0).is_none());
    }

    #[test]
    fn fields_first_from_field_found() {
        let fields: syn::FieldsNamed = parse_quote! {
            { #[from] x: io::Error, y: String }
        };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        assert!(parsed.first_from_field().is_some());
    }

    #[test]
    fn fields_first_from_field_not_found() {
        let fields: syn::FieldsNamed = parse_quote! { { x: i32, y: String } };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        assert!(parsed.first_from_field().is_none());
    }

    #[test]
    fn fields_backtrace_field_explicit() {
        let fields: syn::FieldsNamed = parse_quote! {
            { #[backtrace] bt: std::backtrace::Backtrace }
        };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        let bt_field = parsed.backtrace_field();
        assert!(bt_field.is_some());
        assert_eq!(bt_field.unwrap().kind(), BacktraceFieldKind::Explicit);
    }

    #[test]
    fn fields_backtrace_field_inferred() {
        let fields: syn::FieldsUnnamed = parse_quote! { (std::backtrace::Backtrace) };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Unnamed(fields), &mut errors);
        let bt_field = parsed.backtrace_field();
        assert!(bt_field.is_some());
        assert_eq!(bt_field.unwrap().kind(), BacktraceFieldKind::Inferred);
    }

    #[test]
    fn fields_backtrace_field_not_found() {
        let fields: syn::FieldsNamed = parse_quote! { { x: i32 } };
        let mut errors = Vec::new();
        let parsed = Fields::from_syn(&syn::Fields::Named(fields), &mut errors);
        assert!(parsed.backtrace_field().is_none());
    }

    #[test]
    fn backtrace_field_methods() {
        let field: SynField = parse_quote! { #[backtrace] bt: std::backtrace::Backtrace };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        let bt = BacktraceField::new(&parsed, BacktraceFieldKind::Explicit);
        assert_eq!(bt.field().index, 0);
        assert_eq!(bt.kind(), BacktraceFieldKind::Explicit);
        assert!(bt.stores_backtrace());
        assert_eq!(bt.index(), 0);
    }

    #[test]
    fn backtrace_field_stores_option_backtrace() {
        let field: SynField = parse_quote! { bt: Option<std::backtrace::Backtrace> };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        let bt = BacktraceField::new(&parsed, BacktraceFieldKind::Inferred);
        assert!(bt.stores_backtrace());
    }

    #[test]
    fn field_attrs_has_source_explicit() {
        let field: SynField = parse_quote! { #[source] e: io::Error };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(parsed.attrs.has_source());
        assert!(parsed.attrs.source_attribute().is_some());
    }

    #[test]
    fn field_attrs_has_source_inferred() {
        let field: SynField = parse_quote! { source: io::Error };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(parsed.attrs.has_source());
    }

    #[test]
    fn field_attrs_raw_source_not_inferred() {
        let field: SynField = parse_quote! { r#source: String };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(!parsed.attrs.has_source());
    }

    #[test]
    fn field_attrs_raw_source_explicit_attribute() {
        let field: SynField = parse_quote! { #[source] r#source: io::Error };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(parsed.attrs.has_source());
    }

    #[test]
    fn field_attrs_has_source_from() {
        let field: SynField = parse_quote! { #[from] e: io::Error };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(parsed.attrs.has_source());
    }

    #[test]
    fn field_attrs_has_backtrace_explicit() {
        let field: SynField = parse_quote! { #[backtrace] bt: Backtrace };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(parsed.attrs.has_backtrace());
        assert!(parsed.attrs.backtrace_attribute().is_some());
    }

    #[test]
    fn field_attrs_has_backtrace_inferred() {
        let field: SynField = parse_quote! { bt: std::backtrace::Backtrace };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(parsed.attrs.has_backtrace());
    }

    #[test]
    fn field_attrs_has_backtrace_option() {
        let field: SynField = parse_quote! { bt: Option<Backtrace> };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(parsed.attrs.has_backtrace());
    }

    #[test]
    fn field_attrs_backtrace_kind_none() {
        let field: SynField = parse_quote! { x: i32 };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert!(parsed.attrs.backtrace_kind().is_none());
    }

    #[test]
    fn field_attrs_duplicate_from() {
        let field: SynField = parse_quote! { #[from] #[from] e: io::Error };
        let mut errors = Vec::new();
        let _ = Field::from_syn(&field, 0, &mut errors);
        assert!(!errors.is_empty());
    }

    #[test]
    fn field_attrs_duplicate_source() {
        let field: SynField = parse_quote! { #[source] #[source] e: io::Error };
        let mut errors = Vec::new();
        let _ = Field::from_syn(&field, 0, &mut errors);
        assert!(!errors.is_empty());
    }

    #[test]
    fn field_attrs_duplicate_backtrace() {
        let field: SynField = parse_quote! { #[backtrace] #[backtrace] bt: Backtrace };
        let mut errors = Vec::new();
        let _ = Field::from_syn(&field, 0, &mut errors);
        assert!(!errors.is_empty());
    }

    #[test]
    fn field_attrs_provide() {
        let field: SynField = parse_quote! { #[provide(ref = ErrorCode)] e: io::Error };
        let mut errors = Vec::new();
        let parsed = Field::from_syn(&field, 0, &mut errors);
        assert_eq!(parsed.attrs.provides.len(), 1);
        assert!(errors.is_empty());
    }

    #[test]
    fn format_arg_projection_segment_span_field() {
        let ident: Ident = parse_quote! { foo };
        let segment = FormatArgProjectionSegment::Field(ident);
        let _ = segment.span();
    }

    #[test]
    fn format_arg_projection_segment_span_index() {
        let segment = FormatArgProjectionSegment::Index {
            index: 0,
            span:  Span::call_site()
        };
        let _ = segment.span();
    }

    #[test]
    fn format_arg_projection_segment_span_method_call() {
        let call = FormatArgProjectionMethodCall {
            method:    parse_quote! { to_string },
            turbofish: None,
            args:      Punctuated::new(),
            span:      Span::call_site()
        };
        let segment = FormatArgProjectionSegment::MethodCall(call);
        let _ = segment.span();
    }
}