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
use super::helper;
use frame_support_procedural_tools::get_doc_literals;
use quote::ToTokens;
use syn::spanned::Spanned;
mod keyword {
syn::custom_keyword!(Error);
}
pub struct ErrorDef {
pub index: usize,
pub variants: Vec<(syn::Ident, Vec<syn::Lit>)>,
pub instances: Vec<helper::InstanceUsage>,
pub error: keyword::Error,
pub attr_span: proc_macro2::Span,
}
impl ErrorDef {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Enum(item) = item {
item
} else {
return Err(syn::Error::new(item.span(), "Invalid pallet::error, expected item enum"))
};
if !matches!(item.vis, syn::Visibility::Public(_)) {
let msg = "Invalid pallet::error, `Error` must be public";
return Err(syn::Error::new(item.span(), msg))
}
let mut instances = vec![];
instances.push(helper::check_type_def_gen_no_bounds(&item.generics, item.ident.span())?);
if item.generics.where_clause.is_some() {
let msg = "Invalid pallet::error, where clause is not allowed on pallet error item";
return Err(syn::Error::new(item.generics.where_clause.as_ref().unwrap().span(), msg))
}
let error = syn::parse2::<keyword::Error>(item.ident.to_token_stream())?;
let variants = item
.variants
.iter()
.map(|variant| {
if !matches!(variant.fields, syn::Fields::Unit) {
let msg = "Invalid pallet::error, unexpected fields, must be `Unit`";
return Err(syn::Error::new(variant.fields.span(), msg))
}
if variant.discriminant.is_some() {
let msg = "Invalid pallet::error, unexpected discriminant, discriminant \
are not supported";
let span = variant.discriminant.as_ref().unwrap().0.span();
return Err(syn::Error::new(span, msg))
}
Ok((variant.ident.clone(), get_doc_literals(&variant.attrs)))
})
.collect::<Result<_, _>>()?;
Ok(ErrorDef { attr_span, index, variants, instances, error })
}
}