ink_codegen/generator/as_dependency/
mod.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
15mod call_builder;
16mod contract_ref;
17
18use self::{
19    call_builder::CallBuilder,
20    contract_ref::ContractRef,
21};
22use crate::{
23    traits::GenerateCodeUsing,
24    GenerateCode,
25};
26use derive_more::From;
27use proc_macro2::TokenStream as TokenStream2;
28use quote::quote;
29
30/// Generates code for generating a contract reference.
31///
32/// Contract references are used to dynamically depend on a smart contract.
33/// The contract reference is just a typed thin-wrapper around an `AccountId`
34/// that implements an API mirrored by the smart contract.
35#[derive(From)]
36pub struct ContractReference<'a> {
37    /// The contract to generate code for.
38    contract: &'a ir::Contract,
39}
40impl_as_ref_for_generator!(ContractReference);
41
42impl GenerateCode for ContractReference<'_> {
43    /// Generates ink! contract code.
44    fn generate_code(&self) -> TokenStream2 {
45        let call_builder = self.generate_code_using::<CallBuilder>();
46        let call_forwarder = self.generate_code_using::<ContractRef>();
47        quote! {
48            #call_builder
49            #call_forwarder
50        }
51    }
52}