float_pigment_consistent_bincode/
error.rsuse alloc::{boxed::Box, string::String, string::ToString};
use core::fmt;
use core::str::Utf8Error;
pub type Result<T> = ::core::result::Result<T, Error>;
pub type Error = Box<ErrorKind>;
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorKind {
Io(crate::io::Error),
InvalidUtf8Encoding(Utf8Error),
InvalidBoolEncoding(u8),
InvalidCharEncoding,
InvalidTagEncoding(usize),
DeserializeAnyNotSupported,
SizeLimit,
SequenceMustHaveLength,
Custom(String),
SegmentEnded,
}
impl serde::de::StdError for ErrorKind {
fn source(&self) -> Option<&(dyn serde::de::StdError + 'static)> {
match *self {
ErrorKind::Io(ref err) => Some(err),
ErrorKind::InvalidUtf8Encoding(_) => None,
ErrorKind::InvalidBoolEncoding(_) => None,
ErrorKind::InvalidCharEncoding => None,
ErrorKind::InvalidTagEncoding(_) => None,
ErrorKind::SequenceMustHaveLength => None,
ErrorKind::DeserializeAnyNotSupported => None,
ErrorKind::SizeLimit => None,
ErrorKind::Custom(_) => None,
ErrorKind::SegmentEnded => None,
}
}
}
impl From<crate::io::Error> for Error {
fn from(err: crate::io::Error) -> Error {
if err.kind() == crate::io::ErrorKind::UnexpectedEof {
return ErrorKind::SegmentEnded.into();
}
ErrorKind::Io(err).into()
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
ErrorKind::Io(ref ioerr) => write!(fmt, "io error: {}", ioerr),
ErrorKind::InvalidUtf8Encoding(ref e) => write!(fmt, "string is not valid utf8: {}", e),
ErrorKind::InvalidBoolEncoding(b) => {
write!(fmt, "invalid u8 while decoding bool, expected 0 or 1, found {}", b)
}
ErrorKind::InvalidCharEncoding => write!(fmt, "char is not valid"),
ErrorKind::InvalidTagEncoding(tag) => {
write!(fmt, "tag for enum is not valid, found {}", tag)
}
ErrorKind::SequenceMustHaveLength => write!(fmt, "Bincode can only encode sequences and maps that have a knowable size ahead of time"),
ErrorKind::SizeLimit => write!(fmt, "the size limit has been reached"),
ErrorKind::DeserializeAnyNotSupported => write!(
fmt,
"Bincode does not support the serde::Deserializer::deserialize_any method"
),
ErrorKind::Custom(ref s) => s.fmt(fmt),
ErrorKind::SegmentEnded => write!(fmt, "the segment does not contain enough data"),
}
}
}
impl serde::de::Error for Error {
fn custom<T: fmt::Display>(desc: T) -> Error {
ErrorKind::Custom(desc.to_string()).into()
}
}
impl serde::ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
ErrorKind::Custom(msg.to_string()).into()
}
}