Skip to main content

gsdk_codegen/
lib.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4use proc_macro::TokenStream;
5use syn::ItemFn;
6
7mod at_block;
8
9/// Generate query for the latest state for functions that
10/// query something at specified block.
11///
12/// # Note
13///
14/// - the docs must be end with `at specified block.`
15/// - the function name must be end with `_at`.
16/// - the last argument must be `Option<H256>`.
17///
18/// # Example
19///
20/// ```ignore
21/// /// Imdocs at specified block.
22/// #[at_block]
23/// pub fn query_at(addr: Address, block_hash: Option<H256>) -> R {
24///     // ...
25/// }
26/// ```
27///
28/// will generate functions
29///
30/// ```ignore
31/// /// Imdocs at specified block.
32/// pub fn query_at(addr: Address, block_hash: impl Into<Option<H256>>) -> R {
33///     // ...
34/// }
35///
36/// /// Imdocs.
37/// pub fn query(addr: Address) -> R {
38///     query_at(addr, None)
39/// }
40/// ```
41#[proc_macro_attribute]
42pub fn at_block(_: TokenStream, item: TokenStream) -> TokenStream {
43    let raw: ItemFn = syn::parse_macro_input!(item);
44    at_block::AtBlockBuilder::from(raw).build()
45}