ink_codegen/generator/selector.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 ir::HexLiteral;
18use proc_macro2::TokenStream as TokenStream2;
19use quote::quote_spanned;
20
21/// Generates code for the `selector_id!` macro.
22#[derive(From)]
23pub struct SelectorId<'a> {
24 /// The contract to generate code for.
25 macro_input: &'a ir::SelectorMacro<ir::marker::SelectorId>,
26}
27
28impl GenerateCode for SelectorId<'_> {
29 /// Generates `selector_id!` macro code.
30 fn generate_code(&self) -> TokenStream2 {
31 let span = self.macro_input.input().span();
32 let selector_id = self
33 .macro_input
34 .selector()
35 .into_be_u32()
36 .hex_padded_suffixed();
37 quote_spanned!(span=> #selector_id)
38 }
39}
40
41/// Generates code for the `selector_bytes!` macro.
42#[derive(From)]
43pub struct SelectorBytes<'a> {
44 /// The contract to generate code for.
45 macro_input: &'a ir::SelectorMacro<ir::marker::SelectorBytes>,
46}
47
48impl GenerateCode for SelectorBytes<'_> {
49 /// Generates `selector_bytes!` macro code.
50 fn generate_code(&self) -> TokenStream2 {
51 let span = self.macro_input.input().span();
52 let selector_bytes = self.macro_input.selector().hex_lits();
53 quote_spanned!(span=> [ #( #selector_bytes ),* ] )
54 }
55}