polywrap_wasm_rs/msgpack/
error.rs

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