Repository: paritytech/substrate
File: frame/support/procedural/src/pallet/parse/mod.rs (Rust)

Code before:

/// Parsed definition of a pallet.
pub struct Def {
	/// The module items.
	/// (their order must not be modified because they are registered in individual definitions).
	pub item: syn::ItemMod,
	pub config: config::ConfigDef,
	pub pallet_struct: pallet_struct::PalletStructDef,
	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>,
	pub origin: Option<origin::OriginDef>,
	pub inherent: Option<inherent::InherentDef>,
	pub genesis_config: Option<genesis_config::GenesisConfigDef>,
	pub genesis_build: Option<genesis_build::GenesisBuildDef>,
	pub validate_unsigned: Option<validate_unsigned::ValidateUnsignedDef>,
	pub extra_constants: Option<extra_constants::ExtraConstantsDef>,
	pub type_values: Vec<type_value::TypeValueDef>,
	pub frame_system: syn::Ident,
	pub frame_support: syn::Ident,
}

impl Def {
	fn mark_benchmarking_cached_calls(storage_def: &mut StorageDef, item: &mut syn::Item) {
		if let syn::Item::Type(typ) = item {
			for attr in typ.attrs.as_slice() {
				if attr.style == AttrStyle::Outer {
					if let Some(seg) = attr.path.segments.last() {
						if let Ok(_) = syn::parse2::<keyword::benchmarking>(seg.to_token_stream()) {
							if let Ok(group) = syn::parse2::<Group>(attr.tokens.clone()) {
								if let Ok(_) = syn::parse2::<keyword::cached>(group.stream()) {
									storage_def.benchmarking_cached = true;
									break
								}
							}
						}
					}
				}
			}
		}
	}

	pub fn try_from(mut item: syn::ItemMod) -> syn::Result<Self> {
		let frame_system = generate_crate_access_2018("frame-system")?;
		let frame_support = generate_crate_access_2018("frame-support")?;

		let item_span = item.span();
		let items = &mut item
			.content

Review comment (suggestion):
This code is also way to overcomplicated.

```
if attr.path.is_ident("benchmarking") { attr.parse_args::<keyword::caching>()? }
```

Code after:
	/// (their order must not be modified because they are registered in individual definitions).
	pub item: syn::ItemMod,
	pub config: config::ConfigDef,
	pub pallet_struct: pallet_struct::PalletStructDef,
	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>,
	pub origin: Option<origin::OriginDef>,
	pub inherent: Option<inherent::InherentDef>,
	pub genesis_config: Option<genesis_config::GenesisConfigDef>,
	pub genesis_build: Option<genesis_build::GenesisBuildDef>,
	pub validate_unsigned: Option<validate_unsigned::ValidateUnsignedDef>,
	pub extra_constants: Option<extra_constants::ExtraConstantsDef>,
	pub type_values: Vec<type_value::TypeValueDef>,
	pub frame_system: syn::Ident,
	pub frame_support: syn::Ident,
}

impl Def {
	pub fn try_from(mut item: syn::ItemMod) -> syn::Result<Self> {
		let frame_system = generate_crate_access_2018("frame-system")?;
		let frame_support = generate_crate_access_2018("frame-support")?;

		let item_span = item.span();
		let items = &mut item
			.content
			.as_mut()
			.ok_or_else(|| {
				let msg = "Invalid pallet definition, expected mod to be inlined.";
				syn::Error::new(item_span, msg)
			})?
			.1;

		let mut config = None;
		let mut pallet_struct = None;
		let mut hooks = None;
		let mut call = None;
		let mut error = None;
		let mut event = None;
		let mut origin = None;
		let mut inherent = None;
		let mut genesis_config = None;
		let mut genesis_build = None;
		let mut validate_unsigned = None;
		let mut extra_constants = None;
		let mut storages = vec![];
		let mut type_values = vec![];

		for (index, item) in items.iter_mut().enumerate() {