use serde::{de, ser};
use std::error;
use std::fmt;
use std::io;
use std::result;
#[derive(Clone, PartialEq, Debug)]
pub enum ErrorCode {
Unsupported(char),
EOFWhileParsing,
StackUnderflow,
NegativeLength,
StringNotUTF8,
InvalidStackTop(&'static str, String),
ValueNotHashable,
Recursive,
UnresolvedGlobal,
UnsupportedGlobal(Vec<u8>, Vec<u8>),
MissingMemo(u32),
InvalidLiteral(Vec<u8>),
TrailingBytes,
InvalidValue(String),
Structure(String),
}
impl fmt::Display for ErrorCode {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
ErrorCode::Unsupported(ch) => write!(fmt, "unsupported opcode {ch:?}"),
ErrorCode::EOFWhileParsing => write!(fmt, "EOF while parsing"),
ErrorCode::StackUnderflow => write!(fmt, "pickle stack underflow"),
ErrorCode::NegativeLength => write!(fmt, "negative length prefix"),
ErrorCode::StringNotUTF8 => write!(fmt, "string is not UTF-8 encoded"),
ErrorCode::InvalidStackTop(what, ref it) => {
write!(fmt, "invalid stack top, expected {what}, got {it}")
}
ErrorCode::ValueNotHashable => write!(fmt, "dict key or set item not hashable"),
ErrorCode::Recursive => write!(fmt, "recursive structure found"),
ErrorCode::UnresolvedGlobal => write!(fmt, "unresolved global reference"),
ErrorCode::UnsupportedGlobal(ref m, ref g) => write!(
fmt,
"unsupported global: {}.{}",
String::from_utf8_lossy(m),
String::from_utf8_lossy(g)
),
ErrorCode::MissingMemo(n) => write!(fmt, "missing memo with id {n}"),
ErrorCode::InvalidLiteral(ref l) => {
write!(fmt, "literal is invalid: {}", String::from_utf8_lossy(l))
}
ErrorCode::TrailingBytes => write!(fmt, "trailing bytes found"),
ErrorCode::InvalidValue(ref s) => write!(fmt, "invalid value: {s}"),
ErrorCode::Structure(ref s) => fmt.write_str(s),
}
}
}
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Eval(ErrorCode, usize),
Syntax(ErrorCode),
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Error {
Error::Io(error)
}
}
pub type Result<T> = result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Io(ref error) => error.fmt(fmt),
Error::Eval(ref code, offset) => write!(fmt, "eval error at offset {offset}: {code}"),
Error::Syntax(ref code) => write!(fmt, "decoding error: {code}"),
}
}
}
impl error::Error for Error {}
impl de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Error {
Error::Syntax(ErrorCode::Structure(msg.to_string()))
}
}
impl ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Error {
Error::Syntax(ErrorCode::Structure(msg.to_string()))
}
}