float_pigment_consistent_bincode/
error.rs1use alloc::{boxed::Box, string::String, string::ToString};
2use core::fmt;
3use core::str::Utf8Error;
4
5pub type Result<T> = ::core::result::Result<T, Error>;
7
8pub type Error = Box<ErrorKind>;
10
11#[derive(Debug)]
13#[non_exhaustive]
14pub enum ErrorKind {
15 Io(crate::io::Error),
18 InvalidUtf8Encoding(Utf8Error),
20 InvalidBoolEncoding(u8),
23 InvalidCharEncoding,
25 InvalidTagEncoding(usize),
28 DeserializeAnyNotSupported,
31 SizeLimit,
34 SequenceMustHaveLength,
36 Custom(String),
38 SegmentEnded,
40 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}