use crate::NbtTypeId;
#[derive(Debug, PartialEq, Eq)]
pub enum NbtError {
UnknownErr(String),
WrongRootType(u8),
RootWithoutName,
UnknownType(u8),
NameRead(String),
Overflow(usize, usize, usize),
VarIntTooBig(usize),
VarLongTooBig(usize),
ListTypeNotSame(Vec<NbtTypeId>),
IncorrectType(NbtTypeId, NbtTypeId),
}
pub type NbtResult<T> = std::result::Result<T, NbtError>;
impl std::error::Error for NbtError {}
impl std::fmt::Display for NbtError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NbtError::IncorrectType(t0, t1) => write!(f, "Expected type {t0} but got type {t1}"),
NbtError::NameRead(name) => write!(f, "Could not read name '{name}'"),
NbtError::Overflow(c, l, d) => write!(f, "Can't move cursor ({c}) by {l}, because the data length is only {d}"),
NbtError::UnknownErr(err) => write!(f, "Unknown error occured: {err}"),
NbtError::RootWithoutName => todo!(),
NbtError::UnknownType(t0) => write!(f, "The type id {t0} does not corrospond to a valid type"),
NbtError::VarIntTooBig(byte_length) => write!(f, "Could not construct VarInt, because the data would take {byte_length} bytes"),
NbtError::VarLongTooBig(byte_length) => write!(f, "Could not construct VarLong, because the data would take {byte_length} bytes"),
NbtError::ListTypeNotSame(types) => write!(f, "Could not create list, because following list types are not the same: {:?}", types),
NbtError::WrongRootType(type_id) => write!(f, "The Root has to be an type id 0x0A, but is an {:02x}", type_id),
}
}
}
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
Message(String),
Eof,
Syntax,
ExpectedBoolean,
ExpectedByte,
ExpectedShort,
ExpectedInteger,
ExpectedLong,
ExpectedFloat,
ExpectedDouble,
ExpectedByteArray,
ExpectedIntArray,
ExpectedLongArray,
ExpectedString,
ExpectedList,
ExpectedMap,
TrailingCharacters,
}
impl Error {
pub fn no_root_compound() -> Self {
Self::Message("The found element should have been an root compound, but it wasn't".to_string())
}
pub fn array_as_other() -> Self {
Self::Message("The found element should have been an array, but it wasn't".to_string())
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Message(format!("io error: {}", e))
}
}
pub type Result<T> = std::result::Result<T, Error>;
impl std::fmt::Display for Error {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::Message(msg) => formatter.write_str(msg),
Error::Eof => formatter.write_str("unexpected end of input"),
Error::Syntax => formatter.write_str("unexpected syntax"),
Error::ExpectedBoolean => formatter.write_str("expected boolean"),
Error::ExpectedByte => formatter.write_str("expected byte"),
Error::ExpectedShort => formatter.write_str("expected short"),
Error::ExpectedInteger => formatter.write_str("expected integer"),
Error::ExpectedLong => formatter.write_str("expected long"),
Error::ExpectedFloat => formatter.write_str("expected float"),
Error::ExpectedDouble => formatter.write_str("expected double"),
Error::ExpectedString => formatter.write_str("expected string"),
Error::ExpectedList => formatter.write_str("expected list"),
Error::ExpectedMap => formatter.write_str("expected map"),
Error::ExpectedByteArray => formatter.write_str("expected byte array"),
Error::ExpectedIntArray => formatter.write_str("expected int array"),
Error::ExpectedLongArray => formatter.write_str("expected long array"),
Error::TrailingCharacters => formatter.write_str("found training characters")
}
}
}
impl std::error::Error for Error {}
impl serde::de::Error for Error {
fn custom<T>(msg:T) -> Self where T:std::fmt::Display {
Error::Message(msg.to_string())
}
}
impl serde::ser::Error for Error {
fn custom<T>(msg:T) -> Self where T:std::fmt::Display {
Error::Message(msg.to_string())
}
}