facet_postcard/
error.rs

1use core::fmt;
2
3use facet_reflect::ReflectError;
4
5/// Errors that can occur during postcard serialization
6#[derive(Debug)]
7pub enum SerializeError {
8    /// The output buffer is too small to hold the serialized data
9    BufferTooSmall,
10    /// Encountered a type that cannot be serialized to postcard format
11    UnsupportedType(&'static str),
12}
13
14impl fmt::Display for SerializeError {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            SerializeError::BufferTooSmall => write!(f, "Buffer too small for serialized data"),
18            SerializeError::UnsupportedType(ty) => {
19                write!(f, "Unsupported type for postcard serialization: {ty}")
20            }
21        }
22    }
23}
24
25#[cfg(feature = "std")]
26impl std::error::Error for SerializeError {}
27
28/// Errors that can occur during postcard deserialization
29#[derive(Debug)]
30pub enum DeserializeError {
31    /// Not enough data available to decode a complete value
32    UnexpectedEnd,
33    /// The data is malformed or corrupted
34    InvalidData,
35    /// Integer value is too large for the target type
36    IntegerOverflow,
37    /// Encountered a field name that isn't recognized
38    UnknownField,
39    /// Required field is missing from the input
40    MissingField(&'static str),
41    /// Shape is not supported for deserialization
42    UnsupportedShape,
43    /// Type is not supported for deserialization
44    UnsupportedType(&'static str),
45    /// Invalid enum variant index
46    InvalidVariant,
47    /// Invalid boolean value (not 0 or 1)
48    InvalidBool,
49    /// Invalid UTF-8 in string
50    InvalidUtf8,
51    /// Reflection error from facet-reflect
52    ReflectError(ReflectError),
53    /// Sequence length mismatch
54    LengthMismatch,
55}
56
57impl From<ReflectError> for DeserializeError {
58    fn from(err: ReflectError) -> Self {
59        Self::ReflectError(err)
60    }
61}
62
63impl fmt::Display for DeserializeError {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            DeserializeError::UnexpectedEnd => write!(f, "Unexpected end of input"),
67            DeserializeError::InvalidData => write!(f, "Invalid postcard data"),
68            DeserializeError::IntegerOverflow => {
69                write!(f, "Integer value too large for target type")
70            }
71            DeserializeError::UnknownField => write!(f, "Unknown field encountered"),
72            DeserializeError::MissingField(field) => {
73                write!(f, "Missing required field: {field}")
74            }
75            DeserializeError::UnsupportedShape => {
76                write!(f, "Unsupported shape for deserialization")
77            }
78            DeserializeError::UnsupportedType(ty) => {
79                write!(f, "Unsupported type for deserialization: {ty}")
80            }
81            DeserializeError::InvalidVariant => write!(f, "Invalid enum variant index"),
82            DeserializeError::InvalidBool => write!(f, "Invalid boolean value (expected 0 or 1)"),
83            DeserializeError::InvalidUtf8 => write!(f, "Invalid UTF-8 in string data"),
84            DeserializeError::ReflectError(err) => write!(f, "Reflection error: {err}"),
85            DeserializeError::LengthMismatch => write!(f, "Sequence length mismatch"),
86        }
87    }
88}
89
90#[cfg(feature = "std")]
91impl std::error::Error for DeserializeError {}