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
56                .variants
57                .iter()
58                .any(|variant| variant.attrs.display.is_some() || variant.attrs.fmt.is_some())
59            || self
60                .variants
61                .iter()
62                .all(|variant| variant.attrs.transparent.is_some())
63    }
64}
65
66impl Variant<'_> {
67    pub(crate) fn from_field(&self) -> Option<&Field> {
68        from_field(&self.fields)
69    }
70
71    pub(crate) fn source_field(&self) -> Option<&Field> {
72        source_field(&self.fields)
73    }
74
75    pub(crate) fn backtrace_field(&self) -> Option<&Field> {
76        backtrace_field(&self.fields)
77    }
78
79    pub(crate) fn location_field(&self) -> Option<&Field> {
80        location_field(&self.fields)
81    }
82
83    pub(crate) fn distinct_backtrace_field(&self) -> Option<&Field> {
84        let backtrace_field = self.backtrace_field()?;
85        distinct_backtrace_field(backtrace_field, self.from_field())
86    }
87}
88
89impl Field<'_> {
90    pub(crate) fn is_backtrace(&self) -> bool {
91        type_is_backtrace(self.ty)
92    }
93
94    pub(crate) fn is_location(&self) -> bool {
95        type_is_location(self.ty)
96    }
97
98    pub(crate) fn source_span(&self) -> Span {
99        if let Some(source_attr) = &self.attrs.source {
100            source_attr.span
101        } else if let Some(from_attr) = &self.attrs.from {
102            from_attr.span
103        } else {
104            self.member.span()
105        }
106    }
107}
108
109fn from_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
110    for field in fields {
111        if field.attrs.from.is_some() {
112            return Some(field);
113        }
114    }
115    None
116}
117
118fn source_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
119    for field in fields {
120        if field.attrs.from.is_some() || field.attrs.source.is_some() {
121            return Some(field);
122        }
123    }
124    for field in fields {
125        match &field.member {
126            MemberUnraw::Named(ident) if ident == "source" => return Some(field),
127            _ => {}
128        }
129    }
130    None
131}
132
133fn backtrace_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
134    for field in fields {
135        if field.attrs.backtrace.is_some() {
136            return Some(field);
137        }
138    }
139    for field in fields {
140        if field.is_backtrace() {
141            return Some(field);
142        }
143    }
144    None
145}
146
147fn location_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
148    for field in fields {
149        if field.attrs.location.is_some() {
150            return Some(field);
151        }
152    }
153    for field in fields {
154        if field.is_location() {
155            return Some(field);
156        }
157    }
158    None
159}
160
161// The #[backtrace] field, if it is not the same as the #[from] field.
162fn distinct_backtrace_field<'a, 'b>(
163    backtrace_field: &'a Field<'b>,
164    from_field: Option<&Field>,
165) -> Option<&'a Field<'b>> {
166    if from_field.map_or(false, |from_field| {
167        from_field.member == backtrace_field.member
168    }) {
169        None
170    } else {
171        Some(backtrace_field)
172    }
173}
174
175fn type_is_backtrace(ty: &Type) -> bool {
176    let path = match ty {
177        Type::Path(ty) => &ty.path,
178        _ => return false,
179    };
180
181    let last = path.segments.last().unwrap();
182    last.ident == "Backtrace" && last.arguments.is_empty()
183}
184
185fn type_is_location(ty: &Type) -> bool {
186    let path = match ty {
187        Type::Reference(TypeReference {
188            lifetime: Some(Lifetime { ident: ltident, .. }),
189            elem, // TODO: replace with `elem: box Type::Path(path)` once box_patterns stabalizes
190            ..
191        }) if ltident == "static" => match &**elem {
192            Type::Path(ty) => &ty.path,
193            _ => return false,
194        },
195        _ => return false,
196    };
197
198    let last = path.segments.last().unwrap();
199
200    last.ident == "Location"
201        && match &last.arguments {
202            PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => {
203                match args.first() {
204                    Some(GenericArgument::Lifetime(Lifetime { ident, .. })) => ident == "static",
205                    _ => false,
206                }
207            }
208            _ => false,
209        }
210}