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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::pallet::Def;
pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream {
let event = if let Some(event) = &def.event {
event
} else {
return Default::default()
};
let event_where_clause = &event.where_clause;
let completed_where_clause = super::merge_where_clauses(&[
&event.where_clause,
&def.config.where_clause,
]);
let event_ident = &event.event;
let frame_system = &def.frame_system;
let frame_support = &def.frame_support;
let event_use_gen = &event.gen_kind.type_use_gen(event.attr_span);
let event_impl_gen= &event.gen_kind.type_impl_gen(event.attr_span);
let metadata = event.metadata.iter()
.map(|(ident, args, docs)| {
let name = format!("{}", ident);
quote::quote_spanned!(event.attr_span =>
#frame_support::event::EventMetadata {
name: #frame_support::event::DecodeDifferent::Encode(#name),
arguments: #frame_support::event::DecodeDifferent::Encode(&[
#( #args, )*
]),
documentation: #frame_support::event::DecodeDifferent::Encode(&[
#( #docs, )*
]),
},
)
});
let event_item = {
let item = &mut def.item.content.as_mut().expect("Checked by def parser").1[event.index];
if let syn::Item::Enum(item) = item {
item
} else {
unreachable!("Checked by event parser")
}
};
if event.gen_kind.is_generic() {
let variant = syn::parse_quote!(
#[doc(hidden)]
#[codec(skip)]
__Ignore(
#frame_support::sp_std::marker::PhantomData<(#event_use_gen)>,
#frame_support::Never,
)
);
event_item.variants.push(variant);
}
event_item.attrs.push(syn::parse_quote!(
#[derive(
#frame_support::CloneNoBound,
#frame_support::EqNoBound,
#frame_support::PartialEqNoBound,
#frame_support::RuntimeDebugNoBound,
#frame_support::codec::Encode,
#frame_support::codec::Decode,
)]
));
let deposit_event = if let Some((fn_vis, fn_span)) = &event.deposit_event {
let event_use_gen = &event.gen_kind.type_use_gen(event.attr_span);
let trait_use_gen = &def.trait_use_generics(event.attr_span);
let type_impl_gen = &def.type_impl_generics(event.attr_span);
let type_use_gen = &def.type_use_generics(event.attr_span);
quote::quote_spanned!(*fn_span =>
impl<#type_impl_gen> Pallet<#type_use_gen> #completed_where_clause {
#fn_vis fn deposit_event(event: Event<#event_use_gen>) {
let event = <
<T as Config#trait_use_gen>::Event as
From<Event<#event_use_gen>>
>::from(event);
let event = <
<T as Config#trait_use_gen>::Event as
Into<<T as #frame_system::Config>::Event>
>::into(event);
<#frame_system::Pallet<T>>::deposit_event(event)
}
}
)
} else {
Default::default()
};
quote::quote_spanned!(event.attr_span =>
#deposit_event
impl<#event_impl_gen> From<#event_ident<#event_use_gen>> for () #event_where_clause {
fn from(_: #event_ident<#event_use_gen>) -> () { () }
}
impl<#event_impl_gen> #event_ident<#event_use_gen> #event_where_clause {
#[allow(dead_code)]
#[doc(hidden)]
pub fn metadata() -> &'static [#frame_support::event::EventMetadata] {
&[ #( #metadata )* ]
}
}
)
}