1use std::{error::Error, fmt};
2
3use num_derive::FromPrimitive;
4use serde_repr::{Deserialize_repr, Serialize_repr};
5
6use crate::{constants::ErrorCode, FromReturnCode};
7
8#[derive(
15 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
16)]
17#[repr(i8)]
18pub enum DropErrorCode {
19 NotOwner = -1,
20 Busy = -4,
21 NotEnoughResources = -6,
22 InvalidArgs = -10,
23}
24
25impl FromReturnCode for DropErrorCode {
26 type Error = Self;
27
28 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
29 let maybe_result = Self::try_result_from_i8(val);
30 #[cfg(feature = "unsafe-return-conversion")]
31 unsafe {
32 maybe_result.unwrap_unchecked()
33 }
34 #[cfg(not(feature = "unsafe-return-conversion"))]
35 maybe_result.unwrap()
36 }
37
38 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
39 match val {
40 0 => Some(Ok(())),
41 -1 => Some(Err(DropErrorCode::NotOwner)),
42 -4 => Some(Err(DropErrorCode::Busy)),
43 -6 => Some(Err(DropErrorCode::NotEnoughResources)),
44 -10 => Some(Err(DropErrorCode::InvalidArgs)),
45 _ => None,
46 }
47 }
48}
49
50impl fmt::Display for DropErrorCode {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 let msg: &'static str = match self {
53 DropErrorCode::NotOwner => "you are not the owner of this creep",
54 DropErrorCode::Busy => "the creep is still being spawned",
55 DropErrorCode::NotEnoughResources => {
56 "the creep does not have the given amount of resources"
57 }
58 DropErrorCode::InvalidArgs => "the resourcetype is not a valid resource_* constants",
59 };
60
61 write!(f, "{}", msg)
62 }
63}
64
65impl Error for DropErrorCode {}
66
67impl From<DropErrorCode> for ErrorCode {
68 fn from(value: DropErrorCode) -> Self {
69 Self::result_from_i8(value as i8).unwrap_err()
75 }
76}
77
78#[derive(
86 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
87)]
88#[repr(i8)]
89pub enum NotifyWhenAttackedErrorCode {
90 NotOwner = -1,
91 Busy = -4,
92 InvalidArgs = -10,
93}
94
95impl FromReturnCode for NotifyWhenAttackedErrorCode {
96 type Error = Self;
97
98 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
99 let maybe_result = Self::try_result_from_i8(val);
100 #[cfg(feature = "unsafe-return-conversion")]
101 unsafe {
102 maybe_result.unwrap_unchecked()
103 }
104 #[cfg(not(feature = "unsafe-return-conversion"))]
105 maybe_result.unwrap()
106 }
107
108 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
109 match val {
110 0 => Some(Ok(())),
111 -1 => Some(Err(NotifyWhenAttackedErrorCode::NotOwner)),
112 -4 => Some(Err(NotifyWhenAttackedErrorCode::Busy)),
113 -10 => Some(Err(NotifyWhenAttackedErrorCode::InvalidArgs)),
114 _ => None,
115 }
116 }
117}
118
119impl fmt::Display for NotifyWhenAttackedErrorCode {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 let msg: &'static str = match self {
122 NotifyWhenAttackedErrorCode::NotOwner => "you are not the owner of this creep",
123 NotifyWhenAttackedErrorCode::Busy => "the creep is still being spawned",
124 NotifyWhenAttackedErrorCode::InvalidArgs => "enable argument is not a boolean value",
125 };
126
127 write!(f, "{}", msg)
128 }
129}
130
131impl Error for NotifyWhenAttackedErrorCode {}
132
133impl From<NotifyWhenAttackedErrorCode> for ErrorCode {
134 fn from(value: NotifyWhenAttackedErrorCode) -> Self {
135 Self::result_from_i8(value as i8).unwrap_err()
142 }
143}
144
145#[derive(
152 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
153)]
154#[repr(i8)]
155pub enum PickupErrorCode {
156 NotOwner = -1,
157 Busy = -4,
158 InvalidTarget = -7,
159 Full = -8,
160 NotInRange = -9,
161}
162
163impl FromReturnCode for PickupErrorCode {
164 type Error = Self;
165
166 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
167 let maybe_result = Self::try_result_from_i8(val);
168 #[cfg(feature = "unsafe-return-conversion")]
169 unsafe {
170 maybe_result.unwrap_unchecked()
171 }
172 #[cfg(not(feature = "unsafe-return-conversion"))]
173 maybe_result.unwrap()
174 }
175
176 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
177 match val {
178 0 => Some(Ok(())),
179 -1 => Some(Err(PickupErrorCode::NotOwner)),
180 -4 => Some(Err(PickupErrorCode::Busy)),
181 -7 => Some(Err(PickupErrorCode::InvalidTarget)),
182 -8 => Some(Err(PickupErrorCode::Full)),
183 -9 => Some(Err(PickupErrorCode::NotInRange)),
184 _ => None,
185 }
186 }
187}
188
189impl fmt::Display for PickupErrorCode {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 let msg: &'static str = match self {
192 PickupErrorCode::NotOwner => "you are not the owner of this creep",
193 PickupErrorCode::Busy => "the creep is still being spawned",
194 PickupErrorCode::InvalidTarget => "the target is not a valid object to pick up",
195 PickupErrorCode::Full => "the creep cannot receive any more resource",
196 PickupErrorCode::NotInRange => "the target is too far away",
197 };
198
199 write!(f, "{}", msg)
200 }
201}
202
203impl Error for PickupErrorCode {}
204
205impl From<PickupErrorCode> for ErrorCode {
206 fn from(value: PickupErrorCode) -> Self {
207 Self::result_from_i8(value as i8).unwrap_err()
214 }
215}
216
217#[derive(
224 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
225)]
226#[repr(i8)]
227pub enum SayErrorCode {
228 NotOwner = -1,
229 Busy = -4,
230}
231
232impl FromReturnCode for SayErrorCode {
233 type Error = Self;
234
235 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
236 let maybe_result = Self::try_result_from_i8(val);
237 #[cfg(feature = "unsafe-return-conversion")]
238 unsafe {
239 maybe_result.unwrap_unchecked()
240 }
241 #[cfg(not(feature = "unsafe-return-conversion"))]
242 maybe_result.unwrap()
243 }
244
245 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
246 match val {
247 0 => Some(Ok(())),
248 -1 => Some(Err(SayErrorCode::NotOwner)),
249 -4 => Some(Err(SayErrorCode::Busy)),
250 _ => None,
251 }
252 }
253}
254
255impl fmt::Display for SayErrorCode {
256 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257 let msg: &'static str = match self {
258 SayErrorCode::NotOwner => "you are not the owner of this creep",
259 SayErrorCode::Busy => "the creep is still being spawned",
260 };
261
262 write!(f, "{}", msg)
263 }
264}
265
266impl Error for SayErrorCode {}
267
268impl From<SayErrorCode> for ErrorCode {
269 fn from(value: SayErrorCode) -> Self {
270 Self::result_from_i8(value as i8).unwrap_err()
276 }
277}
278
279#[derive(
286 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
287)]
288#[repr(i8)]
289pub enum SuicideErrorCode {
290 NotOwner = -1,
291 Busy = -4,
292}
293
294impl FromReturnCode for SuicideErrorCode {
295 type Error = Self;
296
297 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
298 let maybe_result = Self::try_result_from_i8(val);
299 #[cfg(feature = "unsafe-return-conversion")]
300 unsafe {
301 maybe_result.unwrap_unchecked()
302 }
303 #[cfg(not(feature = "unsafe-return-conversion"))]
304 maybe_result.unwrap()
305 }
306
307 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
308 match val {
309 0 => Some(Ok(())),
310 -1 => Some(Err(SuicideErrorCode::NotOwner)),
311 -4 => Some(Err(SuicideErrorCode::Busy)),
312 _ => None,
313 }
314 }
315}
316
317impl fmt::Display for SuicideErrorCode {
318 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
319 let msg: &'static str = match self {
320 SuicideErrorCode::NotOwner => "you are not the owner of this creep",
321 SuicideErrorCode::Busy => "the creep is still being spawned",
322 };
323
324 write!(f, "{}", msg)
325 }
326}
327
328impl Error for SuicideErrorCode {}
329
330impl From<SuicideErrorCode> for ErrorCode {
331 fn from(value: SuicideErrorCode) -> Self {
332 Self::result_from_i8(value as i8).unwrap_err()
339 }
340}
341
342#[derive(
349 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
350)]
351#[repr(i8)]
352pub enum TransferErrorCode {
353 NotOwner = -1,
354 Busy = -4,
355 NotEnoughResources = -6,
356 InvalidTarget = -7,
357 Full = -8,
358 NotInRange = -9,
359 InvalidArgs = -10,
360}
361
362impl FromReturnCode for TransferErrorCode {
363 type Error = Self;
364
365 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
366 let maybe_result = Self::try_result_from_i8(val);
367 #[cfg(feature = "unsafe-return-conversion")]
368 unsafe {
369 maybe_result.unwrap_unchecked()
370 }
371 #[cfg(not(feature = "unsafe-return-conversion"))]
372 maybe_result.unwrap()
373 }
374
375 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
376 match val {
377 0 => Some(Ok(())),
378 -1 => Some(Err(TransferErrorCode::NotOwner)),
379 -4 => Some(Err(TransferErrorCode::Busy)),
380 -6 => Some(Err(TransferErrorCode::NotEnoughResources)),
381 -7 => Some(Err(TransferErrorCode::InvalidTarget)),
382 -8 => Some(Err(TransferErrorCode::Full)),
383 -9 => Some(Err(TransferErrorCode::NotInRange)),
384 -10 => Some(Err(TransferErrorCode::InvalidArgs)),
385 _ => None,
386 }
387 }
388}
389
390impl fmt::Display for TransferErrorCode {
391 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
392 let msg: &'static str = match self {
393 TransferErrorCode::NotOwner => "you are not the owner of this creep",
394 TransferErrorCode::Busy => "the creep is still being spawned",
395 TransferErrorCode::NotEnoughResources => "the creep does not have the given amount of resources",
396 TransferErrorCode::InvalidTarget => "the target is not a valid object which can contain the specified resource",
397 TransferErrorCode::Full => "the target cannot receive any more resources",
398 TransferErrorCode::NotInRange => "the target is too far away",
399 TransferErrorCode::InvalidArgs => "the resourcetype is not one of the resource_* constants, or the amount is incorrect",
400 };
401
402 write!(f, "{}", msg)
403 }
404}
405
406impl Error for TransferErrorCode {}
407
408impl From<TransferErrorCode> for ErrorCode {
409 fn from(value: TransferErrorCode) -> Self {
410 Self::result_from_i8(value as i8).unwrap_err()
417 }
418}
419
420#[derive(
427 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
428)]
429#[repr(i8)]
430pub enum WithdrawErrorCode {
431 NotOwner = -1,
432 Busy = -4,
433 NotEnoughResources = -6,
434 InvalidTarget = -7,
435 Full = -8,
436 NotInRange = -9,
437 InvalidArgs = -10,
438}
439
440impl FromReturnCode for WithdrawErrorCode {
441 type Error = Self;
442
443 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
444 let maybe_result = Self::try_result_from_i8(val);
445 #[cfg(feature = "unsafe-return-conversion")]
446 unsafe {
447 maybe_result.unwrap_unchecked()
448 }
449 #[cfg(not(feature = "unsafe-return-conversion"))]
450 maybe_result.unwrap()
451 }
452
453 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
454 match val {
455 0 => Some(Ok(())),
456 -1 => Some(Err(WithdrawErrorCode::NotOwner)),
457 -4 => Some(Err(WithdrawErrorCode::Busy)),
458 -6 => Some(Err(WithdrawErrorCode::NotEnoughResources)),
459 -7 => Some(Err(WithdrawErrorCode::InvalidTarget)),
460 -8 => Some(Err(WithdrawErrorCode::Full)),
461 -9 => Some(Err(WithdrawErrorCode::NotInRange)),
462 -10 => Some(Err(WithdrawErrorCode::InvalidArgs)),
463 _ => None,
464 }
465 }
466}
467
468impl fmt::Display for WithdrawErrorCode {
469 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
470 let msg: &'static str = match self {
471 WithdrawErrorCode::NotOwner => "you are not the owner of this creep, or there is a hostile rampart on top of the target",
472 WithdrawErrorCode::Busy => "the creep is still being spawned",
473 WithdrawErrorCode::NotEnoughResources => "the target does not have the given amount of resources",
474 WithdrawErrorCode::InvalidTarget => "the target is not a valid object which can contain the specified resource",
475 WithdrawErrorCode::Full => "the creep's carry is full",
476 WithdrawErrorCode::NotInRange => "the target is too far away",
477 WithdrawErrorCode::InvalidArgs => "the resourcetype is not one of the resource_* constants, or the amount is incorrect",
478 };
479
480 write!(f, "{}", msg)
481 }
482}
483
484impl Error for WithdrawErrorCode {}
485
486impl From<WithdrawErrorCode> for ErrorCode {
487 fn from(value: WithdrawErrorCode) -> Self {
488 Self::result_from_i8(value as i8).unwrap_err()
495 }
496}