fish_lib/game/errors/
database.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use thiserror::Error;

#[derive(Error, Debug)]
pub enum GameDatabaseError {
    #[error("Database connection failed: {msg}")]
    ConnectionFailed { msg: String },
    #[error("Database migrations failed: {msg}")]
    MigrationsFailed { msg: String },
    #[error("No database connection specified")]
    MissingConnection,
}

impl GameDatabaseError {
    pub fn connection_failed(msg: &str) -> Self {
        Self::ConnectionFailed {
            msg: msg.to_string(),
        }
    }

    pub fn migrations_failed(msg: &str) -> Self {
        Self::MigrationsFailed {
            msg: msg.to_string(),
        }
    }

    pub fn missing_connection() -> Self {
        Self::MissingConnection
    }

    pub fn is_connection_failed(&self) -> bool {
        matches!(self, Self::ConnectionFailed { .. })
    }

    pub fn is_migrations_failed(&self) -> bool {
        matches!(self, Self::MigrationsFailed { .. })
    }

    pub fn is_missing_connection(&self) -> bool {
        matches!(self, Self::MissingConnection)
    }
}