use proc_macro2::{Span, TokenStream as TokenStream2};
use scale_typegen::TypegenError;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CodegenError {
#[error("Could not decode metadata, only V14 and V15 metadata are supported: {0}")]
Decode(#[from] codec::Error),
#[error(
"Out-of-line subxt modules are not supported, make sure you are providing a body to your module: pub mod pezkuwi {{ ... }}"
)]
InvalidModule(Span),
#[error("Invalid type path {0}: {1}")]
InvalidTypePath(String, syn::Error),
#[error(
"Metadata for constant entry {0}_{1} could not be found. Make sure you are providing a valid bizinikiwi-based metadata"
)]
MissingConstantMetadata(String, String),
#[error(
"Metadata for storage entry {0}_{1} could not be found. Make sure you are providing a valid bizinikiwi-based metadata"
)]
MissingStorageMetadata(String, String),
#[error(
"Metadata for call entry {0}_{1} could not be found. Make sure you are providing a valid bizinikiwi-based metadata"
)]
MissingCallMetadata(String, String),
#[error(
"Metadata for runtime API entry {0}_{1} could not be found. Make sure you are providing a valid bizinikiwi-based metadata"
)]
MissingRuntimeApiMetadata(String, String),
#[error(
"Call variant for type {0} must have all named fields. Make sure you are providing a valid bizinikiwi-based metadata"
)]
InvalidCallVariant(u32),
#[error(
"{0} type should be an variant/enum type. Make sure you are providing a valid bizinikiwi-based metadata"
)]
InvalidType(String),
#[error(
"Extrinsic call type could not be found. Make sure you are providing a valid bizinikiwi-based metadata"
)]
MissingCallType,
#[error(
"Could not generate functions for storage entry {storage_entry_name}. There are {key_count} keys, but only {hasher_count} hashers. The number of hashers must equal the number of keys or be exactly 1."
)]
InvalidStorageHasherCount {
storage_entry_name: String,
key_count: usize,
hasher_count: usize,
},
#[error("Type Generation failed: {0}")]
TypeGeneration(#[from] TypegenError),
#[error("Failed to generate metadata from wasm file. reason: {0}")]
Wasm(String),
#[error("Other error: {0}")]
Other(String),
}
impl CodegenError {
fn get_location(&self) -> Span {
match self {
Self::InvalidModule(span) => *span,
Self::TypeGeneration(TypegenError::InvalidSubstitute(err)) => err.span,
Self::InvalidTypePath(_, err) => err.span(),
_ => proc_macro2::Span::call_site(),
}
}
pub fn into_compile_error(self) -> TokenStream2 {
let msg = self.to_string();
let span = self.get_location();
syn::Error::new(span, msg).into_compile_error()
}
}