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

Code before:
	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
			.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;

Review comment (suggestion):
What should possibly happen is to have this function return a `syn::Result<bool>` instead of making it mutable upon `storage_def`, then we'll have a cleaner implementation.

Code after:
	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() {
			let pallet_attr: Option<PalletAttr> = helper::take_first_item_pallet_attr(item)?;

			match pallet_attr {
				Some(PalletAttr::Config(span)) if config.is_none() =>
					config = Some(config::ConfigDef::try_from(&frame_system, span, index, item)?),
				Some(PalletAttr::Pallet(span)) if pallet_struct.is_none() => {
					let p = pallet_struct::PalletStructDef::try_from(span, index, item)?;
					pallet_struct = Some(p);