screeps/enums/action_error_codes/
powercreep_error_codes.rs

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/// Error codes used by [PowerCreep::create](crate::PowerCreep::create).
9///
10/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.create).
11///
12/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L395)
13#[derive(
14    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
15)]
16#[repr(i8)]
17pub enum PowerCreepCreateErrorCode {
18    NameExists = -3,
19    NotEnoughResources = -6,
20    InvalidArgs = -10,
21}
22
23impl FromReturnCode for PowerCreepCreateErrorCode {
24    type Error = Self;
25
26    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
27        let maybe_result = Self::try_result_from_i8(val);
28        #[cfg(feature = "unsafe-return-conversion")]
29        unsafe {
30            maybe_result.unwrap_unchecked()
31        }
32        #[cfg(not(feature = "unsafe-return-conversion"))]
33        maybe_result.unwrap()
34    }
35
36    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
37        match val {
38            0 => Some(Ok(())),
39            -3 => Some(Err(PowerCreepCreateErrorCode::NameExists)),
40            -6 => Some(Err(PowerCreepCreateErrorCode::NotEnoughResources)),
41            -10 => Some(Err(PowerCreepCreateErrorCode::InvalidArgs)),
42            _ => None,
43        }
44    }
45}
46
47impl fmt::Display for PowerCreepCreateErrorCode {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        let msg: &'static str = match self {
50            PowerCreepCreateErrorCode::NameExists => "a power creep with the specified name already exists",
51            PowerCreepCreateErrorCode::NotEnoughResources => "you don't have free power levels in your account",
52            PowerCreepCreateErrorCode::InvalidArgs => "the provided power creep name is exceeds the limit, or the power creep class is invalid",
53        };
54
55        write!(f, "{}", msg)
56    }
57}
58
59impl Error for PowerCreepCreateErrorCode {}
60
61impl From<PowerCreepCreateErrorCode> for ErrorCode {
62    fn from(value: PowerCreepCreateErrorCode) -> Self {
63        // Safety: PowerCreepCreateErrorCode is repr(i8), so we can cast it to get the
64        // discriminant value, which will match the raw return code value that ErrorCode
65        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
66        // Safety: PowerCreepCreateErrorCode discriminants are always error code values,
67        // and thus the Result returned here will always be an `Err` variant, so we can
68        // always extract the error without panicking
69        Self::result_from_i8(value as i8).unwrap_err()
70    }
71}
72
73/// Error codes used by
74/// [PowerCreep::cancel_order](crate::PowerCreep::cancel_order).
75///
76/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.cancelOrder).
77///
78/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L342)
79#[derive(
80    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
81)]
82#[repr(i8)]
83pub enum PowerCreepCancelOrderErrorCode {
84    NotOwner = -1,
85    Busy = -4,
86    NotFound = -5,
87}
88
89impl FromReturnCode for PowerCreepCancelOrderErrorCode {
90    type Error = Self;
91
92    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
93        let maybe_result = Self::try_result_from_i8(val);
94        #[cfg(feature = "unsafe-return-conversion")]
95        unsafe {
96            maybe_result.unwrap_unchecked()
97        }
98        #[cfg(not(feature = "unsafe-return-conversion"))]
99        maybe_result.unwrap()
100    }
101
102    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
103        match val {
104            0 => Some(Ok(())),
105            -1 => Some(Err(PowerCreepCancelOrderErrorCode::NotOwner)),
106            -4 => Some(Err(PowerCreepCancelOrderErrorCode::Busy)),
107            -5 => Some(Err(PowerCreepCancelOrderErrorCode::NotFound)),
108            _ => None,
109        }
110    }
111}
112
113impl fmt::Display for PowerCreepCancelOrderErrorCode {
114    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115        let msg: &'static str = match self {
116            PowerCreepCancelOrderErrorCode::NotOwner => "you are not the owner of the creep",
117            PowerCreepCancelOrderErrorCode::Busy => "the power creep is not spawned in the world",
118            PowerCreepCancelOrderErrorCode::NotFound => {
119                "the order with the specified name is not found"
120            }
121        };
122
123        write!(f, "{}", msg)
124    }
125}
126
127impl Error for PowerCreepCancelOrderErrorCode {}
128
129impl From<PowerCreepCancelOrderErrorCode> for ErrorCode {
130    fn from(value: PowerCreepCancelOrderErrorCode) -> Self {
131        // Safety: PowerCreepCancelOrderErrorCode is repr(i8), so we can cast it to get
132        // the discriminant value, which will match the raw return code value that
133        // ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
134        // Safety: PowerCreepCancelOrderErrorCode discriminants are always error code
135        // values, and thus the Result returned here will always be an `Err` variant, so
136        // we can always extract the error without panicking
137        Self::result_from_i8(value as i8).unwrap_err()
138    }
139}
140
141/// Error codes used by
142/// [AccountPowerCreep::delete](crate::AccountPowerCreep::delete).
143///
144/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.delete).
145///
146/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L204)
147#[derive(
148    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
149)]
150#[repr(i8)]
151pub enum DeleteErrorCode {
152    NotOwner = -1,
153    Busy = -4,
154}
155
156impl FromReturnCode for DeleteErrorCode {
157    type Error = Self;
158
159    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
160        let maybe_result = Self::try_result_from_i8(val);
161        #[cfg(feature = "unsafe-return-conversion")]
162        unsafe {
163            maybe_result.unwrap_unchecked()
164        }
165        #[cfg(not(feature = "unsafe-return-conversion"))]
166        maybe_result.unwrap()
167    }
168
169    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
170        match val {
171            0 => Some(Ok(())),
172            -1 => Some(Err(DeleteErrorCode::NotOwner)),
173            -4 => Some(Err(DeleteErrorCode::Busy)),
174            _ => None,
175        }
176    }
177}
178
179impl fmt::Display for DeleteErrorCode {
180    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181        let msg: &'static str = match self {
182            DeleteErrorCode::NotOwner => "you are not the owner of the creep",
183            DeleteErrorCode::Busy => "the power creep is spawned in the world",
184        };
185
186        write!(f, "{}", msg)
187    }
188}
189
190impl Error for DeleteErrorCode {}
191
192impl From<DeleteErrorCode> for ErrorCode {
193    fn from(value: DeleteErrorCode) -> Self {
194        // Safety: DeleteErrorCode is repr(i8), so we can cast it to get the
195        // discriminant value, which will match the raw return code value that ErrorCode
196        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
197        // Safety: DeleteErrorCode discriminants are always error code values, and thus
198        // the Result returned here will always be an `Err` variant, so we can always
199        // extract the error without panicking
200        Self::result_from_i8(value as i8).unwrap_err()
201    }
202}
203
204/// Error codes used by
205/// [PowerCreep::enable_room](crate::PowerCreep::enable_room).
206///
207/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.enableRoom).
208///
209/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L295)
210#[derive(
211    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
212)]
213#[repr(i8)]
214pub enum EnableRoomErrorCode {
215    NotOwner = -1,
216    Busy = -4,
217    InvalidTarget = -7,
218    NotInRange = -9,
219}
220
221impl FromReturnCode for EnableRoomErrorCode {
222    type Error = Self;
223
224    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
225        let maybe_result = Self::try_result_from_i8(val);
226        #[cfg(feature = "unsafe-return-conversion")]
227        unsafe {
228            maybe_result.unwrap_unchecked()
229        }
230        #[cfg(not(feature = "unsafe-return-conversion"))]
231        maybe_result.unwrap()
232    }
233
234    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
235        match val {
236            0 => Some(Ok(())),
237            -1 => Some(Err(EnableRoomErrorCode::NotOwner)),
238            -4 => Some(Err(EnableRoomErrorCode::Busy)),
239            -7 => Some(Err(EnableRoomErrorCode::InvalidTarget)),
240            -9 => Some(Err(EnableRoomErrorCode::NotInRange)),
241            _ => None,
242        }
243    }
244}
245
246impl fmt::Display for EnableRoomErrorCode {
247    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248        let msg: &'static str = match self {
249            EnableRoomErrorCode::NotOwner => "you are not the owner of this creep",
250            EnableRoomErrorCode::Busy => "the power creep is not spawned in the world",
251            EnableRoomErrorCode::InvalidTarget => "the target is not a controller structure",
252            EnableRoomErrorCode::NotInRange => "the target is too far away",
253        };
254
255        write!(f, "{}", msg)
256    }
257}
258
259impl Error for EnableRoomErrorCode {}
260
261impl From<EnableRoomErrorCode> for ErrorCode {
262    fn from(value: EnableRoomErrorCode) -> Self {
263        // Safety: EnableRoomErrorCode is repr(i8), so we can cast it to get the
264        // discriminant value, which will match the raw return code value that ErrorCode
265        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
266        // Safety: EnableRoomErrorCode discriminants are always error code values, and
267        // thus the Result returned here will always be an `Err` variant, so we can
268        // always extract the error without panicking
269        Self::result_from_i8(value as i8).unwrap_err()
270    }
271}
272
273/// Error codes used by
274/// [PowerCreep::move_direction](crate::PowerCreep::move_direction).
275///
276/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.move).
277///
278/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L106)
279#[derive(
280    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
281)]
282#[repr(i8)]
283pub enum PowerCreepMoveDirectionErrorCode {
284    NotOwner = -1,
285    Busy = -4,
286    NotInRange = -9,
287    InvalidArgs = -10,
288    Tired = -11,
289}
290
291impl FromReturnCode for PowerCreepMoveDirectionErrorCode {
292    type Error = Self;
293
294    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
295        let maybe_result = Self::try_result_from_i8(val);
296        #[cfg(feature = "unsafe-return-conversion")]
297        unsafe {
298            maybe_result.unwrap_unchecked()
299        }
300        #[cfg(not(feature = "unsafe-return-conversion"))]
301        maybe_result.unwrap()
302    }
303
304    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
305        match val {
306            0 => Some(Ok(())),
307            -1 => Some(Err(PowerCreepMoveDirectionErrorCode::NotOwner)),
308            -4 => Some(Err(PowerCreepMoveDirectionErrorCode::Busy)),
309            -9 => Some(Err(PowerCreepMoveDirectionErrorCode::NotInRange)),
310            -10 => Some(Err(PowerCreepMoveDirectionErrorCode::InvalidArgs)),
311            -11 => Some(Err(PowerCreepMoveDirectionErrorCode::Tired)),
312            _ => None,
313        }
314    }
315}
316
317impl fmt::Display for PowerCreepMoveDirectionErrorCode {
318    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
319        let msg: &'static str = match self {
320            PowerCreepMoveDirectionErrorCode::NotOwner => "you are not the owner of this creep",
321            PowerCreepMoveDirectionErrorCode::Busy => "the power creep is not spawned in the world",
322            PowerCreepMoveDirectionErrorCode::NotInRange => "the target creep is too far away",
323            PowerCreepMoveDirectionErrorCode::InvalidArgs => "the provided direction is incorrect",
324            PowerCreepMoveDirectionErrorCode::Tired => {
325                "the fatigue indicator of the creep is non-zero"
326            }
327        };
328
329        write!(f, "{}", msg)
330    }
331}
332
333impl Error for PowerCreepMoveDirectionErrorCode {}
334
335impl From<PowerCreepMoveDirectionErrorCode> for ErrorCode {
336    fn from(value: PowerCreepMoveDirectionErrorCode) -> Self {
337        // Safety: PowerCreepMoveDirectionErrorCode is repr(i8), so we can cast it to
338        // get the discriminant value, which will match the raw return code value that
339        // ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
340        // Safety: PowerCreepMoveDirectionErrorCode discriminants are always error code
341        // values, and thus the Result returned here will always be an `Err` variant, so
342        // we can always extract the error without panicking
343        Self::result_from_i8(value as i8).unwrap_err()
344    }
345}
346
347/// Error codes used by
348/// [PowerCreep::move_by_path](crate::PowerCreep::move_by_path).
349///
350/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.moveByPath).
351///
352/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L120)
353#[derive(
354    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
355)]
356#[repr(i8)]
357pub enum PowerCreepMoveByPathErrorCode {
358    NotOwner = -1,
359    Busy = -4,
360    NotFound = -5,
361    InvalidArgs = -10,
362    Tired = -11,
363}
364
365impl FromReturnCode for PowerCreepMoveByPathErrorCode {
366    type Error = Self;
367
368    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
369        let maybe_result = Self::try_result_from_i8(val);
370        #[cfg(feature = "unsafe-return-conversion")]
371        unsafe {
372            maybe_result.unwrap_unchecked()
373        }
374        #[cfg(not(feature = "unsafe-return-conversion"))]
375        maybe_result.unwrap()
376    }
377
378    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
379        match val {
380            0 => Some(Ok(())),
381            -1 => Some(Err(PowerCreepMoveByPathErrorCode::NotOwner)),
382            -4 => Some(Err(PowerCreepMoveByPathErrorCode::Busy)),
383            -5 => Some(Err(PowerCreepMoveByPathErrorCode::NotFound)),
384            -10 => Some(Err(PowerCreepMoveByPathErrorCode::InvalidArgs)),
385            -11 => Some(Err(PowerCreepMoveByPathErrorCode::Tired)),
386            _ => None,
387        }
388    }
389}
390
391impl fmt::Display for PowerCreepMoveByPathErrorCode {
392    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
393        let msg: &'static str = match self {
394            PowerCreepMoveByPathErrorCode::NotOwner => "you are not the owner of this creep",
395            PowerCreepMoveByPathErrorCode::Busy => "the power creep is not spawned in the world",
396            PowerCreepMoveByPathErrorCode::NotFound => {
397                "the specified path doesn't match the creep's location"
398            }
399            PowerCreepMoveByPathErrorCode::InvalidArgs => "path is not a valid path array",
400            PowerCreepMoveByPathErrorCode::Tired => {
401                "the fatigue indicator of the creep is non-zero"
402            }
403        };
404
405        write!(f, "{}", msg)
406    }
407}
408
409impl Error for PowerCreepMoveByPathErrorCode {}
410
411impl From<PowerCreepMoveByPathErrorCode> for ErrorCode {
412    fn from(value: PowerCreepMoveByPathErrorCode) -> Self {
413        // Safety: PowerCreepMoveByPathErrorCode is repr(i8), so we can cast it to get
414        // the discriminant value, which will match the raw return code value that
415        // ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
416        // Safety: PowerCreepMoveByPathErrorCode discriminants are always error code
417        // values, and thus the Result returned here will always be an `Err` variant, so
418        // we can always extract the error without panicking
419        Self::result_from_i8(value as i8).unwrap_err()
420    }
421}
422
423/// Error codes used by [PowerCreep::move_to](crate::PowerCreep#method.move_to).
424///
425/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.moveTo).
426///
427/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L113)
428#[derive(
429    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
430)]
431#[repr(i8)]
432pub enum PowerCreepMoveToErrorCode {
433    NotOwner = -1,
434    NoPath = -2,
435    Busy = -4,
436    NotFound = -5,
437    InvalidTarget = -7,
438    Tired = -11,
439}
440
441impl FromReturnCode for PowerCreepMoveToErrorCode {
442    type Error = Self;
443
444    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
445        let maybe_result = Self::try_result_from_i8(val);
446        #[cfg(feature = "unsafe-return-conversion")]
447        unsafe {
448            maybe_result.unwrap_unchecked()
449        }
450        #[cfg(not(feature = "unsafe-return-conversion"))]
451        maybe_result.unwrap()
452    }
453
454    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
455        match val {
456            0 => Some(Ok(())),
457            -1 => Some(Err(PowerCreepMoveToErrorCode::NotOwner)),
458            -2 => Some(Err(PowerCreepMoveToErrorCode::NoPath)),
459            -4 => Some(Err(PowerCreepMoveToErrorCode::Busy)),
460            -5 => Some(Err(PowerCreepMoveToErrorCode::NotFound)),
461            -7 => Some(Err(PowerCreepMoveToErrorCode::InvalidTarget)),
462            -11 => Some(Err(PowerCreepMoveToErrorCode::Tired)),
463            _ => None,
464        }
465    }
466}
467
468impl fmt::Display for PowerCreepMoveToErrorCode {
469    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
470        let msg: &'static str = match self {
471            PowerCreepMoveToErrorCode::NotOwner => "you are not the owner of this creep",
472            PowerCreepMoveToErrorCode::NoPath => "no path to the target could be found",
473            PowerCreepMoveToErrorCode::Busy => "the power creep is not spawned in the world",
474            PowerCreepMoveToErrorCode::NotFound => "the creep has no memorized path to reuse",
475            PowerCreepMoveToErrorCode::InvalidTarget => "the target provided is invalid",
476            PowerCreepMoveToErrorCode::Tired => "the fatigue indicator of the creep is non-zero",
477        };
478
479        write!(f, "{}", msg)
480    }
481}
482
483impl Error for PowerCreepMoveToErrorCode {}
484
485impl From<PowerCreepMoveToErrorCode> for ErrorCode {
486    fn from(value: PowerCreepMoveToErrorCode) -> Self {
487        // Safety: PowerCreepMoveToErrorCode is repr(i8), so we can cast it to get the
488        // discriminant value, which will match the raw return code value that ErrorCode
489        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
490        // Safety: PowerCreepMoveToErrorCode discriminants are always error code values,
491        // and thus the Result returned here will always be an `Err` variant, so we can
492        // always extract the error without panicking
493        Self::result_from_i8(value as i8).unwrap_err()
494    }
495}
496
497/// Error codes used by
498/// [AccountPowerCreep::rename](crate::AccountPowerCreep::rename).
499///
500/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.rename).
501///
502/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L356)
503#[derive(
504    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
505)]
506#[repr(i8)]
507pub enum RenameErrorCode {
508    NotOwner = -1,
509    NameExists = -3,
510    Busy = -4,
511    InvalidArgs = -10,
512}
513
514impl FromReturnCode for RenameErrorCode {
515    type Error = Self;
516
517    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
518        let maybe_result = Self::try_result_from_i8(val);
519        #[cfg(feature = "unsafe-return-conversion")]
520        unsafe {
521            maybe_result.unwrap_unchecked()
522        }
523        #[cfg(not(feature = "unsafe-return-conversion"))]
524        maybe_result.unwrap()
525    }
526
527    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
528        match val {
529            0 => Some(Ok(())),
530            -1 => Some(Err(RenameErrorCode::NotOwner)),
531            -3 => Some(Err(RenameErrorCode::NameExists)),
532            -4 => Some(Err(RenameErrorCode::Busy)),
533            -10 => Some(Err(RenameErrorCode::InvalidArgs)),
534            _ => None,
535        }
536    }
537}
538
539impl fmt::Display for RenameErrorCode {
540    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
541        let msg: &'static str = match self {
542            RenameErrorCode::NotOwner => "you are not the owner of the creep",
543            RenameErrorCode::NameExists => "a power creep with the specified name already exists",
544            RenameErrorCode::Busy => "the power creep is spawned in the world",
545            RenameErrorCode::InvalidArgs => "the provided power creep name is exceeds the limit",
546        };
547
548        write!(f, "{}", msg)
549    }
550}
551
552impl Error for RenameErrorCode {}
553
554impl From<RenameErrorCode> for ErrorCode {
555    fn from(value: RenameErrorCode) -> Self {
556        // Safety: RenameErrorCode is repr(i8), so we can cast it to get the
557        // discriminant value, which will match the raw return code value that ErrorCode
558        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
559        // Safety: RenameErrorCode discriminants are always error code values, and thus
560        // the Result returned here will always be an `Err` variant, so we can always
561        // extract the error without panicking
562        Self::result_from_i8(value as i8).unwrap_err()
563    }
564}
565
566/// Error codes used by [PowerCreep::renew](crate::PowerCreep::renew).
567///
568/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.renew).
569///
570/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L319)
571#[derive(
572    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
573)]
574#[repr(i8)]
575pub enum RenewErrorCode {
576    NotOwner = -1,
577    Busy = -4,
578    InvalidTarget = -7,
579    NotInRange = -9,
580}
581
582impl FromReturnCode for RenewErrorCode {
583    type Error = Self;
584
585    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
586        let maybe_result = Self::try_result_from_i8(val);
587        #[cfg(feature = "unsafe-return-conversion")]
588        unsafe {
589            maybe_result.unwrap_unchecked()
590        }
591        #[cfg(not(feature = "unsafe-return-conversion"))]
592        maybe_result.unwrap()
593    }
594
595    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
596        match val {
597            0 => Some(Ok(())),
598            -1 => Some(Err(RenewErrorCode::NotOwner)),
599            -4 => Some(Err(RenewErrorCode::Busy)),
600            -7 => Some(Err(RenewErrorCode::InvalidTarget)),
601            -9 => Some(Err(RenewErrorCode::NotInRange)),
602            _ => None,
603        }
604    }
605}
606
607impl fmt::Display for RenewErrorCode {
608    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
609        let msg: &'static str = match self {
610            RenewErrorCode::NotOwner => "you are not the owner of this creep",
611            RenewErrorCode::Busy => "the power creep is not spawned in the world",
612            RenewErrorCode::InvalidTarget => "the target is not a valid power bank object",
613            RenewErrorCode::NotInRange => "the target is too far away",
614        };
615
616        write!(f, "{}", msg)
617    }
618}
619
620impl Error for RenewErrorCode {}
621
622impl From<RenewErrorCode> for ErrorCode {
623    fn from(value: RenewErrorCode) -> Self {
624        // Safety: RenewErrorCode is repr(i8), so we can cast it to get the discriminant
625        // value, which will match the raw return code value that ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
626        // Safety: RenewErrorCode discriminants are always error code values, and thus
627        // the Result returned here will always be an `Err` variant, so we can always
628        // extract the error without panicking
629        Self::result_from_i8(value as i8).unwrap_err()
630    }
631}
632
633/// Error codes used by
634/// [AccountPowerCreep::spawn](crate::AccountPowerCreep::spawn).
635///
636/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.spawn).
637///
638/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L162)
639#[derive(
640    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
641)]
642#[repr(i8)]
643pub enum SpawnErrorCode {
644    NotOwner = -1,
645    Busy = -4,
646    InvalidTarget = -7,
647    Tired = -11,
648    RclNotEnough = -14,
649}
650
651impl FromReturnCode for SpawnErrorCode {
652    type Error = Self;
653
654    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
655        let maybe_result = Self::try_result_from_i8(val);
656        #[cfg(feature = "unsafe-return-conversion")]
657        unsafe {
658            maybe_result.unwrap_unchecked()
659        }
660        #[cfg(not(feature = "unsafe-return-conversion"))]
661        maybe_result.unwrap()
662    }
663
664    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
665        match val {
666            0 => Some(Ok(())),
667            -1 => Some(Err(SpawnErrorCode::NotOwner)),
668            -4 => Some(Err(SpawnErrorCode::Busy)),
669            -7 => Some(Err(SpawnErrorCode::InvalidTarget)),
670            -11 => Some(Err(SpawnErrorCode::Tired)),
671            -14 => Some(Err(SpawnErrorCode::RclNotEnough)),
672            _ => None,
673        }
674    }
675}
676
677impl fmt::Display for SpawnErrorCode {
678    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
679        let msg: &'static str = match self {
680            SpawnErrorCode::NotOwner => "you are not the owner of the creep or the spawn",
681            SpawnErrorCode::Busy => "the power creep is already spawned in the world",
682            SpawnErrorCode::InvalidTarget => "the specified object is not a power spawn",
683            SpawnErrorCode::Tired => "the power creep cannot be spawned because of the cooldown",
684            SpawnErrorCode::RclNotEnough => "room controller level insufficient to use the spawn",
685        };
686
687        write!(f, "{}", msg)
688    }
689}
690
691impl Error for SpawnErrorCode {}
692
693impl From<SpawnErrorCode> for ErrorCode {
694    fn from(value: SpawnErrorCode) -> Self {
695        // Safety: SpawnErrorCode is repr(i8), so we can cast it to get the discriminant
696        // value, which will match the raw return code value that ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
697        // Safety: SpawnErrorCode discriminants are always error code values, and thus
698        // the Result returned here will always be an `Err` variant, so we can always
699        // extract the error without panicking
700        Self::result_from_i8(value as i8).unwrap_err()
701    }
702}
703
704/// Error codes used by
705/// [AccountPowerCreep::upgrade](crate::AccountPowerCreep::upgrade).
706///
707/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.upgrade).
708///
709/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L217)
710#[derive(
711    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
712)]
713#[repr(i8)]
714pub enum UpgradeErrorCode {
715    NotOwner = -1,
716    NotEnoughResources = -6,
717    Full = -8,
718    InvalidArgs = -10,
719}
720
721impl FromReturnCode for UpgradeErrorCode {
722    type Error = Self;
723
724    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
725        let maybe_result = Self::try_result_from_i8(val);
726        #[cfg(feature = "unsafe-return-conversion")]
727        unsafe {
728            maybe_result.unwrap_unchecked()
729        }
730        #[cfg(not(feature = "unsafe-return-conversion"))]
731        maybe_result.unwrap()
732    }
733
734    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
735        match val {
736            0 => Some(Ok(())),
737            -1 => Some(Err(UpgradeErrorCode::NotOwner)),
738            -6 => Some(Err(UpgradeErrorCode::NotEnoughResources)),
739            -8 => Some(Err(UpgradeErrorCode::Full)),
740            -10 => Some(Err(UpgradeErrorCode::InvalidArgs)),
741            _ => None,
742        }
743    }
744}
745
746impl fmt::Display for UpgradeErrorCode {
747    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
748        let msg: &'static str = match self {
749            UpgradeErrorCode::NotOwner => "you are not the owner of the creep",
750            UpgradeErrorCode::NotEnoughResources => "you account power level is not enough",
751            UpgradeErrorCode::Full => "the specified power cannot be upgraded on this creep's level, or the creep reached the maximum level",
752            UpgradeErrorCode::InvalidArgs => "the specified power id is not valid",
753        };
754
755        write!(f, "{}", msg)
756    }
757}
758
759impl Error for UpgradeErrorCode {}
760
761impl From<UpgradeErrorCode> for ErrorCode {
762    fn from(value: UpgradeErrorCode) -> Self {
763        // Safety: UpgradeErrorCode is repr(i8), so we can cast it to get the
764        // discriminant value, which will match the raw return code value that ErrorCode
765        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
766        // Safety: UpgradeErrorCode discriminants are always error code values, and thus
767        // the Result returned here will always be an `Err` variant, so we can always
768        // extract the error without panicking
769        Self::result_from_i8(value as i8).unwrap_err()
770    }
771}
772
773/// Error codes used by [PowerCreep::use_power](crate::PowerCreep::use_power).
774///
775/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.usePower).
776///
777/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L246)
778#[derive(
779    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
780)]
781#[repr(i8)]
782pub enum UsePowerErrorCode {
783    NotOwner = -1,
784    Busy = -4,
785    NotEnoughResources = -6,
786    InvalidTarget = -7,
787    Full = -8,
788    NotInRange = -9,
789    InvalidArgs = -10,
790    Tired = -11,
791    NoBodypart = -12,
792}
793
794impl FromReturnCode for UsePowerErrorCode {
795    type Error = Self;
796
797    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
798        let maybe_result = Self::try_result_from_i8(val);
799        #[cfg(feature = "unsafe-return-conversion")]
800        unsafe {
801            maybe_result.unwrap_unchecked()
802        }
803        #[cfg(not(feature = "unsafe-return-conversion"))]
804        maybe_result.unwrap()
805    }
806
807    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
808        match val {
809            0 => Some(Ok(())),
810            -1 => Some(Err(UsePowerErrorCode::NotOwner)),
811            -4 => Some(Err(UsePowerErrorCode::Busy)),
812            -6 => Some(Err(UsePowerErrorCode::NotEnoughResources)),
813            -7 => Some(Err(UsePowerErrorCode::InvalidTarget)),
814            -8 => Some(Err(UsePowerErrorCode::Full)),
815            -9 => Some(Err(UsePowerErrorCode::NotInRange)),
816            -10 => Some(Err(UsePowerErrorCode::InvalidArgs)),
817            -11 => Some(Err(UsePowerErrorCode::Tired)),
818            -12 => Some(Err(UsePowerErrorCode::NoBodypart)),
819            _ => None,
820        }
821    }
822}
823
824impl fmt::Display for UsePowerErrorCode {
825    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
826        let msg: &'static str = match self {
827            UsePowerErrorCode::NotOwner => "you are not the owner of the creep",
828            UsePowerErrorCode::Busy => "the creep is not spawned in the world",
829            UsePowerErrorCode::NotEnoughResources => {
830                "the creep doesn't have enough resources to use the power"
831            }
832            UsePowerErrorCode::InvalidTarget => "the specified target is not valid",
833            UsePowerErrorCode::Full => "the target has the same active effect of a higher level",
834            UsePowerErrorCode::NotInRange => "the specified target is too far away",
835            UsePowerErrorCode::InvalidArgs => "using powers is not enabled on the room controller",
836            UsePowerErrorCode::Tired => "the power ability is still on cooldown",
837            UsePowerErrorCode::NoBodypart => "the creep doesn't have the specified power ability",
838        };
839
840        write!(f, "{}", msg)
841    }
842}
843
844impl Error for UsePowerErrorCode {}
845
846impl From<UsePowerErrorCode> for ErrorCode {
847    fn from(value: UsePowerErrorCode) -> Self {
848        // Safety: UsePowerErrorCode is repr(i8), so we can cast it to get the
849        // discriminant value, which will match the raw return code value that ErrorCode
850        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
851        // Safety: UsePowerErrorCode discriminants are always error code values, and
852        // thus the Result returned here will always be an `Err` variant, so we can
853        // always extract the error without panicking
854        Self::result_from_i8(value as i8).unwrap_err()
855    }
856}