-
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 all 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Derive macro for creating XCMs with a builder pattern | ||
|
|
||
| use inflector::Inflector; | ||
| use proc_macro::TokenStream; | ||
| use proc_macro2::TokenStream as TokenStream2; | ||
| use quote::{format_ident, quote}; | ||
| use syn::{ | ||
| parse_macro_input, Data, DeriveInput, Error, Expr, ExprLit, Fields, Lit, Meta, MetaNameValue, | ||
| }; | ||
|
|
||
| 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 = &variant_name.to_string().to_snake_case(); | ||
| let method_name = syn::Ident::new(&method_name_string, variant_name.span()); | ||
| let docs: Vec<_> = variant | ||
| .attrs | ||
| .iter() | ||
| .filter_map(|attr| match &attr.meta { | ||
| Meta::NameValue(MetaNameValue { | ||
| value: Expr::Lit(ExprLit { lit: Lit::Str(literal), .. }), | ||
| .. | ||
| }) if attr.path().is_ident("doc") => Some(literal.value()), | ||
| _ => None, | ||
| }) | ||
| .map(|doc| syn::parse_str::<TokenStream2>(&format!("/// {}", doc)).unwrap()) | ||
| .collect(); | ||
| let method = 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 | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| quote! { | ||
| #(#docs)* | ||
| #method | ||
| } | ||
| }); | ||
| let output = quote! { | ||
| impl<Call> XcmBuilder<Call> { | ||
| #(#methods)* | ||
|
|
||
| pub fn build(self) -> Xcm<Call> { | ||
| Xcm(self.0) | ||
| } | ||
| } | ||
| }; | ||
| output | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! UI tests for XCM procedural macros | ||
|
|
||
| #[cfg(not(feature = "disable-ui-tests"))] | ||
| #[test] | ||
| fn ui() { | ||
| // Only run the ui tests when `RUN_UI_TESTS` is set. | ||
| if std::env::var("RUN_UI_TESTS").is_err() { | ||
| return; | ||
| } | ||
|
|
||
| // As trybuild is using `cargo check`, we don't need the real WASM binaries. | ||
| std::env::set_var("SKIP_WASM_BUILD", "1"); | ||
|
|
||
| let t = trybuild::TestCases::new(); | ||
| t.compile_fail("tests/ui/*.rs"); | ||
| } |
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,25 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Test error when attaching the derive builder macro to something | ||
| //! other than the XCM `Instruction` enum. | ||
|
|
||
| use xcm_procedural::Builder; | ||
|
|
||
| #[derive(Builder)] | ||
| struct SomeStruct; | ||
|
|
||
| fn main() {} |
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,5 @@ | ||
| error: Expected the `Instruction` enum | ||
| --> tests/ui/builder_pattern.rs:23:1 | ||
| | | ||
| 23 | struct SomeStruct; | ||
| | ^^^^^^^^^^^^^^^^^^ |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Schema: Parity PR Documentation Schema (prdoc) | ||
| # See doc at https://github.com/paritytech/prdoc | ||
|
|
||
| title: Add a builder pattern to create XCM programs | ||
|
|
||
| doc: | ||
| - audience: Core Dev | ||
| description: | | ||
| XCMs can now be built using a builder pattern like so: | ||
| Xcm::builder() | ||
| .withdraw_asset(assets) | ||
| .buy_execution(fees, weight_limit) | ||
| .deposit_asset(assets, beneficiary) | ||
| .build(); | ||
|
|
||
| migrations: | ||
| db: [] | ||
|
|
||
| runtime: [] | ||
|
|
||
| crates: | ||
| - name: xcm | ||
|
|
||
| host_functions: [] |
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.