mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-17 03:11:01 +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:
@@ -0,0 +1,110 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Implementation of the `create_tt_return_macro` macro
|
||||
|
||||
use crate::COUNTER;
|
||||
use frame_support_procedural_tools::generate_crate_access_2018;
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::format_ident;
|
||||
|
||||
struct CreateTtReturnMacroDef {
|
||||
name: Ident,
|
||||
args: Vec<(Ident, TokenStream)>,
|
||||
}
|
||||
|
||||
impl syn::parse::Parse for CreateTtReturnMacroDef {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let name = input.parse()?;
|
||||
let _ = input.parse::<syn::Token![,]>()?;
|
||||
|
||||
let mut args = Vec::new();
|
||||
while !input.is_empty() {
|
||||
let mut value;
|
||||
let key: Ident = input.parse()?;
|
||||
let _ = input.parse::<syn::Token![=]>()?;
|
||||
let _: syn::token::Bracket = syn::bracketed!(value in input);
|
||||
let _: syn::token::Brace = syn::braced!(value in value);
|
||||
let value: TokenStream = value.parse()?;
|
||||
|
||||
args.push((key, value))
|
||||
}
|
||||
|
||||
Ok(Self { name, args })
|
||||
}
|
||||
}
|
||||
|
||||
/// A proc macro that accepts a name and any number of key-value pairs, to be used to create a
|
||||
/// declarative macro that follows tt-call conventions and simply calls [`tt_call::tt_return`],
|
||||
/// accepting an optional `frame-support` argument and returning the key-value pairs that were
|
||||
/// supplied to the proc macro.
|
||||
///
|
||||
/// # Example
|
||||
/// ```ignore
|
||||
/// __create_tt_macro! {
|
||||
/// my_tt_macro,
|
||||
/// foo = [{ bar }]
|
||||
/// }
|
||||
///
|
||||
/// // Creates the following declarative macro:
|
||||
///
|
||||
/// macro_rules! my_tt_macro {
|
||||
/// {
|
||||
/// $caller:tt
|
||||
/// $(frame_support = [{ $($frame_support:ident)::* }])?
|
||||
/// } => {
|
||||
/// frame_support::tt_return! {
|
||||
/// $caller
|
||||
/// foo = [{ bar }]
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn create_tt_return_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let CreateTtReturnMacroDef { name, args } =
|
||||
syn::parse_macro_input!(input as CreateTtReturnMacroDef);
|
||||
|
||||
let frame_support = match generate_crate_access_2018("frame-support") {
|
||||
Ok(i) => i,
|
||||
Err(e) => return e.into_compile_error().into(),
|
||||
};
|
||||
let (keys, values): (Vec<_>, Vec<_>) = args.into_iter().unzip();
|
||||
let count = COUNTER.with(|counter| counter.borrow_mut().inc());
|
||||
let unique_name = format_ident!("{}_{}", name, count);
|
||||
|
||||
let decl_macro = quote::quote! {
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! #unique_name {
|
||||
{
|
||||
$caller:tt
|
||||
$(frame_support = [{ $($frame_support:ident)::* }])?
|
||||
} => {
|
||||
#frame_support::tt_return! {
|
||||
$caller
|
||||
#(
|
||||
#keys = [{ #values }]
|
||||
)*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub use #unique_name as #name;
|
||||
};
|
||||
|
||||
decl_macro.into()
|
||||
}
|
||||
Reference in New Issue
Block a user