1use std::fmt;
2
3#[derive(Debug)]
4pub enum StorageError {
5 DatabaseUnavailable(String),
7 NotFound(String),
9 DuplicateKey(String),
11 MigrationRequired(String),
13 Sqlite(rusqlite::Error),
15}
16
17impl fmt::Display for StorageError {
18 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
19 match self {
20 Self::DatabaseUnavailable(message) => {
21 write!(formatter, "[1001] database unavailable: {message}")
22 }
23 Self::NotFound(message) => {
24 write!(formatter, "[1002] not found: {message}")
25 }
26 Self::DuplicateKey(message) => {
27 write!(formatter, "[1003] duplicate key: {message}")
28 }
29 Self::MigrationRequired(message) => {
30 write!(formatter, "[1004] migration required: {message}")
31 }
32 Self::Sqlite(error) => {
33 write!(formatter, "sqlite error: {error}")
34 }
35 }
36 }
37}
38
39impl std::error::Error for StorageError {
40 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41 match self {
42 Self::Sqlite(error) => Some(error),
43 _ => None,
44 }
45 }
46}
47
48impl From<rusqlite::Error> for StorageError {
49 fn from(error: rusqlite::Error) -> Self {
50 Self::Sqlite(error)
51 }
52}