Skip to main content

float_pigment_consistent_bincode/
error.rs

1use alloc::{boxed::Box, string::String, string::ToString};
2use core::fmt;
3use core::str::Utf8Error;
4
5/// The result of a serialization or deserialization operation.
6pub type Result<T> = ::core::result::Result<T, Error>;
7
8/// An error that can be produced during (de)serializing.
9pub type Error = Box<ErrorKind>;
10
11/// The kind of error that can be produced during a serialization or deserialization.
12#[derive(Debug)]
13#[non_exhaustive]
14pub enum ErrorKind {
15    /// If the error stems from the reader/writer that is being used
16    /// during (de)serialization, that error will be stored and returned here.
17    Io(crate::io::Error),
18    /// Returned if the deserializer attempts to deserialize a string that is not valid utf8
19    InvalidUtf8Encoding(Utf8Error),
20    /// Returned if the deserializer attempts to deserialize a bool that was
21    /// not encoded as either a 1 or a 0
22    InvalidBoolEncoding(u8),
23    /// Returned if the deserializer attempts to deserialize a char that is not in the correct format.
24    InvalidCharEncoding,
25    /// Returned if the deserializer attempts to deserialize the tag of an enum that is
26    /// not in the expected ranges
27    InvalidTagEncoding(usize),
28    /// Serde has a deserialize_any method that lets the format hint to the
29    /// object which route to take in deserializing.
30    DeserializeAnyNotSupported,
31    /// If (de)serializing a message takes more than the provided size limit, this
32    /// error is returned.
33    SizeLimit,
34    /// Bincode can not encode sequences of unknown length (like iterators).
35    SequenceMustHaveLength,
36    /// A custom error message from Serde.
37    Custom(String),
38    /// No enough segment data to read.
39    SegmentEnded,
40    /// Invalid data
41    InvalidData,
42}
43
44impl serde::de::StdError for ErrorKind {
45    fn source(&self) -> Option<&(dyn serde::de::StdError + 'static)> {
46        match *self {
47            ErrorKind::Io(ref err) => Some(err),
48            ErrorKind::InvalidUtf8Encoding(_) => None,
49            ErrorKind::InvalidBoolEncoding(_) => None,
50            ErrorKind::InvalidCharEncoding => None,
51            ErrorKind::InvalidTagEncoding(_) => None,
52            ErrorKind::SequenceMustHaveLength => None,
53            ErrorKind::DeserializeAnyNotSupported => None,
54            ErrorKind::SizeLimit => None,
55            ErrorKind::Custom(_) => None,
56            ErrorKind::SegmentEnded => None,
57            ErrorKind::InvalidData => None,
58        }
59    }
60}
61
62impl From<crate::io::Error> for Error {
63    fn from(err: crate::io::Error) -> Error {
64        if err.kind() == crate::io::ErrorKind::UnexpectedEof {
65            return ErrorKind::SegmentEnded.into();
66        }
67        ErrorKind::Io(err).into()
68    }
69}
70
71impl fmt::Display for ErrorKind {
72    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
73        match *self {
74            ErrorKind::Io(ref ioerr) => write!(fmt, "io error: {ioerr}"),
75            ErrorKind::InvalidUtf8Encoding(ref e) => write!(fmt, "string is not valid utf8: {e}"),
76            ErrorKind::InvalidBoolEncoding(b) => {
77                write!(fmt, "invalid u8 while decoding bool, expected 0 or 1, found {b}")
78            }
79            ErrorKind::InvalidCharEncoding => write!(fmt, "char is not valid"),
80            ErrorKind::InvalidTagEncoding(tag) => {
81                write!(fmt, "tag for enum is not valid, found {tag}")
82            }
83            ErrorKind::SequenceMustHaveLength => write!(fmt, "Bincode can only encode sequences and maps that have a knowable size ahead of time"),
84            ErrorKind::SizeLimit => write!(fmt, "the size limit has been reached"),
85            ErrorKind::DeserializeAnyNotSupported => write!(
86                fmt,
87                "Bincode does not support the serde::Deserializer::deserialize_any method"
88            ),
89            ErrorKind::Custom(ref s) => s.fmt(fmt),
90            ErrorKind::SegmentEnded => write!(fmt, "the segment does not contain enough data"),
91            ErrorKind::InvalidData => write!(fmt, "the data is invalid"),
92        }
93    }
94}
95
96impl serde::de::Error for Error {
97    fn custom<T: fmt::Display>(desc: T) -> Error {
98        ErrorKind::Custom(desc.to_string()).into()
99    }
100}
101
102impl serde::ser::Error for Error {
103    fn custom<T: fmt::Display>(msg: T) -> Self {
104        ErrorKind::Custom(msg.to_string()).into()
105    }
106}