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
+19 -24
View File
@@ -2,6 +2,7 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use super::CodegenError;
use crate::{
types::{
CompositeDefFields,
@@ -18,7 +19,6 @@ use heck::{
ToUpperCamelCase as _,
};
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::abort_call_site;
use quote::{
format_ident,
quote,
@@ -41,12 +41,10 @@ pub fn generate_calls(
types_mod_ident: &syn::Ident,
crate_path: &CratePath,
should_gen_docs: bool,
) -> TokenStream2 {
) -> Result<TokenStream2, CodegenError> {
// Early return if the pallet has no calls.
let call = if let Some(ref calls) = pallet.calls {
calls
} else {
return quote!()
let Some(call) = &pallet.calls else {
return Ok(quote!());
};
let mut struct_defs = super::generate_structs_from_variants(
@@ -56,7 +54,7 @@ pub fn generate_calls(
"Call",
crate_path,
should_gen_docs,
);
)?;
let (call_structs, call_fns): (Vec<_>, Vec<_>) = struct_defs
.iter_mut()
.map(|(variant_name, struct_def)| {
@@ -77,26 +75,20 @@ pub fn generate_calls(
}
CompositeDefFields::NoFields => Default::default(),
CompositeDefFields::Unnamed(_) => {
abort_call_site!(
"Call variant for type {} must have all named fields",
call.ty.id()
)
return Err(CodegenError::InvalidCallVariant(call.ty.id()))
}
};
let pallet_name = &pallet.name;
let call_name = &variant_name;
let struct_name = &struct_def.name;
let call_hash =
subxt_metadata::get_call_hash(metadata, pallet_name, call_name)
.unwrap_or_else(|_| {
abort_call_site!(
"Metadata information for the call {}_{} could not be found",
pallet_name,
call_name
)
});
let Ok(call_hash) =
subxt_metadata::get_call_hash(metadata, pallet_name, call_name) else {
return Err(CodegenError::MissingCallMetadata(
pallet_name.into(),
call_name.to_string(),
))
};
let fn_name = format_ident!("{}", variant_name.to_snake_case());
// Propagate the documentation just to `TransactionApi` methods, while
// draining the documentation of inner call structures.
@@ -121,8 +113,11 @@ pub fn generate_calls(
)
}
};
(call_struct, client_fn)
Ok((call_struct, client_fn))
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.unzip();
let call_ty = type_gen.resolve_type(call.ty.id());
@@ -131,7 +126,7 @@ pub fn generate_calls(
.then_some(quote! { #( #[doc = #docs ] )* })
.unwrap_or_default();
quote! {
Ok(quote! {
#docs
pub mod calls {
use super::root_mod;
@@ -147,5 +142,5 @@ pub fn generate_calls(
#( #call_fns )*
}
}
}
})
}