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(
19 "Out-of-line subxt modules are not supported, make sure you are providing a body to your module: pub mod polkadot {{ ... }}"
20 )]
21 InvalidModule(Span),
22 #[error("Invalid type path {0}: {1}")]
24 InvalidTypePath(String, syn::Error),
25 #[error(
27 "Metadata for constant entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata"
28 )]
29 MissingConstantMetadata(String, String),
30 #[error(
32 "Metadata for storage entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata"
33 )]
34 MissingStorageMetadata(String, String),
35 #[error(
37 "Metadata for call entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata"
38 )]
39 MissingCallMetadata(String, String),
40 #[error(
42 "Metadata for runtime API entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata"
43 )]
44 MissingRuntimeApiMetadata(String, String),
45 #[error(
47 "Call variant for type {0} must have all named fields. Make sure you are providing a valid substrate-based metadata"
48 )]
49 InvalidCallVariant(u32),
50 #[error(
52 "{0} type should be an variant/enum type. Make sure you are providing a valid substrate-based metadata"
53 )]
54 InvalidType(String),
55 #[error(
57 "Extrinsic call type could not be found. Make sure you are providing a valid substrate-based metadata"
58 )]
59 MissingCallType,
60 #[error(
62 "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."
63 )]
64 InvalidStorageHasherCount {
65 storage_entry_name: String,
67 key_count: usize,
69 hasher_count: usize,
71 },
72 #[error("Type Generation failed: {0}")]
74 TypeGeneration(#[from] TypegenError),
75 #[error("Failed to generate metadata from wasm file. reason: {0}")]
77 Wasm(String),
78 #[error("Other error: {0}")]
80 Other(String),
81}
82
83impl CodegenError {
84 fn get_location(&self) -> Span {
88 match self {
89 Self::InvalidModule(span) => *span,
90 Self::TypeGeneration(TypegenError::InvalidSubstitute(err)) => err.span,
91 Self::InvalidTypePath(_, err) => err.span(),
92 _ => proc_macro2::Span::call_site(),
93 }
94 }
95 pub fn into_compile_error(self) -> TokenStream2 {
97 let msg = self.to_string();
98 let span = self.get_location();
99 syn::Error::new(span, msg).into_compile_error()
100 }
101}