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
use core::fmt;
/// Error types for serialization and deserialization operations.
#[non_exhaustive]
pub enum Error {
/// Unexpected end of input buffer.
UnexpectedEof,
/// Invalid UTF-8 sequence encountered.
InvalidUtf8 {
/// Byte offset where the invalid sequence was detected.
byte_offset: usize,
},
/// An unexpected byte was encountered.
UnexpectedByte {
/// Description of what was expected.
expected: &'static str,
/// The byte that was actually found.
got: u8,
/// Byte offset where the unexpected byte was detected.
offset: usize,
},
/// A numeric value overflowed its target type.
NumberOverflow {
/// The name of the target type (e.g., "u64", "i32").
type_name: &'static str,
},
/// A required field is missing from the input.
MissingField {
/// The name of the missing field.
name: &'static str,
},
/// An unknown field was encountered (if strict mode is enabled).
UnknownField {
/// The name of the unknown field.
name: alloc::vec::Vec<u8>,
},
/// The output buffer is full.
BufferFull {
/// Number of bytes needed.
needed: usize,
/// Number of bytes available in the buffer.
available: usize,
},
/// A floating point value is invalid (e.g., NaN or Infinity).
InvalidFloat,
/// The binary format magic number is invalid.
InvalidMagic,
/// Unsupported binary version.
UnsupportedVersion {
/// The unsupported version number found in the input.
version: u16,
},
/// Extra data found after decoding the main value.
TrailingData,
/// A custom error from an external source.
Custom(alloc::boxed::Box<dyn std::error::Error + Send + Sync>),
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedEof => write!(f, "Unexpected end of input"),
Self::InvalidUtf8 { byte_offset } => {
write!(f, "Invalid UTF-8 at offset {}", byte_offset)
}
Self::UnexpectedByte {
expected,
got,
offset,
} => {
write!(
f,
"Unexpected byte at offset {}: expected {}, got {:#04x}",
offset, expected, got
)
}
Self::NumberOverflow { type_name } => write!(f, "Number overflow for {}", type_name),
Self::MissingField { name } => write!(f, "Missing required field: {}", name),
Self::UnknownField { name } => {
write!(
f,
"Unknown field: {:?}",
alloc::string::String::from_utf8_lossy(name)
)
}
Self::BufferFull { needed, available } => {
write!(
f,
"Buffer overflow: need {} bytes, have {}",
needed, available
)
}
Self::InvalidFloat => write!(f, "Invalid float value (NaN or Infinity)"),
Self::InvalidMagic => write!(f, "Invalid binary magic number"),
Self::UnsupportedVersion { version } => {
write!(f, "Unsupported binary version: {:#06x}", version)
}
Self::TrailingData => write!(f, "Extra data found after decoding the main value"),
Self::Custom(_) => write!(f, "Custom error"),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl core::error::Error for Error {}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Custom(alloc::boxed::Box::new(e))
}
}
impl Error {
pub fn missing_field(name: &'static str) -> Self {
Error::MissingField { name }
}
}