fish_lib/game/errors/
resource.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum GameResourceError {
5    #[error("User with id '{user_id}' has no fishing history with species with id '{species_id}'")]
6    FishingHistoryNotFound { user_id: i64, species_id: i32 },
7    #[error("User with external id '{external_id}' has reached the maximum amount of instances for item of type id '{item_type_id}'")]
8    ItemMaxCountExceeded { item_type_id: i32, external_id: i64 },
9    #[error("Item of type id '{item_type_id}' does not exist")]
10    ItemNotFound { item_type_id: i32 },
11    #[error("Item of type id '{item_type_id}' was unable to be stacked: {msg}")]
12    ItemUnstackable { msg: String, item_type_id: i32 },
13    #[error("User with external id '{external_id}' has already unlocked location with id '{location_id}'")]
14    LocationAlreadyUnlocked { external_id: i64, location_id: i32 },
15    #[error("Location with id '{location_id}' does not exist")]
16    LocationNotFound { location_id: i32 },
17    #[error("No available encounters for the specified conditions")]
18    NoAvailableEncounters,
19    #[error("User with external id '{external_id}' has no fishing history with species with id '{species_id}'")]
20    NoFishingHistory { external_id: i64, species_id: i32 },
21    #[error("Species with id '{species_id}' does not exist")]
22    SpeciesNotFound { species_id: i32 },
23    #[error(
24        "Unable to unlock location with id '{location_id}' because of unmet unlock requirements"
25    )]
26    UnmetLocationUnlockRequirements { location_id: i32 },
27    #[error("User with external id '{external_id}' already exists")]
28    UserAlreadyExists { external_id: i64 },
29    #[error("User with external id '{external_id}' does not exist")]
30    UserNotFound { external_id: i64 },
31}
32
33impl GameResourceError {
34    pub fn fishing_history_not_found(user_id: i64, species_id: i32) -> Self {
35        Self::FishingHistoryNotFound {
36            user_id,
37            species_id,
38        }
39    }
40
41    pub fn item_max_count_exceeded(item_type_id: i32, external_id: i64) -> Self {
42        Self::ItemMaxCountExceeded {
43            item_type_id,
44            external_id,
45        }
46    }
47
48    pub fn item_not_found(item_type_id: i32) -> Self {
49        Self::ItemNotFound { item_type_id }
50    }
51
52    pub fn item_unstackable(item_type_id: i32, message: &str) -> Self {
53        Self::ItemUnstackable {
54            item_type_id,
55            msg: message.to_string(),
56        }
57    }
58
59    pub fn location_already_unlocked(external_id: i64, location_id: i32) -> Self {
60        Self::LocationAlreadyUnlocked {
61            external_id,
62            location_id,
63        }
64    }
65
66    pub fn location_not_found(location_id: i32) -> Self {
67        Self::LocationNotFound { location_id }
68    }
69
70    pub fn no_available_encounters() -> Self {
71        Self::NoAvailableEncounters
72    }
73
74    pub fn no_fishing_history(external_id: i64, species_id: i32) -> Self {
75        Self::NoFishingHistory {
76            external_id,
77            species_id,
78        }
79    }
80
81    pub fn species_not_found(species_id: i32) -> Self {
82        Self::SpeciesNotFound { species_id }
83    }
84
85    pub fn unmet_location_unlock_requirements(location_id: i32) -> Self {
86        Self::UnmetLocationUnlockRequirements { location_id }
87    }
88
89    pub fn user_already_exists(external_id: i64) -> Self {
90        Self::UserAlreadyExists { external_id }
91    }
92
93    pub fn user_not_found(external_id: i64) -> Self {
94        Self::UserNotFound { external_id }
95    }
96
97    pub fn is_fishing_history_not_found(&self) -> bool {
98        matches!(self, Self::FishingHistoryNotFound { .. })
99    }
100
101    pub fn is_item_max_count_exceeded(&self) -> bool {
102        matches!(self, Self::ItemMaxCountExceeded { .. })
103    }
104
105    pub fn is_item_not_found(&self) -> bool {
106        matches!(self, Self::ItemNotFound { .. })
107    }
108
109    pub fn is_item_unstackable(&self) -> bool {
110        matches!(self, Self::ItemUnstackable { .. })
111    }
112
113    pub fn is_location_already_unlocked(&self) -> bool {
114        matches!(self, Self::LocationAlreadyUnlocked { .. })
115    }
116
117    pub fn is_location_not_found(&self) -> bool {
118        matches!(self, Self::LocationNotFound { .. })
119    }
120
121    pub fn is_no_available_encounters(&self) -> bool {
122        matches!(self, Self::NoAvailableEncounters)
123    }
124
125    pub fn is_no_fishing_history(&self) -> bool {
126        matches!(self, Self::NoFishingHistory { .. })
127    }
128
129    pub fn is_species_not_found(&self) -> bool {
130        matches!(self, Self::SpeciesNotFound { .. })
131    }
132
133    pub fn is_unmet_location_unlock_requirements(&self) -> bool {
134        matches!(self, Self::UnmetLocationUnlockRequirements { .. })
135    }
136
137    pub fn is_user_already_exists(&self) -> bool {
138        matches!(self, Self::UserAlreadyExists { .. })
139    }
140
141    pub fn is_user_not_found(&self) -> bool {
142        matches!(self, Self::UserNotFound { .. })
143    }
144
145    pub fn get_external_id(&self) -> Option<i64> {
146        match self {
147            Self::ItemMaxCountExceeded { external_id, .. } => Some(*external_id),
148            Self::LocationAlreadyUnlocked { external_id, .. } => Some(*external_id),
149            Self::NoFishingHistory { external_id, .. } => Some(*external_id),
150            Self::UserAlreadyExists { external_id } => Some(*external_id),
151            Self::UserNotFound { external_id } => Some(*external_id),
152            _ => None,
153        }
154    }
155
156    pub fn get_item_type_id(&self) -> Option<i32> {
157        match self {
158            Self::ItemMaxCountExceeded { item_type_id, .. } => Some(*item_type_id),
159            Self::ItemNotFound { item_type_id, .. } => Some(*item_type_id),
160            Self::ItemUnstackable { item_type_id, .. } => Some(*item_type_id),
161            _ => None,
162        }
163    }
164
165    pub fn get_location_id(&self) -> Option<i32> {
166        match self {
167            Self::LocationAlreadyUnlocked { location_id, .. } => Some(*location_id),
168            Self::LocationNotFound { location_id } => Some(*location_id),
169            Self::UnmetLocationUnlockRequirements { location_id, .. } => Some(*location_id),
170            _ => None,
171        }
172    }
173
174    pub fn get_message(&self) -> Option<&str> {
175        match self {
176            Self::ItemUnstackable { msg, .. } => Some(msg),
177            _ => None,
178        }
179    }
180
181    pub fn get_species_id(&self) -> Option<i32> {
182        match self {
183            Self::FishingHistoryNotFound { species_id, .. } => Some(*species_id),
184            Self::NoFishingHistory { species_id, .. } => Some(*species_id),
185            Self::SpeciesNotFound { species_id } => Some(*species_id),
186            _ => None,
187        }
188    }
189
190    pub fn get_user_id(&self) -> Option<i64> {
191        match self {
192            Self::FishingHistoryNotFound { user_id, .. } => Some(*user_id),
193            _ => None,
194        }
195    }
196}