wherror_impl/
prop.rs

1use crate::ast::{Enum, Field, Struct, Variant};
2use crate::unraw::MemberUnraw;
3use proc_macro2::Span;
4use syn::Type;
5use syn::{
6    AngleBracketedGenericArguments, GenericArgument, Lifetime, PathArguments, TypeReference,
7};
8
9impl Struct<'_> {
10    pub(crate) fn from_field(&self) -> Option<&Field> {
11        from_field(&self.fields)
12    }
13
14    pub(crate) fn source_field(&self) -> Option<&Field> {
15        source_field(&self.fields)
16    }
17
18    pub(crate) fn backtrace_field(&self) -> Option<&Field> {
19        backtrace_field(&self.fields)
20    }
21
22    pub(crate) fn location_field(&self) -> Option<&Field> {
23        location_field(&self.fields)
24    }
25
26    pub(crate) fn distinct_backtrace_field(&self) -> Option<&Field> {
27        let backtrace_field = self.backtrace_field()?;
28        distinct_backtrace_field(backtrace_field, self.from_field())
29    }
30}
31
32impl Enum<'_> {
33    pub(crate) fn has_source(&self) -> bool {
34        self.variants
35            .iter()
36            .any(|variant| variant.source_field().is_some() || variant.attrs.transparent.is_some())
37    }
38
39    pub(crate) fn has_backtrace(&self) -> bool {
40        self.variants
41            .iter()
42            .any(|variant| variant.backtrace_field().is_some())
43    }
44
45    pub(crate) fn has_location(&self) -> bool {
46        self.variants
47            .iter()
48            .any(|variant| variant.location_field().is_some())
49    }
50
51    pub(crate) fn has_display(&self) -> bool {
52        self.attrs.display.is_some()
53            || self.attrs.transparent.is_some()
54            || self.attrs.fmt.is_some()
55            || self.attrs.debug.is_some()
56            || self.variants.iter().any(|variant| {
57                variant.attrs.display.is_some()
58                    || variant.attrs.fmt.is_some()
59                    || variant.attrs.debug.is_some()
60            })
61            || self
62                .variants
63                .iter()
64                .all(|variant| variant.attrs.transparent.is_some())
65    }
66}
67
68impl Variant<'_> {
69    pub(crate) fn from_field(&self) -> Option<&Field> {
70        from_field(&self.fields)
71    }
72
73    pub(crate) fn source_field(&self) -> Option<&Field> {
74        source_field(&self.fields)
75    }
76
77    pub(crate) fn backtrace_field(&self) -> Option<&Field> {
78        backtrace_field(&self.fields)
79    }
80
81    pub(crate) fn location_field(&self) -> Option<&Field> {
82        location_field(&self.fields)
83    }
84
85    pub(crate) fn distinct_backtrace_field(&self) -> Option<&Field> {
86        let backtrace_field = self.backtrace_field()?;
87        distinct_backtrace_field(backtrace_field, self.from_field())
88    }
89}
90
91impl Field<'_> {
92    pub(crate) fn is_backtrace(&self) -> bool {
93        type_is_backtrace(self.ty)
94    }
95
96    pub(crate) fn is_location(&self) -> bool {
97        type_is_location(self.ty)
98    }
99
100    pub(crate) fn source_span(&self) -> Span {
101        if let Some(source_attr) = &self.attrs.source {
102            source_attr.span
103        } else if let Some(from_attr) = &self.attrs.from {
104            from_attr.span
105        } else {
106            self.member.span()
107        }
108    }
109}
110
111fn from_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
112    for field in fields {
113        if field.attrs.from.is_some() {
114            return Some(field);
115        }
116    }
117    None
118}
119
120fn source_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
121    for field in fields {
122        if field.attrs.from.is_some() || field.attrs.source.is_some() {
123            return Some(field);
124        }
125    }
126    for field in fields {
127        match &field.member {
128            MemberUnraw::Named(ident) if ident == "source" => return Some(field),
129            _ => {}
130        }
131    }
132    None
133}
134
135fn backtrace_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
136    for field in fields {
137        if field.attrs.backtrace.is_some() {
138            return Some(field);
139        }
140    }
141    for field in fields {
142        if field.is_backtrace() {
143            return Some(field);
144        }
145    }
146    None
147}
148
149fn location_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
150    for field in fields {
151        if field.attrs.location.is_some() {
152            return Some(field);
153        }
154    }
155    for field in fields {
156        if field.is_location() {
157            return Some(field);
158        }
159    }
160    None
161}
162
163// The #[backtrace] field, if it is not the same as the #[from] field.
164fn distinct_backtrace_field<'a, 'b>(
165    backtrace_field: &'a Field<'b>,
166    from_field: Option<&Field>,
167) -> Option<&'a Field<'b>> {
168    if from_field.map_or(false, |from_field| {
169        from_field.member == backtrace_field.member
170    }) {
171        None
172    } else {
173        Some(backtrace_field)
174    }
175}
176
177fn type_is_backtrace(ty: &Type) -> bool {
178    let path = match ty {
179        Type::Path(ty) => &ty.path,
180        _ => return false,
181    };
182
183    let last = path.segments.last().unwrap();
184    last.ident == "Backtrace" && last.arguments.is_empty()
185}
186
187fn type_is_location(ty: &Type) -> bool {
188    let path = match ty {
189        Type::Reference(TypeReference {
190            lifetime: Some(Lifetime { ident: ltident, .. }),
191            elem, // TODO: replace with `elem: box Type::Path(path)` once box_patterns stabalizes
192            ..
193        }) if ltident == "static" => match &**elem {
194            Type::Path(ty) => &ty.path,
195            _ => return false,
196        },
197        _ => return false,
198    };
199
200    let last = path.segments.last().unwrap();
201
202    last.ident == "Location"
203        && match &last.arguments {
204            PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => {
205                match args.first() {
206                    Some(GenericArgument::Lifetime(Lifetime { ident, .. })) => ident == "static",
207                    _ => false,
208                }
209            }
210            _ => false,
211        }
212}