facet_msgpack/
errors.rs

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