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
140
141
142
143
144
145
146
147
148
149
150
151
152
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::helper;
use quote::ToTokens;
use syn::spanned::Spanned;

/// List of additional token to be used for parsing.
mod keyword {
	syn::custom_keyword!(Event);
	syn::custom_keyword!(pallet);
	syn::custom_keyword!(generate_deposit);
	syn::custom_keyword!(deposit_event);
}

/// Definition for pallet event enum.
pub struct EventDef {
	/// The index of event item in pallet module.
	pub index: usize,
	/// The keyword Event used (contains span).
	pub event: keyword::Event,
	/// A set of usage of instance, must be check for consistency with trait.
	pub instances: Vec<helper::InstanceUsage>,
	/// The kind of generic the type `Event` has.
	pub gen_kind: super::GenericKind,
	/// Whether the function `deposit_event` must be generated.
	pub deposit_event: Option<PalletEventDepositAttr>,
	/// Where clause used in event definition.
	pub where_clause: Option<syn::WhereClause>,
	/// The span of the pallet::event attribute.
	pub attr_span: proc_macro2::Span,
	/// event attributes
	pub attrs: Vec<syn::Attribute>,
}

/// Attribute for a pallet's Event.
///
/// Syntax is:
/// * `#[pallet::generate_deposit($vis fn deposit_event)]`
pub struct PalletEventDepositAttr {
	pub fn_vis: syn::Visibility,
	// Span for the keyword deposit_event
	pub fn_span: proc_macro2::Span,
	// Span of the attribute
	pub span: proc_macro2::Span,
}

impl syn::parse::Parse for PalletEventDepositAttr {
	fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
		input.parse::<syn::Token![#]>()?;
		let content;
		syn::bracketed!(content in input);
		content.parse::<keyword::pallet>()?;
		content.parse::<syn::Token![::]>()?;

		let span = content.parse::<keyword::generate_deposit>()?.span();
		let generate_content;
		syn::parenthesized!(generate_content in content);
		let fn_vis = generate_content.parse::<syn::Visibility>()?;
		generate_content.parse::<syn::Token![fn]>()?;
		let fn_span = generate_content.parse::<keyword::deposit_event>()?.span();

		Ok(PalletEventDepositAttr { fn_vis, span, fn_span })
	}
}

struct PalletEventAttrInfo {
	deposit_event: Option<PalletEventDepositAttr>,
}

impl PalletEventAttrInfo {
	fn from_attrs(attrs: Vec<PalletEventDepositAttr>) -> syn::Result<Self> {
		let mut deposit_event = None;
		for attr in attrs {
			if deposit_event.is_none() {
				deposit_event = Some(attr)
			} else {
				return Err(syn::Error::new(attr.span, "Duplicate attribute"))
			}
		}

		Ok(PalletEventAttrInfo { deposit_event })
	}
}

impl EventDef {
	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::event, expected enum item"))
		};
		let attrs = item.attrs.clone();
		let event_attrs: Vec<PalletEventDepositAttr> =
			helper::take_item_pallet_attrs(&mut item.attrs)?;
		let attr_info = PalletEventAttrInfo::from_attrs(event_attrs)?;
		let deposit_event = attr_info.deposit_event;

		if !matches!(item.vis, syn::Visibility::Public(_)) {
			let msg = "Invalid pallet::event, `Event` must be public";
			return Err(syn::Error::new(item.span(), msg))
		}

		let where_clause = item.generics.where_clause.clone();

		let mut instances = vec![];
		// NOTE: Event is not allowed to be only generic on I because it is not supported
		// by construct_runtime.
		if let Some(u) = helper::check_type_def_optional_gen(&item.generics, item.ident.span())? {
			instances.push(u);
		} else {
			// construct_runtime only allow non generic event for non instantiable pallet.
			instances.push(helper::InstanceUsage { has_instance: false, span: item.ident.span() })
		}

		let has_instance = item.generics.type_params().any(|t| t.ident == "I");
		let has_config = item.generics.type_params().any(|t| t.ident == "T");
		let gen_kind = super::GenericKind::from_gens(has_config, has_instance)
			.expect("Checked by `helper::check_type_def_optional_gen` above");

		let event = syn::parse2::<keyword::Event>(item.ident.to_token_stream())?;

		Ok(EventDef {
			attr_span,
			index,
			instances,
			deposit_event,
			event,
			gen_kind,
			where_clause,
			attrs,
		})
	}
}