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
use alloc::string::String;
use alloc::string::ToString;
use core::fmt::{self, Debug, Display};
use core::result;
use serde::{de, ser};
#[derive(Debug)]
pub enum Error {
/// Contains a general error message as a string.
Message(String),
/// Occurs when the data length is incorrect while parsing a number or molecule header.
MismatchedLength,
/// Occurs when the data length is insufficient while parsing a number or molecule header.
LengthNotEnough,
/// Indicates that the method or type is not implemented. Not all types in Rust can be serialized.
Unimplemented,
/// Occurs when assembling a molecule fixvec, and the size of each element is inconsistent.
AssembleFixvec,
/// Occurs when the header or size is incorrect while parsing a molecule fixvec.
InvalidFixvec,
/// Occurs when the field count is mismatched while parsing a molecule table.
MismatchedTableFieldCount,
/// Occurs when an overflow happens while parsing a molecule header.
Overflow,
/// Indicates an error encountered while parsing a molecule array.
InvalidArray,
/// Indicates that non-fixed size fields are not allowed in a molecule struct, e.g., `Option`, `Vec`, `DynVec`, `enum`.
InvalidStructField,
/// Indicates that a map should have exactly two fields: a key and a value.
InvalidMap,
/// Indicates that the table header is invalid or malformed.
InvalidTable,
/// Indicates that the table length is invalid or malformed.
InvalidTableLength,
/// Indicates that the table header is invalid or malformed.
InvalidTableHeader,
/// Indicates that the field count in serialization is mismatched.
InvalidTableCount,
/// Indicates that non-fixed size fields are not allowed in a molecule struct, e.g., `Option`, `Vec`, `DynVec`, `enum`.
MixTableAndStruct,
/// Invalid char
InvalidChar,
}
pub type Result<T> = result::Result<T, Error>;
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Error {
Error::Message(msg.to_string())
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Error {
Error::Message(msg.to_string())
}
}
impl de::StdError for Error {
fn source(&self) -> Option<&(dyn de::StdError + 'static)> {
None
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Message(m) => f.write_str(m),
_ => {
write!(f, "{:?}", self)
}
}
}
}