Skip to main content

spam_db/
error.rs

1use std::fmt;
2
3/// Errors that can occur when opening or querying a spam database.
4#[derive(Debug)]
5pub enum Error {
6  /// An I/O error reading the database file.
7  Io(std::io::Error),
8  /// The file does not match the expected spam-db format.
9  InvalidDatabase(String),
10}
11
12impl fmt::Display for Error {
13  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14    match self {
15      Error::Io(e) => write!(f, "I/O error: {e}"),
16      Error::InvalidDatabase(msg) => write!(f, "invalid database: {msg}"),
17    }
18  }
19}
20
21impl std::error::Error for Error {
22  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
23    match self {
24      Error::Io(e) => Some(e),
25      Error::InvalidDatabase(_) => None,
26    }
27  }
28}
29
30impl From<std::io::Error> for Error {
31  fn from(e: std::io::Error) -> Self {
32    Error::Io(e)
33  }
34}