use std::fmt;
#[derive(Debug)]
pub enum EpubError {
Io(std::io::Error),
Zip(zip::result::ZipError),
Xml(quick_xml::Error),
Utf8(std::str::Utf8Error),
OpfNotFound,
ChapterNotFound(String),
InvalidChapterIndex(usize),
CacheLockError,
Encoding(quick_xml::encoding::EncodingError),
}
impl fmt::Display for EpubError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EpubError::Io(err) => write!(f, "IO error: {}", err),
EpubError::Zip(err) => write!(f, "ZIP error: {}", err),
EpubError::Xml(err) => write!(f, "XML parsing error: {}", err),
EpubError::Utf8(err) => write!(f, "UTF-8 encoding error: {}", err),
EpubError::OpfNotFound => write!(f, "OPF file not found"),
EpubError::ChapterNotFound(path) => write!(f, "Chapter file not found: {}", path),
EpubError::InvalidChapterIndex(idx) => {
write!(f, "Invalid chapter index: {}", idx)
}
EpubError::CacheLockError => {
write!(f, "Failed to acquire cache lock")
}
EpubError::Encoding(err) => write!(f, "Encoding error: {}", err),
}
}
}
impl std::error::Error for EpubError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
EpubError::Io(err) => Some(err),
EpubError::Zip(err) => Some(err),
EpubError::Xml(err) => Some(err),
EpubError::Utf8(err) => Some(err),
EpubError::Encoding(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for EpubError {
fn from(err: std::io::Error) -> Self {
EpubError::Io(err)
}
}
impl From<zip::result::ZipError> for EpubError {
fn from(err: zip::result::ZipError) -> Self {
EpubError::Zip(err)
}
}
impl From<quick_xml::Error> for EpubError {
fn from(err: quick_xml::Error) -> Self {
EpubError::Xml(err)
}
}
impl From<quick_xml::encoding::EncodingError> for EpubError {
fn from(err: quick_xml::encoding::EncodingError) -> Self {
EpubError::Encoding(err)
}
}
impl From<std::str::Utf8Error> for EpubError {
fn from(err: std::str::Utf8Error) -> Self {
EpubError::Utf8(err)
}
}
impl From<std::string::FromUtf8Error> for EpubError {
fn from(err: std::string::FromUtf8Error) -> Self {
EpubError::Utf8(err.utf8_error())
}
}
impl From<quick_xml::events::attributes::AttrError> for EpubError {
fn from(err: quick_xml::events::attributes::AttrError) -> Self {
EpubError::Xml(quick_xml::Error::InvalidAttr(err))
}
}