codegen: Add codegen error (#841)

* codegen: Add codegen error

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Use codegen error instead of aborts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Remove `proc-macro-error` dependency

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* macro/subxt: Transform codegen error into compile_error!

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Pretty printing for `CodegenError`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update cargo.lock

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Adjust testing for codegen error

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Fix documentation example

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Export `CodegenError`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Use collect::<Result<_>, _>()

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Adjust comment regarding error printing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Improve error messages

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2023-03-03 19:57:46 +02:00
committed by GitHub
parent 5320ca9d55
commit a7b45ef1d1
18 changed files with 405 additions and 322 deletions
+9 -8
View File
@@ -11,6 +11,8 @@ use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use scale_info::form::PortableForm;
use super::CodegenError;
/// Generate events from the provided pallet metadata.
///
/// The function creates a new module named `events` under the pallet's module.
@@ -46,12 +48,10 @@ pub fn generate_events(
types_mod_ident: &syn::Ident,
crate_path: &CratePath,
should_gen_docs: bool,
) -> TokenStream2 {
) -> Result<TokenStream2, CodegenError> {
// Early return if the pallet has no events.
let event = if let Some(ref event) = pallet.event {
event
} else {
return quote!()
let Some(event) = &pallet.event else {
return Ok(quote!())
};
let struct_defs = super::generate_structs_from_variants(
@@ -61,7 +61,8 @@ pub fn generate_events(
"Event",
crate_path,
should_gen_docs,
);
)?;
let event_structs = struct_defs.iter().map(|(variant_name, struct_def)| {
let pallet_name = &pallet.name;
let event_struct = &struct_def.name;
@@ -83,12 +84,12 @@ pub fn generate_events(
.then_some(quote! { #( #[doc = #docs ] )* })
.unwrap_or_default();
quote! {
Ok(quote! {
#docs
pub type Event = #event_type;
pub mod events {
use super::#types_mod_ident;
#( #event_structs )*
}
}
})
}