use std::error::Error;
use std::fmt::{Display, Formatter};
use std::string::FromUtf8Error;
use wow_world_base::EnumError;
#[derive(Debug)]
pub enum DbcError {
Io(std::io::Error),
InvalidEnum(EnumError),
String(FromUtf8Error),
InvalidHeader(InvalidHeaderError),
}
impl Display for DbcError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
DbcError::Io(i) => i.fmt(f),
DbcError::InvalidEnum(i) => i.fmt(f),
DbcError::String(i) => i.fmt(f),
DbcError::InvalidHeader(i) => i.fmt(f),
}
}
}
impl Error for DbcError {}
impl From<std::io::Error> for DbcError {
fn from(i: std::io::Error) -> Self {
Self::Io(i)
}
}
impl From<FromUtf8Error> for DbcError {
fn from(e: FromUtf8Error) -> Self {
Self::String(e)
}
}
impl From<InvalidHeaderError> for DbcError {
fn from(e: InvalidHeaderError) -> Self {
Self::InvalidHeader(e)
}
}
#[derive(Debug)]
pub enum InvalidHeaderError {
MagicValue {
actual: u32,
},
RecordSize {
expected: u32,
actual: u32,
},
FieldCount {
expected: u32,
actual: u32,
},
}
impl Display for InvalidHeaderError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
InvalidHeaderError::RecordSize { expected, actual } => {
write!(
f,
"invalid record size. Expected '{}', got '{}'",
expected, actual
)
}
InvalidHeaderError::FieldCount { expected, actual } => write!(
f,
"invalid field count. Expected '{}', got '{}'",
expected, actual
),
InvalidHeaderError::MagicValue { actual } => {
write!(f, "invalid header magic: '{}'", actual)
}
}
}
}
impl Error for InvalidHeaderError {}
impl From<EnumError> for DbcError {
fn from(i: EnumError) -> Self {
Self::InvalidEnum(i)
}
}