-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve pallet template generation (#261)
* Interactive interface added * Update pick_options_and_give_name macro to handle an arbitrary amount of cases, allowing pick several options for the same object * New macro for multiselect from an enum. Improve the CLI to support more options * Adding the possibility of using custom origins * Adding Cargo.toml generation * Template lib.rs. Pending of review * Finishing lib (and related) templates * New mock config * Finishing templates * Adding some tests for the folder structure * Formatting repo * Solving mock implementation of common types when no default config issue * Adding more links to Polkadot-SDK-docs * Formatting lib.rs.templ * Solving small issues * Solving doc test issue, the struct used in that test was modified by this PR * Apply suggestions from code review Comment corrections by Bruno Co-authored-by: Bruno Galvao <[email protected]> * Adding simple/advanced modes * Adding advanced mode * Adding advanced mode * Formatting and solving conflicts * Solving small template issues * Formatting the template after generation * Formatting pallet * Updating template generation * Improving manifest test suite * Allowing storage/common_types from CLI + template improvement * Solving bug related to PR #277: Regenerating a crate with POP that's already in the workspace doesn't include it again * Finishing templates with examples; solving some small bugs produced when a pallet is generated from a workspace * Finishing templates with examples; solving some small bugs produced when a pallet is generated from a workspace * Deleting legacy file * Improving UX * Applying suggested changes * Applying suggested changes * Adding a whiteline to lib.rs.templ for formatting, not so important * Not including default config if config trait empty * Improve advanced help examples * Improve advanced help examples * Improve advanced help examples * Applying suggestions --------- Co-authored-by: Bruno Galvao <[email protected]>
- Loading branch information
1 parent
e829471
commit 43b592b
Showing
33 changed files
with
1,673 additions
and
219 deletions.
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 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 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 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
/// A macro to facilitate the select multiple variant of an enum and store them inside a `Vec`. | ||
/// # Arguments | ||
/// * `$enum`: The enum type to be iterated over for the selection. This enum must implement | ||
/// `IntoEnumIterator` and `EnumMessage` traits from the `strum` crate. Each variant is | ||
/// responsible of its own messages. | ||
/// * `$prompt_message`: The message displayed to the user. It must implement the `Display` trait. | ||
/// * `$excluded_variants`: If the enum contain variants that shouldn't be included in the | ||
/// multiselect pick, they're specified here. This is useful if a enum is used in a few places and | ||
/// not all of them need all the variants but share some of them. It has to be a `Vec`; | ||
/// # Note | ||
/// This macro only works with a 1-byte sized enums, this is, fieldless enums with at most 255 | ||
/// elements each. This is because we're just interested in letting the user to pick some options | ||
/// among a predefined set, then the name should be descriptive enough, and 1-byte sized enums are | ||
/// really easy to convert to and from a `u8`, so we can work with `u8` all the time and just | ||
/// recover the variant at the end. | ||
/// | ||
/// The decision of using 1-byte enums instead of just fieldless enums is for simplicity: we won't | ||
/// probably offer a user to pick from > 256 options. If this macro is used with enums containing | ||
/// fields, the conversion to `u8` will simply be detected at compile time and the compilation will | ||
/// fail. If this macro is used with fieldless enums greater than 1-byte (really weird but | ||
/// possible), the conversion to u8 will overflow and lead to unexpected behavior, so we panic at | ||
/// runtime if that happens for completeness. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use strum::{IntoEnumIterator, EnumMessage}; | ||
/// use strum_macros::{EnumIter, EnumMessage as EnumMessageDerive}; | ||
/// use cliclack::{multiselect}; | ||
/// use pop_common::multiselect_pick; | ||
/// | ||
/// #[derive(Debug, EnumIter, EnumMessageDerive, Copy, Clone)] | ||
/// enum FieldlessEnum { | ||
/// #[strum(message = "Type 1", detailed_message = "Detailed message for Type 1")] | ||
/// Type1, | ||
/// #[strum(message = "Type 2", detailed_message = "Detailed message for Type 2")] | ||
/// Type2, | ||
/// #[strum(message = "Type 3", detailed_message = "Detailed message for Type 3")] | ||
/// Type3, | ||
/// } | ||
/// | ||
/// fn test_function() -> Result<(),std::io::Error>{ | ||
/// let vec = multiselect_pick!(FieldlessEnum, "Hello, world!"); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
/// | ||
/// # Requirements | ||
/// | ||
/// This macro requires the following imports to function correctly: | ||
/// | ||
/// ```rust | ||
/// use cliclack::{multiselect}; | ||
/// use strum::{EnumMessage, IntoEnumIterator}; | ||
/// ``` | ||
/// | ||
/// Additionally, this macro handle results, so it must be used inside a function doing so. | ||
/// Otherwise the compilation will fail. | ||
#[macro_export] | ||
macro_rules! multiselect_pick { | ||
($enum: ty, $prompt_message: expr $(, $excluded_variants: expr)?) => {{ | ||
// Ensure the enum is 1-byte long. This is needed cause fieldless enums with > 256 elements | ||
// will lead to unexpected behavior as the conversion to u8 for them isn't detected as wrong | ||
// at compile time. Enums containing variants with fields will be catched at compile time. | ||
// Weird but possible. | ||
assert_eq!(std::mem::size_of::<$enum>(), 1); | ||
let mut prompt = multiselect(format!( | ||
"{} {}", | ||
$prompt_message, | ||
"Pick an option by pressing the spacebar. Press enter when you're done!" | ||
)) | ||
.required(false); | ||
|
||
for variant in <$enum>::iter() { | ||
$(if $excluded_variants.contains(&variant){continue; })? | ||
prompt = prompt.item( | ||
variant as u8, | ||
variant.get_message().unwrap_or_default(), | ||
variant.get_detailed_message().unwrap_or_default(), | ||
); | ||
} | ||
|
||
// The unsafe block is safe cause the bytes are the discriminants of the enum picked above, | ||
// qed; | ||
prompt | ||
.interact()? | ||
.iter() | ||
.map(|byte| unsafe { std::mem::transmute(*byte) }) | ||
.collect::<Vec<$enum>>() | ||
}}; | ||
} |
This file contains 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
#[cfg(feature = "contract")] | ||
pub mod contracts; | ||
pub mod helpers; |
This file contains 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 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
Oops, something went wrong.