web3api_wasm_rs/msgpack/
error.rs

1//! Errors returned from I/O `Write` and `Read` operations
2
3use super::Format;
4use thiserror::Error;
5
6/// Errors from encoding data
7#[derive(Debug, Error)]
8pub enum EncodeError {
9    #[error("{0}")]
10    NilWriteError(String),
11
12    #[error("{0}")]
13    FormatWriteError(String),
14
15    #[error("{0}")]
16    BooleanWriteError(String),
17
18    #[error("{0}")]
19    BinWriteError(String),
20
21    #[error("{0}")]
22    BigIntWriteError(String),
23
24    #[error("{0}")]
25    JSONWriteError(String),
26
27    #[error("{0}")]
28    Float32WriteError(String),
29
30    #[error("{0}")]
31    Float64WriteError(String),
32
33    #[error("{0}")]
34    Uint8WriteError(String),
35
36    #[error("{0}")]
37    Uint16WriteError(String),
38
39    #[error("{0}")]
40    Uint32WriteError(String),
41
42    #[error("{0}")]
43    Int8WriteError(String),
44
45    #[error("{0}")]
46    Int16WriteError(String),
47
48    #[error("{0}")]
49    Int32WriteError(String),
50
51    #[error("{0}")]
52    StrWriteError(String),
53
54    #[error("{0}")]
55    TypeWriteError(String),
56
57    #[error("{0}")]
58    IOError(String),
59}
60
61impl From<std::io::Error> for EncodeError {
62    fn from(e: std::io::Error) -> Self {
63        EncodeError::IOError(e.to_string())
64    }
65}
66
67impl From<serde_json::Error> for EncodeError {
68    fn from(e: serde_json::Error) -> EncodeError {
69        EncodeError::JSONWriteError(e.to_string())
70    }
71}
72
73/// Errors from decoding data
74#[derive(Debug, Error)]
75pub enum DecodeError {
76    #[error("Found NIL, but expected: '{0}'")]
77    FoundNilButExpected(String),
78
79    #[error("{0}")]
80    BooleanReadError(String),
81
82    #[error("{0}")]
83    BytesReadError(String),
84
85    #[error("{0}")]
86    ParseBigIntError(String),
87
88    #[error("{0}")]
89    ParseBigNumberError(String),
90
91    #[error("{0}")]
92    IntReadError(String),
93
94    #[error("{0}")]
95    UintReadError(String),
96
97    #[error("{0}")]
98    FloatReadError(String),
99
100    #[error("{0}")]
101    BigIntReadError(String),
102
103    #[error("{0}")]
104    BigNumberReadError(String),
105
106    #[error("{0}")]
107    JSONReadError(String),
108
109    #[error("{0}")]
110    IntRangeError(String),
111
112    #[error("{0}")]
113    ArrayReadError(String),
114
115    #[error("{0}")]
116    MapReadError(String),
117
118    #[error("{0}")]
119    ExtGenericMapReadError(String),
120
121    #[error("{0}")]
122    StrReadError(String),
123
124    #[error("{0}")]
125    EnumReadError(String),
126
127    #[error("UnknownFieldName: '{0}'")]
128    UnknownFieldName(String),
129
130    #[error("{0}")]
131    WrongMsgPackFormat(String),
132
133    #[error("Missing required field: '{0}'")]
134    MissingField(String),
135
136    #[error("{0}")]
137    TypeReadError(String),
138
139    #[error("{0}")]
140    IOError(String),
141}
142
143impl From<std::io::Error> for DecodeError {
144    fn from(e: std::io::Error) -> Self {
145        DecodeError::IOError(e.to_string())
146    }
147}
148
149impl From<serde_json::Error> for DecodeError {
150    fn from(e: serde_json::Error) -> DecodeError {
151        DecodeError::JSONReadError(e.to_string())
152    }
153}
154
155impl From<num_bigint::ParseBigIntError> for DecodeError {
156    fn from(e: num_bigint::ParseBigIntError) -> Self {
157        DecodeError::BigIntReadError(e.to_string())
158    }
159}
160
161/// Error types for CustomEnum
162#[derive(Debug, Error)]
163pub enum EnumTypeError {
164    #[error("{0}")]
165    EnumProcessingError(String),
166
167    #[error("{0}")]
168    ParseEnumError(String),
169}
170
171impl From<EnumTypeError> for DecodeError {
172  fn from(e: EnumTypeError) -> Self {
173      DecodeError::EnumReadError(e.to_string())
174  }
175}
176
177pub fn get_error_message(format: Format) -> String {
178    match format {
179        Format::Nil => "Found 'nil'.".to_string(),
180        Format::Reserved => "Found 'reserved'.".to_string(),
181        Format::False | Format::True => "Found 'bool'.".to_string(),
182        Format::Bin8 => "Found 'BIN8'.".to_string(),
183        Format::Bin16 => "Found 'BIN16'.".to_string(),
184        Format::Bin32 => "Found 'BIN32'.".to_string(),
185        Format::Ext8 => "Found 'EXT8'.".to_string(),
186        Format::Ext16 => "Found 'EXT16'.".to_string(),
187        Format::Ext32 => "Found 'EXT32'.".to_string(),
188        Format::Float32 => "Found 'float32'.".to_string(),
189        Format::Float64 => "Found 'float64'.".to_string(),
190        Format::NegativeFixInt(_) | Format::PositiveFixInt(_) => "Found 'int'.".to_string(),
191        Format::Uint8 => "Found 'uint8'.".to_string(),
192        Format::Uint16 => "Found 'uint16'.".to_string(),
193        Format::Uint32 => "Found 'uint32'.".to_string(),
194        Format::Uint64 => "Found 'uint64'.".to_string(),
195        Format::Int8 => "Found 'int8'.".to_string(),
196        Format::Int16 => "Found 'int16'.".to_string(),
197        Format::Int32 => "Found 'int32'.".to_string(),
198        Format::Int64 => "Found 'int64'.".to_string(),
199        Format::FixExt1 => "Found 'FIXEXT1'.".to_string(),
200        Format::FixExt2 => "Found 'FIXEXT2'.".to_string(),
201        Format::FixExt4 => "Found 'FIXEXT4'.".to_string(),
202        Format::FixExt8 => "Found 'FIXEXT8'.".to_string(),
203        Format::FixExt16 => "Found 'FIXEXT16'.".to_string(),
204        Format::FixStr(_) | Format::Str8 | Format::Str16 | Format::Str32 => "Found 'string'.".to_string(),
205        Format::FixArray(_) | Format::Array16 | Format::Array32 => "Found 'array'.".to_string(),
206        Format::FixMap(_) | Format::Map16 | Format::Map32 => "Found 'map'.".to_string(),
207    }
208}