ic_kit_macros/
lib.rs

1mod entry;
2
3use entry::{gen_entry_point_code, EntryPoint};
4use proc_macro::TokenStream;
5
6fn process_entry_point(
7    entry_point: EntryPoint,
8    attr: TokenStream,
9    item: TokenStream,
10) -> TokenStream {
11    gen_entry_point_code(entry_point, attr.into(), item.into())
12        .unwrap_or_else(|error| error.to_compile_error())
13        .into()
14}
15
16/// Export the function as the init hook of the canister.
17#[proc_macro_attribute]
18pub fn init(attr: TokenStream, item: TokenStream) -> TokenStream {
19    process_entry_point(EntryPoint::Init, attr, item)
20}
21
22/// Export the function as the pre_upgrade hook of the canister.
23#[proc_macro_attribute]
24pub fn pre_upgrade(attr: TokenStream, item: TokenStream) -> TokenStream {
25    process_entry_point(EntryPoint::PreUpgrade, attr, item)
26}
27
28/// Export the function as the post_upgrade hook of the canister.
29#[proc_macro_attribute]
30pub fn post_upgrade(attr: TokenStream, item: TokenStream) -> TokenStream {
31    process_entry_point(EntryPoint::PostUpgrade, attr, item)
32}
33
34/// Export the function as the inspect_message hook of the canister.
35#[proc_macro_attribute]
36pub fn inspect_message(attr: TokenStream, item: TokenStream) -> TokenStream {
37    process_entry_point(EntryPoint::InspectMessage, attr, item)
38}
39
40/// Export the function as the heartbeat hook of the canister.
41#[proc_macro_attribute]
42pub fn heartbeat(attr: TokenStream, item: TokenStream) -> TokenStream {
43    process_entry_point(EntryPoint::Heartbeat, attr, item)
44}
45
46/// Export an update method for the canister.
47#[proc_macro_attribute]
48pub fn update(attr: TokenStream, item: TokenStream) -> TokenStream {
49    process_entry_point(EntryPoint::Update, attr, item)
50}
51
52/// Export a query method for the canister.
53#[proc_macro_attribute]
54pub fn query(attr: TokenStream, item: TokenStream) -> TokenStream {
55    process_entry_point(EntryPoint::Query, attr, item)
56}