gsdk_codegen/lib.rs
1// This file is part of Gear.
2//
3// Copyright (C) 2021-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use proc_macro::TokenStream;
20use syn::ItemFn;
21
22mod storage;
23
24/// Generate storage query for the latest state for functions that
25/// query storage at specified block.
26///
27/// # Note
28///
29/// - the docs must be end with `at specified block.`
30/// - the function name must be end with `_at`.
31/// - the last argument must be `Option<H256>`.
32///
33/// # Example
34///
35/// ```ignore
36/// /// Imdocs at specified block.
37/// #[storage_fetch]
38/// pub fn query_storage_at(addr: Address, block_hash: Option<H256>) -> R {
39/// // ...
40/// }
41/// ```
42///
43/// will generate functions
44///
45/// ```ignore
46/// /// Imdocs at specified block.
47/// pub fn query_storage_at(addr: Address, block_hash: impl Into<Option<H256>>) -> R {
48/// // ...
49/// }
50///
51/// /// Imdocs.
52/// pub fn query_storage(addr: Address) -> R {
53/// query_storage_at(addr, None)
54/// }
55/// ```
56#[proc_macro_attribute]
57pub fn storage_fetch(_: TokenStream, item: TokenStream) -> TokenStream {
58 let raw: ItemFn = syn::parse_macro_input!(item);
59 storage::StorageQueryBuilder::from(raw).build()
60}