1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#[cfg(feature = "alloc")]
use alloc::string::{String, ToString};
use core::fmt::{self, Debug, Display};
#[derive(Debug)]
pub enum Error<E> {
FloatingPointUnsupported,
TooManyVariants {
enum_name: &'static str,
variant_name: &'static str,
variant_index: u32,
},
LengthNeeded,
TypeMustBeKnown,
ExpectedBoolean {
found: u8,
},
InvalidCharacter {
found: u32,
},
CollectionTooLargeToSerialize {
len: usize,
},
CollectionTooLargeToDeserialize,
InvalidUnicode(core::str::Utf8Error),
InvalidOption {
found_discriminant: u8,
},
Io(E),
#[cfg(feature = "alloc")]
Other(String),
#[cfg(not(feature = "alloc"))]
Other(&'static str),
}
impl<E> From<E> for Error<E> {
fn from(e: E) -> Self {
Error::Io(e)
}
}
impl<E: Display> Display for Error<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::FloatingPointUnsupported => {
write!(f, "Floating point values are not supported by the SCALE encoding")
}
Error::TooManyVariants { enum_name, variant_name, variant_index } => {
write!(f, "Variant {}::{} has index {} but the SCALE encoding limits enumerations \
to 255 variants", enum_name, variant_name, variant_index)
}
Error::LengthNeeded => {
write!(f, "Sequence length unknown but the SCALE encoding requires to know it")
}
Error::TypeMustBeKnown => {
write!(f, "Type unknown but the SCALE encoding requires to know it")
}
Error::ExpectedBoolean { found } => {
write!(f, "Expected boolean (0 or 1), found {}", found)
}
Error::InvalidCharacter { found } => {
write!(f, "{} is an invalid UTF-32 codepoint", found)
}
Error::CollectionTooLargeToSerialize { len } => {
write!(f, "Found a collection of {} elements but this implementation limits \
collections to 2^64 elements", len)
}
Error::CollectionTooLargeToDeserialize => {
write!(f, "Collections of more than 2^64 elements are not supported")
}
Error::InvalidUnicode(e) => {
write!(f, "Invalid Unicode in string: {}", e)
}
Error::InvalidOption { found_discriminant } => {
write!(f, "Invalid option. Expected a discriminant of 0 or 1 but found {}",
found_discriminant)
}
Error::Io(e) => {
write!(f, "I/O error: {}", e)
}
Error::Other(e) => write!(f, "{}", e),
}
}
}
#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> std::error::Error for Error<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::InvalidUnicode(e) => Some(e),
Error::Io(e) => Some(e),
Error::FloatingPointUnsupported
| Error::TooManyVariants { .. }
| Error::LengthNeeded
| Error::TypeMustBeKnown
| Error::ExpectedBoolean { .. }
| Error::InvalidCharacter { .. }
| Error::CollectionTooLargeToSerialize { .. }
| Error::CollectionTooLargeToDeserialize
| Error::InvalidOption { .. }
| Error::Other(_) => None,
}
}
}
impl<E: SuperError + 'static> serde::ser::Error for Error<E> {
fn custom<T: Display>(msg: T) -> Self {
#[cfg(feature = "alloc")]
{
Error::Other(msg.to_string())
}
#[cfg(not(feature = "alloc"))]
{
let _ = msg;
Error::Other("Custom error")
}
}
}
impl<E: SuperError + 'static> serde::de::Error for Error<E> {
fn custom<T: Display>(msg: T) -> Self {
serde::ser::Error::custom(msg)
}
}
pub use super_error::SuperError;
#[cfg(feature = "std")]
mod super_error {
pub use std::error::Error as SuperError;
}
#[cfg(not(feature = "std"))]
mod super_error {
use core::fmt::{Debug, Display};
pub trait SuperError: Debug + Display {}
impl<E: Debug + Display + ?Sized> SuperError for E {}
}