ink_codegen/generator/event.rs
1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::GenerateCode;
16use derive_more::From;
17use proc_macro2::TokenStream as TokenStream2;
18use syn::spanned::Spanned;
19
20/// Generates code for the event item.
21#[derive(From, Copy, Clone)]
22pub struct Event<'a> {
23 /// The storage item to generate code for.
24 item: &'a ir::Event,
25}
26
27impl GenerateCode for Event<'_> {
28 /// Generates ink! event item code.
29 fn generate_code(&self) -> TokenStream2 {
30 let item = self.item.item();
31 let anonymous = self
32 .item
33 .anonymous()
34 .then(|| quote::quote! { #[ink(anonymous)] });
35 let signature_topic = self
36 .item
37 .signature_topic_hex()
38 .map(|hex_s| quote::quote! { #[ink(signature_topic = #hex_s)] });
39 let cfg_attrs = self.item.get_cfg_attrs(item.span());
40
41 quote::quote! (
42 #( #cfg_attrs )*
43 #[cfg_attr(feature = "std", derive(::ink::EventMetadata))]
44 #[derive(::ink::Event)]
45 #[::ink::scale_derive(Encode, Decode)]
46 #anonymous
47 #signature_topic
48 #item
49 )
50 }
51}