Skip to main content

opendict/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4#[non_exhaustive]
5pub enum Error {
6    /// File I/O failures (not found, permission denied, read errors)
7    Io(std::io::Error),
8    /// File is corrupt or malformed (bad magic, truncated, checksum mismatch)
9    InvalidFormat(String),
10    /// File uses features not yet implemented (LZO, Salsa20, v1.2, etc.)
11    Unsupported(String),
12}
13
14pub type Result<T> = std::result::Result<T, Error>;
15
16impl fmt::Display for Error {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Error::Io(e) => write!(f, "I/O error: {}", e),
20            Error::InvalidFormat(msg) => write!(f, "invalid format: {}", msg),
21            Error::Unsupported(msg) => write!(f, "unsupported: {}", msg),
22        }
23    }
24}
25
26impl std::error::Error for Error {
27    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28        match self {
29            Error::Io(e) => Some(e),
30            _ => None,
31        }
32    }
33}
34
35impl From<std::io::Error> for Error {
36    fn from(e: std::io::Error) -> Self {
37        Error::Io(e)
38    }
39}