use std::{fmt, io};
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("The {0} could not be found.")]
NotFound(ErrorValue),
#[error("The {0} is invalid: {1}.")]
Invalid(ErrorValue, String),
#[error("An error occured while working with the provided resource: {0}")]
Resource(Box<dyn std::error::Error + Send + Sync>),
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Resource(error.into())
}
}
impl From<binrw::Error> for Error {
fn from(error: binrw::Error) -> Self {
Error::Resource(error.into())
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorValue {
Path(String),
#[cfg(feature = "excel")]
Sheet(String),
#[cfg(feature = "exd")]
Row {
row: u32,
subrow: u16,
sheet: Option<String>,
},
#[cfg(feature = "sqpack")]
File(Vec<u8>),
Other(String),
}
impl fmt::Display for ErrorValue {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Path(path) => write!(formatter, "path {path:?}"),
#[cfg(feature = "excel")]
Self::Sheet(sheet) => write!(formatter, "Excel sheet {sheet:?}"),
#[cfg(feature = "exd")]
Self::Row { row, subrow, sheet } => write!(
formatter,
"Excel row {}/{row}:{subrow}",
sheet.as_deref().unwrap_or("(none)"),
),
#[cfg(feature = "sqpack")]
Self::File(file) => write!(formatter, "SqPack file ({} bytes)", file.len()),
Self::Other(value) => write!(formatter, "{value}"),
}
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;