facet_msgpack/
errors.rs

1use core::fmt;
2
3use facet_reflect::ReflectError;
4
5#[derive(Debug)]
6/// Errors that can occur during MessagePack encoding/decoding operations
7pub enum Error {
8    /// Encountered a MessagePack type that doesn't match the expected type
9    UnexpectedType,
10    /// Not enough data available to decode a complete MessagePack value
11    InsufficientData,
12    /// The MessagePack data is malformed or corrupted
13    InvalidData,
14    /// Encountered a field name that isn't recognized
15    UnknownField(String),
16    /// Required field is missing from the input
17    MissingField(String),
18    /// Integer value is too large for the target type
19    IntegerOverflow,
20    /// Shape is not supported for deserialization
21    UnsupportedShape(String),
22    /// Type is not supported for deserialization
23    UnsupportedType(String),
24    /// Feature not yet implemented
25    NotImplemented(String),
26    /// Reflection error
27    ReflectError(ReflectError),
28    /// Invalid enum variant
29    InvalidEnum(String),
30}
31
32impl From<ReflectError> for Error {
33    fn from(err: ReflectError) -> Self {
34        Self::ReflectError(err)
35    }
36}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Error::UnexpectedType => write!(f, "Unexpected MessagePack type"),
42            Error::InsufficientData => write!(f, "Insufficient data to decode"),
43            Error::InvalidData => write!(f, "Invalid MessagePack data"),
44            Error::UnknownField(field) => write!(f, "Unknown field: {field}"),
45            Error::MissingField(field) => write!(f, "Missing required field: {field}"),
46            Error::IntegerOverflow => write!(f, "Integer value too large for target type"),
47            Error::UnsupportedShape(shape) => {
48                write!(f, "Unsupported shape for deserialization: {shape}")
49            }
50            Error::UnsupportedType(typ) => {
51                write!(f, "Unsupported type for deserialization: {typ}")
52            }
53            Error::NotImplemented(feature) => {
54                write!(f, "Feature not yet implemented: {feature}")
55            }
56            Error::ReflectError(err) => {
57                write!(f, "Reflection error: {err}")
58            }
59            Error::InvalidEnum(message) => {
60                write!(f, "Invalid enum variant: {message}")
61            }
62        }
63    }
64}
65
66impl std::error::Error for Error {}