mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 23:37:56 +00:00
Allow pallet error enum variants to contain fields (#10242)
* Allow pallet errors to contain at most one field * Update docs on pallet::error * Reword documentation * cargo fmt * Introduce CompactPalletError trait and require #[pallet::error] fields to implement them * cargo fmt * Do not assume tuple variants * Add CompactPalletError derive macro * Check for error type compactness in construct_runtime * cargo fmt * Derive CompactPalletError instead of implementing it directly during macro expansion * Implement CompactPalletError on OptionBool instead of Option<bool> * Check for type idents instead of variant ident * Add doc comments for ErrorCompactnessTest * Add an trait implementation of ErrorCompactnessTest for () * Convert the error field of DispatchError to a 4-element byte array * Add static check for pallet error size * Rename to MAX_PALLET_ERROR_ENCODED_SIZE * Remove ErrorCompactnessTest trait * Remove check_compactness * Return only the most significant byte when constructing a custom InvalidTransaction * Rename CompactPalletError to PalletError * Use counter to generate unique idents for assert macros * Make declarative pallet macros compile with pallet error size checks * Remove unused doc comment * Try and fix build errors * Fix build errors * Add macro_use for some test modules * Test fix * Fix compilation errors * Remove unneeded #[macro_use] * Resolve import ambiguity * Make path to pallet Error enum more specific * Fix test expectation * Disambiguate imports * Fix test expectations * Revert appending pallet module name to path * Rename bags_list::list::Error to BagError * Fixes * Fixes * Fixes * Fix test expectations * Fix test expectation * Add more implementations for PalletError * Lift the 1-field requirement for nested pallet errors * Fix UI test expectation * Remove PalletError impl for OptionBool * Use saturating operations * cargo fmt * Delete obsolete test * Fix test expectation * Try and use assert macro in const context * Pull out the pallet error size check macro * Fix UI test for const assertion * cargo fmt * Apply clippy suggestion * Fix doc comment * Docs for create_tt_return_macro * Ensure TryInto is imported in earlier Rust editions * Apply suggestions from code review Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Fix up comments and names * Implement PalletError for Never * cargo fmt * Don't compile example code * Bump API version for block builder * Factor in codec attributes while derving PalletError * Rename module and fix unit test * Add missing attribute * Check API version and convert ApplyExtrinsicResult accordingly * Rename BagError to ListError Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Use codec crate re-exported from frame support * Add links to types mentioned in doc comments Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * cargo fmt * cargo fmt * Re-add attribute for hidden docs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
@@ -18,20 +18,26 @@
|
||||
use super::helper;
|
||||
use frame_support_procedural_tools::get_doc_literals;
|
||||
use quote::ToTokens;
|
||||
use syn::spanned::Spanned;
|
||||
use syn::{spanned::Spanned, Fields};
|
||||
|
||||
/// List of additional token to be used for parsing.
|
||||
mod keyword {
|
||||
syn::custom_keyword!(Error);
|
||||
}
|
||||
|
||||
/// Records information about the error enum variants.
|
||||
pub struct VariantField {
|
||||
/// Whether or not the field is named, i.e. whether it is a tuple variant or struct variant.
|
||||
pub is_named: bool,
|
||||
}
|
||||
|
||||
/// This checks error declaration as a enum declaration with only variants without fields nor
|
||||
/// discriminant.
|
||||
pub struct ErrorDef {
|
||||
/// The index of error item in pallet module.
|
||||
pub index: usize,
|
||||
/// Variants ident and doc literals (ordered as declaration order)
|
||||
pub variants: Vec<(syn::Ident, Vec<syn::Lit>)>,
|
||||
/// Variants ident, optional field and doc literals (ordered as declaration order)
|
||||
pub variants: Vec<(syn::Ident, Option<VariantField>, Vec<syn::Lit>)>,
|
||||
/// A set of usage of instance, must be check for consistency with trait.
|
||||
pub instances: Vec<helper::InstanceUsage>,
|
||||
/// The keyword error used (contains span).
|
||||
@@ -70,18 +76,19 @@ impl ErrorDef {
|
||||
.variants
|
||||
.iter()
|
||||
.map(|variant| {
|
||||
if !matches!(variant.fields, syn::Fields::Unit) {
|
||||
let msg = "Invalid pallet::error, unexpected fields, must be `Unit`";
|
||||
return Err(syn::Error::new(variant.fields.span(), msg))
|
||||
}
|
||||
let field_ty = match &variant.fields {
|
||||
Fields::Unit => None,
|
||||
Fields::Named(_) => Some(VariantField { is_named: true }),
|
||||
Fields::Unnamed(_) => Some(VariantField { is_named: false }),
|
||||
};
|
||||
if variant.discriminant.is_some() {
|
||||
let msg = "Invalid pallet::error, unexpected discriminant, discriminant \
|
||||
let msg = "Invalid pallet::error, unexpected discriminant, discriminants \
|
||||
are not supported";
|
||||
let span = variant.discriminant.as_ref().unwrap().0.span();
|
||||
return Err(syn::Error::new(span, msg))
|
||||
}
|
||||
|
||||
Ok((variant.ident.clone(), get_doc_literals(&variant.attrs)))
|
||||
Ok((variant.ident.clone(), field_ty, get_doc_literals(&variant.attrs)))
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user