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.source.is_some() {
123            return Some(field);
124        }
125    }
126    for field in fields {
127        if let Some(from) = field.attrs.from {
128            if !from.no_source {
129                return Some(field);
130            }
131        }
132    }
133    for field in fields {
134        match &field.member {
135            MemberUnraw::Named(ident) if ident == "source" => return Some(field),
136            _ => {}
137        }
138    }
139    None
140}
141
142fn backtrace_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
143    for field in fields {
144        if field.attrs.backtrace.is_some() {
145            return Some(field);
146        }
147    }
148    for field in fields {
149        if field.is_backtrace() {
150            return Some(field);
151        }
152    }
153    None
154}
155
156fn location_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
157    for field in fields {
158        if field.attrs.location.is_some() {
159            return Some(field);
160        }
161    }
162    for field in fields {
163        if field.is_location() {
164            return Some(field);
165        }
166    }
167    None
168}
169
170// The #[backtrace] field, if it is not the same as the #[from] field.
171fn distinct_backtrace_field<'a, 'b>(
172    backtrace_field: &'a Field<'b>,
173    from_field: Option<&Field>,
174) -> Option<&'a Field<'b>> {
175    if from_field.map_or(false, |from_field| {
176        from_field.member == backtrace_field.member
177    }) {
178        None
179    } else {
180        Some(backtrace_field)
181    }
182}
183
184fn type_is_backtrace(ty: &Type) -> bool {
185    let path = match ty {
186        Type::Path(ty) => &ty.path,
187        _ => return false,
188    };
189
190    let last = path.segments.last().unwrap();
191    last.ident == "Backtrace" && last.arguments.is_empty()
192}
193
194fn type_is_location(ty: &Type) -> bool {
195    let path = match ty {
196        Type::Reference(TypeReference {
197            lifetime: Some(Lifetime { ident: ltident, .. }),
198            elem, // TODO: replace with `elem: box Type::Path(path)` once box_patterns stabalizes
199            ..
200        }) if ltident == "static" => match &**elem {
201            Type::Path(ty) => &ty.path,
202            _ => return false,
203        },
204        _ => return false,
205    };
206
207    let last = path.segments.last().unwrap();
208
209    last.ident == "Location"
210        && match &last.arguments {
211            PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => {
212                match args.first() {
213                    Some(GenericArgument::Lifetime(Lifetime { ident, .. })) => ident == "static",
214                    _ => false,
215                }
216            }
217            _ => false,
218        }
219}