1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
//! This crate provide a set of attribute macros to faciliate canister development.
//!
//! The macros fall into two categories:
//! * To register functions as canister entry points
//! * To import another canister as a rust struct for inter-canister operation.
//!
//! ## Register functions as canister entry points
//!
//! These macros are directly related to the [Internet Computer Specification](https://smartcontracts.org/docs/interface-spec/index.html#_entry_points).
//!
//! * [`init`](attr.init.html)
//! * [`pre_upgrade`](attr.pre_upgrade.html)
//! * [`post_upgrade`](attr.post_upgrade.html)
//! * [`inspect_message`](attr.inspect_message.html)
//! * [`heartbeat`](attr.heartbeat.html)
//! * [`update`](attr.update.html)
//! * [`query`](attr.query.html)
//!
//! ## Import another canister as a rust struct
//!
//! * [`import`](attr.import.html)
use proc_macro::TokenStream;
use std::sync::atomic::{AtomicU32, Ordering};
use syn::Error;
mod export;
mod import;
// To generate unique identifiers for functions and arguments
static NEXT_ID: AtomicU32 = AtomicU32::new(0);
pub(crate) fn id() -> u32 {
NEXT_ID.fetch_add(1, Ordering::SeqCst)
}
fn handle_debug_and_errors<F>(
cb: F,
name: &str,
attr: TokenStream,
item: TokenStream,
) -> TokenStream
where
F: FnOnce(
proc_macro2::TokenStream,
proc_macro2::TokenStream,
) -> Result<proc_macro2::TokenStream, Error>,
{
if std::env::var_os("IC_CDK_DEBUG").is_some() {
eprintln!("--- IC_CDK_MACROS DEBUG ---");
eprintln!("{}\n attr: {}\n item: {}", name, attr, item);
}
let result = cb(attr.into(), item.into());
if std::env::var_os("IC_CDK_DEBUG").is_some() {
eprintln!("--------- RESULT ---------");
if let Ok(ref stream) = result {
eprintln!("{}", stream);
}
eprintln!("---------------------------");
}
result.map_or_else(|e| e.to_compile_error().into(), Into::into)
}
/// Register a query call entry point.
///
/// This attribute macro will export a function with name `canister_query <name>`
/// in the canister module.
///
/// # Example
///
/// ```rust
/// # use ic_cdk_macros::query;
/// #[query]
/// fn query_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
///
/// You can also specify the name of the exported function.
///
/// ```rust
/// # use ic_cdk_macros::*;
/// #[query(name = "some_name")]
/// fn query_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
#[proc_macro_attribute]
pub fn query(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(export::ic_query, "ic_query", attr, item)
}
/// Register an update call entry point.
///
/// This attribute macro will export a function with name `canister_update <name>`
/// in the canister module.
///
/// # Example
///
/// ```rust
/// # use ic_cdk_macros::update;
/// #[update]
/// fn update_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
///
/// You can also specify the name of the exported function.
///
/// ```rust
/// # use ic_cdk_macros::*;
/// #[update(name = "some_name")]
/// fn update_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
#[proc_macro_attribute]
pub fn update(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(export::ic_update, "ic_update", attr, item)
}
/// Register the `canister_init` entry point of a canister.
///
/// This attribute macro will export the function `canister_init`
/// in the canister module.
///
/// The function under this attribute must have no return value.
///
/// Each canister can only have one `canister_init` entry point.
///
/// # Example
///
/// ```rust
/// # use ic_cdk_macros::init;
/// #[init]
/// fn init_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
#[proc_macro_attribute]
pub fn init(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(export::ic_init, "ic_init", attr, item)
}
/// Register the `canister_pre_upgrade` entry point of a canister.
///
/// This attribute macro will export the function `canister_pre_upgrade`
/// in the canister module.
///
/// The function under this attribute must have no return value.
///
/// Each canister can only have one `canister_pre_upgrade` entry point.
///
/// # Example
///
/// ```rust
/// # use ic_cdk_macros::pre_upgrade;
/// #[pre_upgrade]
/// fn pre_upgrade_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
#[proc_macro_attribute]
pub fn pre_upgrade(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(export::ic_pre_upgrade, "ic_pre_upgrade", attr, item)
}
/// Register the `canister_post_upgrade` entry point of a canister.
///
/// This attribute macro will export the function `canister_post_upgrade`
/// in the canister module.
///
/// The function under this attribute must have no return value.
///
/// Each canister can only have one `canister_post_upgrade` entry point.
///
/// # Example
///
/// ```rust
/// # use ic_cdk_macros::post_upgrade;
/// #[post_upgrade]
/// fn post_upgrade_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
#[proc_macro_attribute]
pub fn post_upgrade(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(export::ic_post_upgrade, "ic_post_upgrade", attr, item)
}
/// Register the `canister_heartbeat` entry point of a canister.
///
/// This attribute macro will export the function `canister_heartbeat`
/// in the canister module.
///
/// The function under this attribute must have no return value.
///
/// Each canister can only have one `canister_heartbeat` entry point.
///
/// # Example
///
/// ```rust
/// # use ic_cdk_macros::heartbeat;
/// #[heartbeat]
/// fn heartbeat_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
#[proc_macro_attribute]
pub fn heartbeat(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(export::ic_heartbeat, "ic_heartbeat", attr, item)
}
/// Register the `canister_inspect_message` entry point of a canister.
///
/// This attribute macro will export the function `canister_inspect_message`
/// in the canister module.
///
/// The function under this attribute must have no return value.
///
/// Each canister can only have one `canister_inspect_message` entry point.
///
/// # Example
///
/// ```rust
/// # use ic_cdk_macros::inspect_message;
/// #[inspect_message]
/// fn inspect_message_function() {
/// // ...
/// # unimplemented!()
/// }
/// ```
#[proc_macro_attribute]
pub fn inspect_message(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(export::ic_inspect_message, "ic_inspect_message", attr, item)
}
/// Import another canister as a rust struct.
///
/// All public interfaces defined in corresponding candid file can be accessed through the annotated struct.
///
/// # Example
///
/// You can specify the canister with it's name.
///
/// Please be noted that this approach relies on the project organization by [dfx](https://github.com/dfinity/sdk).
///
/// During `dfx build`, the imported canister will be correctly resolved.
///
/// ```rust,ignore
/// # use ic_cdk_macros::import;
/// #[import(canister = "some_canister")]
/// struct SomeCanister;
/// ```
///
/// Or you can specify both the `canister_id` and the `candid_path`.
///
/// ```rust,ignore
/// # use ic_cdk_macros::import;
/// #[import(canister_id = "abcde-cai", candid_path = "path/to/some_canister.did")]
/// struct SomeCanister;
/// ```
#[proc_macro_attribute]
pub fn import(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(import::ic_import, "ic_import", attr, item)
}