pub enum Error {
EndOfBuffer,
ExtraData(usize),
InvalidVarint(usize),
InvalidUsize,
InvalidBool,
InvalidEnum(u8),
InvalidLength(usize),
Invalid(&'static str, &'static str),
Wrapped(&'static str, Box<dyn Error + Send + Sync>),
}
Expand description
Error type for codec operations
Variants§
EndOfBuffer
Indicates that the input buffer (Buf
) did not contain enough bytes to read
the next piece of data required by a Read
implementation.
This suggests the input data is truncated or incomplete.
ExtraData(usize)
Indicates that after successfully decoding a value using a method like
Decode::decode_cfg
, there were still
unconsumed bytes remaining in the input buffer.
This usually means the input data contained more than just the expected encoded value.
The contained usize
is the number of bytes that remained unconsumed.
InvalidVarint(usize)
A variable-length integer (varint), often used for encoding lengths, could not be decoded correctly. This might happen if:
- The varint encoding itself is malformed (e.g., too long).
- The decoded varint value exceeds the capacity of the target integer type.
See the varint
module for encoding details.
InvalidUsize
Same as InvalidVarint
, but specifically for usize
-sized varints.
InvalidBool
A byte representing a boolean was expected to be 0
(false) or 1
(true),
but a different value was encountered during decoding.
InvalidEnum(u8)
An enum variant was expected, but the decoded value did not match any of the expected variants.
InvalidLength(usize)
A length prefix (e.g., for Vec<T>
, Bytes
, HashMap<K, V>
) was decoded,
but its value fell outside the permitted range.
This range is typically configured via a RangeCfg
passed within the Cfg
parameter to Read::read_cfg
.
The contained usize
is the invalid length that was decoded.
Invalid(&'static str, &'static str)
A semantic validation error occurred during decoding, indicating the data, while perhaps structurally valid, does not meet application-specific criteria.
- The first
&'static str
provides context (e.g., the name of the type being decoded). - The second
&'static str
provides a specific error message.
Example: Trying to decode a HashMap
where keys were not in ascending order.
Wrapped(&'static str, Box<dyn Error + Send + Sync>)
An error occurred in underlying code (e.g., external library call, complex validation)
and has been wrapped into a codec Error
.
- The
&'static str
provides context about the operation being performed. - The boxed
std::error::Error
is the original source error.
Allows propagating custom errors through the codec reading process.