1use proc_macro2::{Span, TokenStream as TokenStream2};
8use scale_typegen::TypegenError;
9
10#[derive(Debug, thiserror::Error)]
12#[non_exhaustive]
13pub enum CodegenError {
14 #[error("Could not decode metadata, only V14 and V15 metadata are supported: {0}")]
16 Decode(#[from] codec::Error),
17 #[error("Out-of-line subxt modules are not supported, make sure you are providing a body to your module: pub mod polkadot {{ ... }}")]
19 InvalidModule(Span),
20 #[error("Invalid type path {0}: {1}")]
22 InvalidTypePath(String, syn::Error),
23 #[error("Metadata for constant entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
25 MissingConstantMetadata(String, String),
26 #[error("Metadata for storage entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
28 MissingStorageMetadata(String, String),
29 #[error("Metadata for call entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
31 MissingCallMetadata(String, String),
32 #[error("Metadata for runtime API entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
34 MissingRuntimeApiMetadata(String, String),
35 #[error("Call variant for type {0} must have all named fields. Make sure you are providing a valid substrate-based metadata")]
37 InvalidCallVariant(u32),
38 #[error("{0} type should be an variant/enum type. Make sure you are providing a valid substrate-based metadata")]
40 InvalidType(String),
41 #[error("Extrinsic call type could not be found. Make sure you are providing a valid substrate-based metadata")]
43 MissingCallType,
44 #[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.")]
46 InvalidStorageHasherCount {
47 storage_entry_name: String,
49 key_count: usize,
51 hasher_count: usize,
53 },
54 #[error("Type Generation failed: {0}")]
56 TypeGeneration(#[from] TypegenError),
57 #[error("Failed to generate metadata from wasm file. reason: {0}")]
59 Wasm(String),
60 #[error("Other error: {0}")]
62 Other(String),
63}
64
65impl CodegenError {
66 fn get_location(&self) -> Span {
70 match self {
71 Self::InvalidModule(span) => *span,
72 Self::TypeGeneration(TypegenError::InvalidSubstitute(err)) => err.span,
73 Self::InvalidTypePath(_, err) => err.span(),
74 _ => proc_macro2::Span::call_site(),
75 }
76 }
77 pub fn into_compile_error(self) -> TokenStream2 {
79 let msg = self.to_string();
80 let span = self.get_location();
81 syn::Error::new(span, msg).into_compile_error()
82 }
83}