facet_msgpack/
errors.rs

1use core::fmt;
2
3use facet_reflect::ReflectError;
4
5/// Errors that can occur during MessagePack encoding/decoding operations
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
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    /// Float value is too large for the target type
22    FloatOverflow,
23    /// Shape is not supported for deserialization
24    UnsupportedShape(String),
25    /// Type is not supported for deserialization
26    UnsupportedType(String),
27    /// Feature not yet implemented
28    NotImplemented(String),
29    /// Reflection error
30    ReflectError(ReflectError),
31    /// Invalid enum variant
32    InvalidEnum(String),
33}
34
35impl From<ReflectError> for Error {
36    fn from(err: ReflectError) -> Self {
37        Self::ReflectError(err)
38    }
39}
40
41impl fmt::Display for Error {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Error::UnexpectedType => write!(f, "Unexpected MessagePack type"),
45            Error::InsufficientData => write!(f, "Insufficient data to decode"),
46            Error::InvalidData => write!(f, "Invalid MessagePack data"),
47            Error::UnknownField(field) => write!(f, "Unknown field: {field}"),
48            Error::MissingField(field) => write!(f, "Missing required field: {field}"),
49            Error::IntegerOverflow => write!(f, "Integer value too large for target type"),
50            Error::FloatOverflow => write!(f, "Float value too large for target type"),
51            Error::UnsupportedShape(shape) => {
52                write!(f, "Unsupported shape for deserialization: {shape}")
53            }
54            Error::UnsupportedType(typ) => {
55                write!(f, "Unsupported type for deserialization: {typ}")
56            }
57            Error::NotImplemented(feature) => {
58                write!(f, "Feature not yet implemented: {feature}")
59            }
60            Error::ReflectError(err) => {
61                write!(f, "Reflection error: {err}")
62            }
63            Error::InvalidEnum(message) => {
64                write!(f, "Invalid enum variant: {message}")
65            }
66        }
67    }
68}
69
70impl std::error::Error for Error {}