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
use super::helper;
use syn::spanned::Spanned;
pub struct OriginDef {
pub index: usize,
pub has_instance: bool,
pub is_generic: bool,
pub instances: Vec<helper::InstanceUsage>,
}
impl OriginDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
let item_span = item.span();
let (vis, ident, generics) = match &item {
syn::Item::Enum(item) => (&item.vis, &item.ident, &item.generics),
syn::Item::Struct(item) => (&item.vis, &item.ident, &item.generics),
syn::Item::Type(item) => (&item.vis, &item.ident, &item.generics),
_ => {
let msg = "Invalid pallet::origin, expected enum or struct or type";
return Err(syn::Error::new(item.span(), msg))
},
};
let has_instance = generics.params.len() == 2;
let is_generic = !generics.params.is_empty();
let mut instances = vec![];
if let Some(u) = helper::check_type_def_optional_gen(&generics, item.span())? {
instances.push(u);
} else {
instances.push(helper::InstanceUsage { has_instance: false, span: ident.span() })
}
if !matches!(vis, syn::Visibility::Public(_)) {
let msg = "Invalid pallet::origin, Origin must be public";
return Err(syn::Error::new(item_span, msg))
}
if ident != "Origin" {
let msg = "Invalid pallet::origin, ident must `Origin`";
return Err(syn::Error::new(ident.span(), msg))
}
Ok(OriginDef { index, has_instance, is_generic, instances })
}
}