Make hooks and call attributes optional in pallet macro (#8853)

* Make #[pallet::hooks] optional

* Make #[pallet::call] optional

* Remove unused imports

* Update UI test expectations

* Update UI test expectations

* Remove unnecessary HooksDef::empty method

* Remove unnecessary CallDef::empty method

* Clarify what would happen when no call or hooks are specified in a pallet
This commit is contained in:
Keith Yeung
2021-05-20 12:31:56 -07:00
committed by GitHub
parent 029b8a1d07
commit e5954cf863
9 changed files with 90 additions and 57 deletions
@@ -45,6 +45,7 @@ pub struct CallDef {
pub docs: Vec<syn::Lit>,
}
#[derive(Clone)]
/// Definition of dispatchable typically: `#[weight...] fn foo(origin .., param1: ...) -> ..`
pub struct CallVariantDef {
/// Function name.
@@ -45,8 +45,8 @@ pub struct Def {
pub item: syn::ItemMod,
pub config: config::ConfigDef,
pub pallet_struct: pallet_struct::PalletStructDef,
pub hooks: hooks::HooksDef,
pub call: call::CallDef,
pub hooks: Option<hooks::HooksDef>,
pub call: Option<call::CallDef>,
pub storages: Vec<storage::StorageDef>,
pub error: Option<error::ErrorDef>,
pub event: Option<event::EventDef>,
@@ -156,9 +156,8 @@ impl Def {
config: config.ok_or_else(|| syn::Error::new(item_span, "Missing `#[pallet::config]`"))?,
pallet_struct: pallet_struct
.ok_or_else(|| syn::Error::new(item_span, "Missing `#[pallet::pallet]`"))?,
hooks: hooks
.ok_or_else(|| syn::Error::new(item_span, "Missing `#[pallet::hooks]`"))?,
call: call.ok_or_else(|| syn::Error::new(item_span, "Missing `#[pallet::call]"))?,
hooks,
call,
extra_constants,
genesis_config,
genesis_build,
@@ -206,10 +205,14 @@ impl Def {
/// instance iff it is defined with instance.
fn check_instance_usage(&self) -> syn::Result<()> {
let mut instances = vec![];
instances.extend_from_slice(&self.call.instances[..]);
instances.extend_from_slice(&self.pallet_struct.instances[..]);
instances.extend_from_slice(&self.hooks.instances[..]);
instances.extend(&mut self.storages.iter().flat_map(|s| s.instances.clone()));
if let Some(call) = &self.call {
instances.extend_from_slice(&call.instances[..]);
}
if let Some(hooks) = &self.hooks {
instances.extend_from_slice(&hooks.instances[..]);
}
if let Some(event) = &self.event {
instances.extend_from_slice(&event.instances[..]);
}