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.
1415//! Generates the ink! trait definition item.
1617use super::TraitDefinition;
18use heck::ToLowerCamelCase as _;
19use proc_macro2::TokenStream as TokenStream2;
20use quote::{
21 format_ident,
22 quote,
23 quote_spanned,
24};
2526impl TraitDefinition<'_> {
27fn generate_for_message(message: ir::InkTraitMessage<'_>) -> TokenStream2 {
28let span = message.span();
29let attrs = message.attrs();
30let sig = message.sig();
31let ident = &sig.ident;
32let inputs = &sig.inputs;
33let cfg_attrs = message.get_cfg_attrs(span);
34let output = match &sig.output {
35 syn::ReturnType::Default => quote! { () },
36 syn::ReturnType::Type(_, ty) => quote! { #ty },
37 };
38let output_ident =
39format_ident!("{}Output", ident.to_string().to_lower_camel_case());
40quote_spanned!(span =>
41/// Output type of the respective trait message.
42#(#cfg_attrs)*
43type #output_ident: ::ink::codegen::ImpliesReturn<#output>;
4445 #(#attrs)*
46fn #ident(#inputs) -> Self::#output_ident;
47 )
48 }
49}
5051impl TraitDefinition<'_> {
52pub(super) fn generate_trait_definition(&self) -> TokenStream2 {
53let item = self.trait_def.item();
54let span = item.span();
55let attrs = item.attrs();
56let ident = item.ident();
57let messages = item
58 .iter_items()
59 .map(|(item, _)| item)
60 .flat_map(ir::InkTraitItem::filter_map_message)
61 .map(Self::generate_for_message);
62quote_spanned!(span =>
63 #(#attrs)*
64pub trait #ident: ::ink::env::ContractEnv {
65/// Holds general and global information about the trait.
66#[doc(hidden)]
67 #[allow(non_camel_case_types)]
68type __ink_TraitInfo: ::ink::codegen::TraitCallForwarder;
6970 #(#messages)*
71 }
72 )
73 }
74}