Skip to main content

metadata_shortener/
error.rs

1//! Errors.
2#[cfg(any(feature = "merkle-lean", test))]
3use merkle_cbt_lean::ErrorMT;
4use substrate_parser::error::SignableError;
5
6use crate::std::string::String;
7
8#[cfg(feature = "std")]
9use std::{
10    error::Error,
11    fmt::{Display, Formatter, Result as FmtResult},
12};
13
14#[cfg(not(feature = "std"))]
15use core::fmt::{Display, Formatter, Result as FmtResult};
16
17use external_memory_tools::ExternalMemory;
18use substrate_parser::traits::AsMetadata;
19
20/// Error in generating shortened metadata.
21#[derive(Debug, Eq, PartialEq)]
22pub enum MetaCutError<E: ExternalMemory, M: AsMetadata<E>> {
23    NoEntryLargerRegistry,
24    Registry(RegistryCutError),
25    Signable(SignableError<E, M>),
26    #[cfg(any(feature = "merkle-lean", test))]
27    TreeCalculateProof(ErrorMT<E>),
28    #[cfg(any(feature = "merkle-lean", test))]
29    TreeCalculateRoot(ErrorMT<E>),
30    #[cfg(any(feature = "merkle-lean", test))]
31    TreeConstructProof(ErrorMT<E>),
32}
33
34/// Error in generating shortened registry.
35#[derive(Debug, Eq, PartialEq)]
36pub enum RegistryCutError {
37    IndexTwice { id: u32 },
38}
39
40impl<E: ExternalMemory, M: AsMetadata<E>> MetaCutError<E, M> {
41    fn error_text(&self) -> String {
42        match &self {
43            MetaCutError::NoEntryLargerRegistry => String::from("While forming metadata types registry with excluded types, found type that should exist in larger registry, but does not. This is code bug, please report it."),
44            MetaCutError::Registry(registry_cut_error) => format!("{registry_cut_error}"),
45            MetaCutError::Signable(signable_error) => format!("{signable_error}"),
46            #[cfg(any(feature = "merkle-lean", test))]
47            MetaCutError::TreeCalculateProof(error_merkle_tree) => format!("Unable to calculate proof for merkle tree. {error_merkle_tree}"),
48            #[cfg(any(feature = "merkle-lean", test))]
49            MetaCutError::TreeCalculateRoot(error_merkle_tree) => format!("Unable to calculate root hash. {error_merkle_tree}"),
50            #[cfg(any(feature = "merkle-lean", test))]
51            MetaCutError::TreeConstructProof(error_merkle_tree) => format!("Unable to construct proof with provided data. {error_merkle_tree}"),
52        }
53    }
54}
55
56impl RegistryCutError {
57    fn error_text(&self) -> String {
58        match &self {
59            RegistryCutError::IndexTwice{id} => format!("While forming shortened metadata types registry, tried to enter type with already existing index {id} and different description. This is code bug, please report it."),
60        }
61    }
62}
63
64#[derive(Debug, Eq, PartialEq)]
65pub enum MetadataDescriptorError {
66    DescriptorVersionIncompatible,
67}
68
69impl MetadataDescriptorError {
70    fn error_text(&self) -> String {
71        match &self {
72            MetadataDescriptorError::DescriptorVersionIncompatible => {
73                String::from("MetadataDescriptor version is incompatible.")
74            }
75        }
76    }
77}
78
79/// Implement [`Display`] for errors in both `std` and `no_std` cases.
80/// Implement `Error` for `std` case.
81macro_rules! impl_display_and_error {
82    ($($ty: ty), *) => {
83        $(
84            impl Display for $ty {
85                fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
86                    write!(f, "{}", self.error_text())
87                }
88            }
89
90            #[cfg(feature = "std")]
91            impl Error for $ty {
92                fn source(&self) -> Option<&(dyn Error + 'static)> {
93                    None
94                }
95            }
96        )*
97    }
98}
99
100impl_display_and_error!(MetadataDescriptorError, RegistryCutError);
101
102/// Implement [`Display`] for errors in both `std` and `no_std` cases.
103/// Implement `Error` for `std` case.
104macro_rules! impl_display_and_error_traited {
105    ($($ty: ty), *) => {
106        $(
107            impl <E: ExternalMemory, M: AsMetadata<E>> Display for $ty {
108                fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
109                    write!(f, "{}", self.error_text())
110                }
111            }
112
113            #[cfg(feature = "std")]
114            impl <E: ExternalMemory, M: AsMetadata<E>> Error for $ty {
115                fn source(&self) -> Option<&(dyn Error + 'static)> {
116                    None
117                }
118            }
119        )*
120    }
121}
122
123impl_display_and_error_traited!(MetaCutError<E, M>);