-
Notifications
You must be signed in to change notification settings - Fork 1.2k
XCM builder pattern #2107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
XCM builder pattern #2107
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ad28ca9
XCM builder pattern
franciscoaguirre 0621788
Merge branch 'master' into cis-xcm-builder-pattern
franciscoaguirre 47da310
Use the inflector crate for snake casing methods
franciscoaguirre 8684208
Put instruction doc comments on top of builder methods
franciscoaguirre 4fd472d
Update polkadot/xcm/procedural/src/builder_pattern.rs
franciscoaguirre 8104124
Simplify match
franciscoaguirre 5f82cc7
Add UI tests
franciscoaguirre eca0cba
Add missing licenses
franciscoaguirre a84dad4
Add prdoc
franciscoaguirre 48285f2
Move module descriptions
franciscoaguirre 4668aa2
Update xcm-procedural Cargo.toml
franciscoaguirre 2db4fe4
Merge branch 'master' into cis-xcm-builder-pattern
franciscoaguirre d4c1b55
".git/.scripts/commands/fmt/fmt.sh"
9ca5803
Merge branch 'master' into cis-xcm-builder-pattern
franciscoaguirre 88b0d33
Move trybuild to dev dependencies
franciscoaguirre c5ad9d1
Merge branch 'master' into cis-xcm-builder-pattern
franciscoaguirre 549f2fe
Merge branch 'master' into cis-xcm-builder-pattern
franciscoaguirre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| use proc_macro::TokenStream; | ||
| use proc_macro2::TokenStream as TokenStream2; | ||
| use syn::{parse_macro_input, DeriveInput, Data, Error, Fields}; | ||
| use quote::{quote, format_ident}; | ||
|
|
||
| pub fn derive(input: TokenStream) -> TokenStream { | ||
| let input = parse_macro_input!(input as DeriveInput); | ||
| let builder_impl = match &input.data { | ||
| Data::Enum(data_enum) => generate_methods_for_enum(input.ident, data_enum), | ||
| _ => return Error::new_spanned(&input, "Expected the `Instruction` enum") | ||
| .to_compile_error().into(), | ||
| }; | ||
| let output = quote! { | ||
| pub struct XcmBuilder<Call>(Vec<Instruction<Call>>); | ||
| impl<Call> Xcm<Call> { | ||
| pub fn builder() -> XcmBuilder<Call> { | ||
| XcmBuilder::<Call>(Vec::new()) | ||
| } | ||
| } | ||
| #builder_impl | ||
| }; | ||
| output.into() | ||
| } | ||
|
|
||
| fn generate_methods_for_enum(name: syn::Ident, data_enum: &syn::DataEnum) -> TokenStream2 { | ||
| let methods = data_enum.variants.iter().map(|variant| { | ||
| let variant_name = &variant.ident; | ||
| let method_name_string = to_snake_case(&variant_name.to_string()); | ||
| let method_name = syn::Ident::new(&method_name_string, variant_name.span()); | ||
| match &variant.fields { | ||
| Fields::Unit => { | ||
| quote! { | ||
| pub fn #method_name(mut self) -> Self { | ||
franciscoaguirre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.0.push(#name::<Call>::#variant_name); | ||
| self | ||
| } | ||
| } | ||
| }, | ||
| Fields::Unnamed(fields) => { | ||
| let arg_names: Vec<_> = fields.unnamed.iter().enumerate() | ||
| .map(|(index, _)| format_ident!("arg{}", index)) | ||
| .collect(); | ||
| let arg_types: Vec<_> = fields.unnamed.iter().map(|field| &field.ty) | ||
| .collect(); | ||
| quote! { | ||
| pub fn #method_name(mut self, #(#arg_names: #arg_types),*) -> Self { | ||
| self.0.push(#name::<Call>::#variant_name(#(#arg_names),*)); | ||
| self | ||
| } | ||
| } | ||
| }, | ||
| Fields::Named(fields) => { | ||
| let arg_names: Vec<_> = fields.named.iter().map(|field| &field.ident).collect(); | ||
| let arg_types: Vec<_> = fields.named.iter().map(|field| &field.ty).collect(); | ||
| quote! { | ||
| pub fn #method_name(mut self, #(#arg_names: #arg_types),*) -> Self { | ||
| self.0.push(#name::<Call>::#variant_name { #(#arg_names),* }); | ||
| self | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| }); | ||
| let output = quote! { | ||
| impl<Call> XcmBuilder<Call> { | ||
| #(#methods)* | ||
|
|
||
| pub fn build(self) -> Xcm<Call> { | ||
| Xcm(self.0) | ||
| } | ||
| } | ||
| }; | ||
| output | ||
| } | ||
|
|
||
| fn to_snake_case(s: &str) -> String { | ||
| let mut snake = String::with_capacity(s.len()); | ||
| let mut chars = s.chars().peekable(); | ||
|
|
||
| while let Some(ch) = chars.next() { | ||
| // If it's an uppercase character and not the start | ||
| if ch.is_uppercase() && !snake.is_empty() { | ||
| // If the next character is lowercase, prepend an underscore | ||
| if chars.peek().map_or(false, |next| next.is_lowercase()) { | ||
| snake.push('_'); | ||
| } | ||
| } | ||
|
|
||
| snake.extend(ch.to_lowercase()); | ||
| } | ||
|
|
||
| snake | ||
| } | ||
franciscoaguirre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.