Skip to main content

ebook_rs/
error.rs

1use std::fmt;
2
3/// Strongly-typed error enum for `ebook-rs` operations and multi-format parsers.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum EbookError {
6    /// File or stream input/output error.
7    Io(String),
8    /// XML or HTML structural parsing error.
9    Xml(String),
10    /// Zip or container archive extraction error.
11    Zip(String),
12    /// Digital Rights Management (DRM / ADEPT / LCP / Mobipocket) restriction.
13    DrmProtected(String),
14    /// Unsupported or invalid eBook format specifications.
15    InvalidFormat(String),
16    /// Data integrity or corrupted file record.
17    CorruptedData(String),
18    /// Missing file, entry, or resource within an eBook archive.
19    NotFound(String),
20    /// General or custom operation error message.
21    Custom(String),
22}
23
24impl fmt::Display for EbookError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            EbookError::Io(msg) => write!(f, "I/O Error: {}", msg),
28            EbookError::Xml(msg) => write!(f, "XML Parse Error: {}", msg),
29            EbookError::Zip(msg) => write!(f, "Zip Archive Error: {}", msg),
30            EbookError::DrmProtected(msg) => write!(f, "DRM Protected: {}", msg),
31            EbookError::InvalidFormat(msg) => write!(f, "Invalid Format: {}", msg),
32            EbookError::CorruptedData(msg) => write!(f, "Corrupted Data: {}", msg),
33            EbookError::NotFound(msg) => write!(f, "Not Found: {}", msg),
34            EbookError::Custom(msg) => write!(f, "{}", msg),
35        }
36    }
37}
38
39impl std::error::Error for EbookError {}
40
41impl From<std::io::Error> for EbookError {
42    fn from(err: std::io::Error) -> Self {
43        EbookError::Io(err.to_string())
44    }
45}
46
47impl From<String> for EbookError {
48    fn from(msg: String) -> Self {
49        EbookError::Custom(msg)
50    }
51}
52
53impl From<&str> for EbookError {
54    fn from(msg: &str) -> Self {
55        EbookError::Custom(msg.to_string())
56    }
57}
58
59impl From<EbookError> for String {
60    fn from(err: EbookError) -> Self {
61        err.to_string()
62    }
63}