Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Factor in module path while generating enum variant or fn names
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang committed May 17, 2021
1 parent 1f8e903 commit ba58289
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
12 changes: 6 additions & 6 deletions frame/support/procedural/src/construct_runtime/expand/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn expand_event_variant(
generics: &Generics,
) -> TokenStream {
let pallet_is_generic = !generics.params.is_empty();
let mod_name = &path.inner.segments.last().unwrap().ident;
let mod_name = &path.mod_name();

match (instance, pallet_is_generic) {
(Some(inst), true) => {
Expand All @@ -125,11 +125,11 @@ fn expand_event_conversion(
instance: Option<&Ident>,
pallet_event: &TokenStream,
) -> TokenStream {
let mod_name = &path.inner.segments.last().unwrap().ident;
let variant = if let Some(inst) = &instance {
let mod_name = path.mod_name();
let variant = if let Some(inst) = instance {
format_ident!("{}_{}", mod_name, inst)
} else {
mod_name.clone()
mod_name
};

quote!{
Expand All @@ -156,7 +156,7 @@ fn expand_event_metadata(
path: &PalletPath,
pallet_event: &TokenStream,
) -> TokenStream {
let mod_name = &path.inner.segments.last().unwrap().ident;
let mod_name = path.mod_name();

quote!{(stringify!(#mod_name), #scrate::event::FnEncode(#pallet_event::metadata)),}
}
Expand All @@ -167,7 +167,7 @@ fn expand_pallet_event_fn(
instance: Option<&Ident>,
pallet_event: &TokenStream,
) -> TokenStream {
let mod_name = &path.inner.segments.last().unwrap().ident;
let mod_name = path.mod_name();
let fn_name = if let Some(inst) = instance {
format_ident!("__module_events_{}_{}", mod_name, inst)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn expand_pallet_metadata_events(
decl: &Pallet,
) -> TokenStream {
if filtered_names.contains(&"Event") {
let mod_name = &decl.pallet.inner.segments.last().unwrap().ident;
let mod_name = decl.pallet.mod_name();
let event = if let Some(instance) = decl.instance.as_ref() {
format_ident!("__module_events_{}_{}", mod_name, instance)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ fn expand_origin_caller_variant(
generics: &Generics,
) -> TokenStream {
let pallet_is_generic = !generics.params.is_empty();
let mod_name = &path.inner.segments.last().unwrap().ident;
let mod_name = &path.mod_name();

match (instance, pallet_is_generic) {
(Some(inst), true) => {
Expand All @@ -284,11 +284,11 @@ fn expand_origin_pallet_conversions(
instance: Option<&Ident>,
generics: &Generics,
) -> TokenStream {
let mod_name = &path.inner.segments.last().unwrap().ident;
let variant = if let Some(inst) = &instance {
let mod_name = path.mod_name();
let variant = if let Some(inst) = instance {
format_ident!("{}_{}", mod_name, inst)
} else {
mod_name.clone()
mod_name
};

let pallet_is_generic = !generics.params.is_empty();
Expand Down
15 changes: 15 additions & 0 deletions frame/support/procedural/src/construct_runtime/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ impl Parse for PalletPath {
}
}

impl PalletPath {
/// Return the snake-cased module name for this path.
pub fn mod_name(&self) -> Ident {
let mut iter = self.inner.segments.iter();
let mut mod_name =
iter.next().expect("Path should always have 1 segment; qed").ident.clone();

for segment in iter {
mod_name = quote::format_ident!("{}_{}", mod_name, segment.ident);
}

mod_name
}
}

impl quote::ToTokens for PalletPath {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.inner.to_tokens(tokens);
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/construct_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn origin_codec() {
let origin = OriginCaller::module1_Instance2(module1::Origin(Default::default()));
assert_eq!(origin.encode()[0], 33);

let origin = OriginCaller::module3(nested::module3::Origin);
let origin = OriginCaller::nested_module3(nested::module3::Origin);
assert_eq!(origin.encode()[0], 34);

let origin = OriginCaller::module1_Instance6(module1::Origin(Default::default()));
Expand Down

0 comments on commit ba58289

Please sign in to comment.