fish_lib/game/errors/
resource.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use thiserror::Error;

#[derive(Error, Debug)]
pub enum GameResourceError {
    #[error("Location with id '{location_id}' does not exist")]
    LocationNotFound { location_id: i32 },
    #[error("User with external id '{external_id}' has no fishing history with species with id '{species_id}'")]
    NoFishingHistory { external_id: i64, species_id: i32 },
    #[error("Species with id '{species_id}' does not exist")]
    SpeciesNotFound { species_id: i32 },
    #[error("User with external id '{external_id}' already exists")]
    UserAlreadyExists { external_id: i64 },
    #[error("User with external id '{external_id}' does not exist")]
    UserNotFound { external_id: i64 },
}

impl GameResourceError {
    pub fn location_not_found(location_id: i32) -> Self {
        Self::LocationNotFound { location_id }
    }

    pub fn no_fishing_history(external_id: i64, species_id: i32) -> Self {
        Self::NoFishingHistory {
            external_id,
            species_id,
        }
    }

    pub fn species_not_found(species_id: i32) -> Self {
        Self::SpeciesNotFound { species_id }
    }

    pub fn user_already_exists(external_id: i64) -> Self {
        Self::UserAlreadyExists { external_id }
    }

    pub fn user_not_found(external_id: i64) -> Self {
        Self::UserNotFound { external_id }
    }

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

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

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

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

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

    pub fn get_external_id(&self) -> Option<i64> {
        match self {
            Self::NoFishingHistory { external_id, .. } => Some(*external_id),
            Self::UserAlreadyExists { external_id } => Some(*external_id),
            Self::UserNotFound { external_id } => Some(*external_id),
            _ => None,
        }
    }

    pub fn get_location_id(&self) -> Option<i32> {
        match self {
            Self::LocationNotFound { location_id } => Some(*location_id),
            _ => None,
        }
    }

    pub fn get_species_id(&self) -> Option<i32> {
        match self {
            Self::NoFishingHistory { species_id, .. } => Some(*species_id),
            Self::SpeciesNotFound { species_id } => Some(*species_id),
            _ => None,
        }
    }
}