Repository: paritytech/substrate
File: primitives/api/proc-macro/src/decl_runtime_apis.rs (Rust)

Code before:

			if !input.is_empty() {
				return Err(input.error("No more arguments expected"))
			}

			Ok((old_name.value(), version.base10_parse()?))
		})
		.map_err(|_| err())
}

/// Generate the declaration of the trait for the runtime.
fn generate_runtime_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
	let mut result = Vec::new();

	for decl in decls {
		let mut decl = decl.clone();
		let decl_span = decl.span();
		extend_generics_with_block(&mut decl.generics);
		let mod_name = generate_runtime_mod_name_for_trait(&decl.ident);
		let found_attributes = remove_supported_attributes(&mut decl.attrs);
		let api_version =
			get_api_version(&found_attributes).map(|v| generate_runtime_api_version(v as u32))?;
		let id = generate_runtime_api_id(&decl.ident.to_string());

		#[cfg(feature = "frame-metadata")]
		let metadata = generate_decl_runtime_metadata(&decl);
		#[cfg(not(feature = "frame-metadata"))]
		let metadata = quote!();

		let trait_api_version = get_api_version(&found_attributes)?;

		let mut methods_by_version: BTreeMap<u64, Vec<TraitItemFn>> = BTreeMap::new();

		// Process the items in the declaration. The filter_map function below does a lot of stuff
		// because the method attributes are stripped at this point
		decl.items.iter_mut().for_each(|i| match i {
			TraitItem::Fn(ref mut method) => {
				let method_attrs = remove_supported_attributes(&mut method.attrs);
				let mut method_version = trait_api_version;
				// validate the api version for the method (if any) and generate default
				// implementation for versioned methods
				if let Some(version_attribute) = method_attrs.get(API_VERSION_ATTRIBUTE) {
					method_version = match parse_runtime_api_version(version_attribute) {
						Ok(method_api_ver) if method_api_ver < trait_api_version => {
							let method_ver = method_api_ver.to_string();
							let trait_ver = trait_api_version.to_string();
							let mut err1 = Error::new(
								version_attribute.span(),
								format!(
										"Method version `{}` is older than (or equal to) trait version `{}`.\
										 Methods can't define versions older than the trait version.",

Review comment (suggestion):
```suggestion
		let metadata = crate::runtime_metadata::generate_decl_runtime_metadata(&decl);
```

Then you can remove

```rust
#[cfg(feature = "frame-metadata")]
use crate::runtime_metadata::generate_decl_runtime_metadata;
```

altogether and there will be only one `#[cfg(feature = "frame-metadata")]` necessary instead of two. (Same for the other file.)

Code after:
				return Err(input.error("No more arguments expected"))
			}

			Ok((old_name.value(), version.base10_parse()?))
		})
		.map_err(|_| err())
}

/// Generate the declaration of the trait for the runtime.
fn generate_runtime_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
	let mut result = Vec::new();

	for decl in decls {
		let mut decl = decl.clone();
		let decl_span = decl.span();
		extend_generics_with_block(&mut decl.generics);
		let mod_name = generate_runtime_mod_name_for_trait(&decl.ident);
		let found_attributes = remove_supported_attributes(&mut decl.attrs);
		let api_version =
			get_api_version(&found_attributes).map(|v| generate_runtime_api_version(v as u32))?;
		let id = generate_runtime_api_id(&decl.ident.to_string());

		#[cfg(feature = "frame-metadata")]
		let metadata = crate::runtime_metadata::generate_decl_runtime_metadata(&decl);
		#[cfg(not(feature = "frame-metadata"))]
		let metadata = quote!();

		let trait_api_version = get_api_version(&found_attributes)?;

		let mut methods_by_version: BTreeMap<u64, Vec<TraitItemFn>> = BTreeMap::new();

		// Process the items in the declaration. The filter_map function below does a lot of stuff
		// because the method attributes are stripped at this point
		decl.items.iter_mut().for_each(|i| match i {
			TraitItem::Fn(ref mut method) => {
				let method_attrs = remove_supported_attributes(&mut method.attrs);
				let mut method_version = trait_api_version;
				// validate the api version for the method (if any) and generate default
				// implementation for versioned methods
				if let Some(version_attribute) = method_attrs.get(API_VERSION_ATTRIBUTE) {
					method_version = match parse_runtime_api_version(version_attribute) {
						Ok(method_api_ver) if method_api_ver < trait_api_version => {
							let method_ver = method_api_ver.to_string();
							let trait_ver = trait_api_version.to_string();
							let mut err1 = Error::new(
								version_attribute.span(),
								format!(
										"Method version `{}` is older than (or equal to) trait version `{}`.\
										 Methods can't define versions older than the trait version.",
										method_ver,
										trait_ver,