Support extra constant renaming (#9814)

* Rebased with master. Resolved merge conflict in
frame/support/test/tests/pallet.rs

* Switching Account ID to SomeType1, as SomeType3 was giving me conversion error.

* Wrong indent config.  Fixed.

* These tabs look fine locally, but look different on Github.  Trying to get the style config right.

* Parsing pallet::constant_name.
Passing unit tests, which is confusing because I didn't change `ident` in the ExtraConstantDef initialization.

* Finalized parsing of extra constant name by adding optional metadata field.
Added expansion logic that replaces respective `idents` where they exist.

* Erasing this to try to keep the format the same across the source code.

* Another formatting change for consistency.

* Update frame/support/procedural/src/pallet/expand/constants.rs

strictly more idiomatic.

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/procedural/src/pallet/parse/extra_constants.rs

strictly idiomatic change.

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Fixing formatting and CI warnings.

* switched to nightly compiler to use rustfmt.toml

Co-authored-by: Eric Miller <emiller@lirio.co>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Eric Miller
2021-10-07 19:02:09 -04:00
committed by GitHub
parent e81a425241
commit b564f4f031
4 changed files with 65 additions and 3 deletions
@@ -28,6 +28,7 @@ mod keyword {
syn::custom_keyword!(compact);
syn::custom_keyword!(T);
syn::custom_keyword!(pallet);
syn::custom_keyword!(constant_name);
}
/// Definition of extra constants typically `impl<T: Config> Pallet<T> { ... }`
@@ -50,6 +51,29 @@ pub struct ExtraConstantDef {
pub type_: syn::Type,
/// The doc associated
pub doc: Vec<syn::Lit>,
/// Optional MetaData Name
pub metadata_name: Option<syn::Ident>,
}
/// Attributes for functions in extra_constants impl block.
/// Parse for `#[pallet::constant_name(ConstantName)]`
pub struct ExtraConstAttr {
metadata_name: syn::Ident,
}
impl syn::parse::Parse for ExtraConstAttr {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
input.parse::<syn::Token![#]>()?;
let content;
syn::bracketed!(content in input);
content.parse::<keyword::pallet>()?;
content.parse::<syn::Token![::]>()?;
content.parse::<keyword::constant_name>()?;
let metadata_name;
syn::parenthesized!(metadata_name in content);
Ok(ExtraConstAttr { metadata_name: metadata_name.parse::<syn::Ident>()? })
}
}
impl ExtraConstantsDef {
@@ -57,7 +81,10 @@ impl ExtraConstantsDef {
let item = if let syn::Item::Impl(item) = item {
item
} else {
return Err(syn::Error::new(item.span(), "Invalid pallet::call, expected item impl"))
return Err(syn::Error::new(
item.span(),
"Invalid pallet::extra_constants, expected item impl",
))
};
let mut instances = vec![];
@@ -102,10 +129,23 @@ impl ExtraConstantsDef {
syn::ReturnType::Type(_, type_) => *type_.clone(),
};
// parse metadata_name
let mut extra_constant_attrs: Vec<ExtraConstAttr> =
helper::take_item_pallet_attrs(method)?;
if extra_constant_attrs.len() > 1 {
let msg =
"Invalid attribute in pallet::constant_name, only one attribute is expected";
return Err(syn::Error::new(extra_constant_attrs[1].metadata_name.span(), msg))
}
let metadata_name = extra_constant_attrs.pop().map(|attr| attr.metadata_name);
extra_constants.push(ExtraConstantDef {
ident: method.sig.ident.clone(),
type_,
doc: get_doc_literals(&method.attrs),
metadata_name,
});
}
@@ -139,6 +139,12 @@ impl MutItemAttrs for syn::ItemMod {
}
}
impl MutItemAttrs for syn::ImplItemMethod {
fn mut_item_attrs(&mut self) -> Option<&mut Vec<syn::Attribute>> {
Some(&mut self.attrs)
}
}
/// Parse for `()`
struct Unit;
impl syn::parse::Parse for Unit {