screeps-game-api 0.23.3

WASM bindings to the in-game Screeps API
Documentation
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
use std::{error::Error, fmt};

use num_derive::FromPrimitive;
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::{constants::ErrorCode, FromReturnCode};

/// Error codes used by [PowerCreep::create](crate::PowerCreep::create).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.create).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L395)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum PowerCreepCreateErrorCode {
    NameExists = -3,
    NotEnoughResources = -6,
    InvalidArgs = -10,
}

impl FromReturnCode for PowerCreepCreateErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -3 => Some(Err(PowerCreepCreateErrorCode::NameExists)),
            -6 => Some(Err(PowerCreepCreateErrorCode::NotEnoughResources)),
            -10 => Some(Err(PowerCreepCreateErrorCode::InvalidArgs)),
            _ => None,
        }
    }
}

impl fmt::Display for PowerCreepCreateErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            PowerCreepCreateErrorCode::NameExists => "a power creep with the specified name already exists",
            PowerCreepCreateErrorCode::NotEnoughResources => "you don't have free power levels in your account",
            PowerCreepCreateErrorCode::InvalidArgs => "the provided power creep name is exceeds the limit, or the power creep class is invalid",
        };

        write!(f, "{}", msg)
    }
}

impl Error for PowerCreepCreateErrorCode {}

impl From<PowerCreepCreateErrorCode> for ErrorCode {
    fn from(value: PowerCreepCreateErrorCode) -> Self {
        // Safety: PowerCreepCreateErrorCode is repr(i8), so we can cast it to get the
        // discriminant 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
        // Safety: PowerCreepCreateErrorCode discriminants are always error code values,
        // and thus the Result returned here will always be an `Err` variant, so we can
        // always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [PowerCreep::cancel_order](crate::PowerCreep::cancel_order).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.cancelOrder).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L342)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum PowerCreepCancelOrderErrorCode {
    NotOwner = -1,
    Busy = -4,
    NotFound = -5,
}

impl FromReturnCode for PowerCreepCancelOrderErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(PowerCreepCancelOrderErrorCode::NotOwner)),
            -4 => Some(Err(PowerCreepCancelOrderErrorCode::Busy)),
            -5 => Some(Err(PowerCreepCancelOrderErrorCode::NotFound)),
            _ => None,
        }
    }
}

impl fmt::Display for PowerCreepCancelOrderErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            PowerCreepCancelOrderErrorCode::NotOwner => "you are not the owner of the creep",
            PowerCreepCancelOrderErrorCode::Busy => "the power creep is not spawned in the world",
            PowerCreepCancelOrderErrorCode::NotFound => {
                "the order with the specified name is not found"
            }
        };

        write!(f, "{}", msg)
    }
}

impl Error for PowerCreepCancelOrderErrorCode {}

impl From<PowerCreepCancelOrderErrorCode> for ErrorCode {
    fn from(value: PowerCreepCancelOrderErrorCode) -> Self {
        // Safety: PowerCreepCancelOrderErrorCode is repr(i8), so we can cast it to get
        // the discriminant 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
        // Safety: PowerCreepCancelOrderErrorCode discriminants are always error code
        // values, and thus the Result returned here will always be an `Err` variant, so
        // we can always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [AccountPowerCreep::delete](crate::AccountPowerCreep::delete).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.delete).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L204)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum DeleteErrorCode {
    NotOwner = -1,
    Busy = -4,
}

impl FromReturnCode for DeleteErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(DeleteErrorCode::NotOwner)),
            -4 => Some(Err(DeleteErrorCode::Busy)),
            _ => None,
        }
    }
}

impl fmt::Display for DeleteErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            DeleteErrorCode::NotOwner => "you are not the owner of the creep",
            DeleteErrorCode::Busy => "the power creep is spawned in the world",
        };

        write!(f, "{}", msg)
    }
}

impl Error for DeleteErrorCode {}

impl From<DeleteErrorCode> for ErrorCode {
    fn from(value: DeleteErrorCode) -> Self {
        // Safety: DeleteErrorCode is repr(i8), so we can cast it to get the
        // discriminant 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
        // Safety: DeleteErrorCode discriminants are always error code values, and thus
        // the Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [PowerCreep::enable_room](crate::PowerCreep::enable_room).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.enableRoom).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L295)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum EnableRoomErrorCode {
    NotOwner = -1,
    Busy = -4,
    InvalidTarget = -7,
    NotInRange = -9,
}

impl FromReturnCode for EnableRoomErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(EnableRoomErrorCode::NotOwner)),
            -4 => Some(Err(EnableRoomErrorCode::Busy)),
            -7 => Some(Err(EnableRoomErrorCode::InvalidTarget)),
            -9 => Some(Err(EnableRoomErrorCode::NotInRange)),
            _ => None,
        }
    }
}

impl fmt::Display for EnableRoomErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            EnableRoomErrorCode::NotOwner => "you are not the owner of this creep",
            EnableRoomErrorCode::Busy => "the power creep is not spawned in the world",
            EnableRoomErrorCode::InvalidTarget => "the target is not a controller structure",
            EnableRoomErrorCode::NotInRange => "the target is too far away",
        };

        write!(f, "{}", msg)
    }
}

impl Error for EnableRoomErrorCode {}

impl From<EnableRoomErrorCode> for ErrorCode {
    fn from(value: EnableRoomErrorCode) -> Self {
        // Safety: EnableRoomErrorCode is repr(i8), so we can cast it to get the
        // discriminant 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
        // Safety: EnableRoomErrorCode discriminants are always error code values, and
        // thus the Result returned here will always be an `Err` variant, so we can
        // always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [PowerCreep::move_direction](crate::PowerCreep::move_direction).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.move).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L106)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum PowerCreepMoveDirectionErrorCode {
    NotOwner = -1,
    Busy = -4,
    NotInRange = -9,
    InvalidArgs = -10,
    Tired = -11,
}

impl FromReturnCode for PowerCreepMoveDirectionErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(PowerCreepMoveDirectionErrorCode::NotOwner)),
            -4 => Some(Err(PowerCreepMoveDirectionErrorCode::Busy)),
            -9 => Some(Err(PowerCreepMoveDirectionErrorCode::NotInRange)),
            -10 => Some(Err(PowerCreepMoveDirectionErrorCode::InvalidArgs)),
            -11 => Some(Err(PowerCreepMoveDirectionErrorCode::Tired)),
            _ => None,
        }
    }
}

impl fmt::Display for PowerCreepMoveDirectionErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            PowerCreepMoveDirectionErrorCode::NotOwner => "you are not the owner of this creep",
            PowerCreepMoveDirectionErrorCode::Busy => "the power creep is not spawned in the world",
            PowerCreepMoveDirectionErrorCode::NotInRange => "the target creep is too far away",
            PowerCreepMoveDirectionErrorCode::InvalidArgs => "the provided direction is incorrect",
            PowerCreepMoveDirectionErrorCode::Tired => {
                "the fatigue indicator of the creep is non-zero"
            }
        };

        write!(f, "{}", msg)
    }
}

impl Error for PowerCreepMoveDirectionErrorCode {}

impl From<PowerCreepMoveDirectionErrorCode> for ErrorCode {
    fn from(value: PowerCreepMoveDirectionErrorCode) -> Self {
        // Safety: PowerCreepMoveDirectionErrorCode is repr(i8), so we can cast it to
        // get the discriminant 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
        // Safety: PowerCreepMoveDirectionErrorCode discriminants are always error code
        // values, and thus the Result returned here will always be an `Err` variant, so
        // we can always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [PowerCreep::move_by_path](crate::PowerCreep::move_by_path).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.moveByPath).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L120)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum PowerCreepMoveByPathErrorCode {
    NotOwner = -1,
    Busy = -4,
    NotFound = -5,
    InvalidArgs = -10,
    Tired = -11,
}

impl FromReturnCode for PowerCreepMoveByPathErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(PowerCreepMoveByPathErrorCode::NotOwner)),
            -4 => Some(Err(PowerCreepMoveByPathErrorCode::Busy)),
            -5 => Some(Err(PowerCreepMoveByPathErrorCode::NotFound)),
            -10 => Some(Err(PowerCreepMoveByPathErrorCode::InvalidArgs)),
            -11 => Some(Err(PowerCreepMoveByPathErrorCode::Tired)),
            _ => None,
        }
    }
}

impl fmt::Display for PowerCreepMoveByPathErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            PowerCreepMoveByPathErrorCode::NotOwner => "you are not the owner of this creep",
            PowerCreepMoveByPathErrorCode::Busy => "the power creep is not spawned in the world",
            PowerCreepMoveByPathErrorCode::NotFound => {
                "the specified path doesn't match the creep's location"
            }
            PowerCreepMoveByPathErrorCode::InvalidArgs => "path is not a valid path array",
            PowerCreepMoveByPathErrorCode::Tired => {
                "the fatigue indicator of the creep is non-zero"
            }
        };

        write!(f, "{}", msg)
    }
}

impl Error for PowerCreepMoveByPathErrorCode {}

impl From<PowerCreepMoveByPathErrorCode> for ErrorCode {
    fn from(value: PowerCreepMoveByPathErrorCode) -> Self {
        // Safety: PowerCreepMoveByPathErrorCode is repr(i8), so we can cast it to get
        // the discriminant 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
        // Safety: PowerCreepMoveByPathErrorCode discriminants are always error code
        // values, and thus the Result returned here will always be an `Err` variant, so
        // we can always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by [PowerCreep::move_to](crate::PowerCreep#method.move_to).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.moveTo).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L113)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum PowerCreepMoveToErrorCode {
    NotOwner = -1,
    NoPath = -2,
    Busy = -4,
    NotFound = -5,
    InvalidTarget = -7,
    Tired = -11,
}

impl FromReturnCode for PowerCreepMoveToErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(PowerCreepMoveToErrorCode::NotOwner)),
            -2 => Some(Err(PowerCreepMoveToErrorCode::NoPath)),
            -4 => Some(Err(PowerCreepMoveToErrorCode::Busy)),
            -5 => Some(Err(PowerCreepMoveToErrorCode::NotFound)),
            -7 => Some(Err(PowerCreepMoveToErrorCode::InvalidTarget)),
            -11 => Some(Err(PowerCreepMoveToErrorCode::Tired)),
            _ => None,
        }
    }
}

impl fmt::Display for PowerCreepMoveToErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            PowerCreepMoveToErrorCode::NotOwner => "you are not the owner of this creep",
            PowerCreepMoveToErrorCode::NoPath => "no path to the target could be found",
            PowerCreepMoveToErrorCode::Busy => "the power creep is not spawned in the world",
            PowerCreepMoveToErrorCode::NotFound => "the creep has no memorized path to reuse",
            PowerCreepMoveToErrorCode::InvalidTarget => "the target provided is invalid",
            PowerCreepMoveToErrorCode::Tired => "the fatigue indicator of the creep is non-zero",
        };

        write!(f, "{}", msg)
    }
}

impl Error for PowerCreepMoveToErrorCode {}

impl From<PowerCreepMoveToErrorCode> for ErrorCode {
    fn from(value: PowerCreepMoveToErrorCode) -> Self {
        // Safety: PowerCreepMoveToErrorCode is repr(i8), so we can cast it to get the
        // discriminant 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
        // Safety: PowerCreepMoveToErrorCode discriminants are always error code values,
        // and thus the Result returned here will always be an `Err` variant, so we can
        // always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [AccountPowerCreep::rename](crate::AccountPowerCreep::rename).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.rename).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L356)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum RenameErrorCode {
    NotOwner = -1,
    NameExists = -3,
    Busy = -4,
    InvalidArgs = -10,
}

impl FromReturnCode for RenameErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(RenameErrorCode::NotOwner)),
            -3 => Some(Err(RenameErrorCode::NameExists)),
            -4 => Some(Err(RenameErrorCode::Busy)),
            -10 => Some(Err(RenameErrorCode::InvalidArgs)),
            _ => None,
        }
    }
}

impl fmt::Display for RenameErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            RenameErrorCode::NotOwner => "you are not the owner of the creep",
            RenameErrorCode::NameExists => "a power creep with the specified name already exists",
            RenameErrorCode::Busy => "the power creep is spawned in the world",
            RenameErrorCode::InvalidArgs => "the provided power creep name is exceeds the limit",
        };

        write!(f, "{}", msg)
    }
}

impl Error for RenameErrorCode {}

impl From<RenameErrorCode> for ErrorCode {
    fn from(value: RenameErrorCode) -> Self {
        // Safety: RenameErrorCode is repr(i8), so we can cast it to get the
        // discriminant 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
        // Safety: RenameErrorCode discriminants are always error code values, and thus
        // the Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by [PowerCreep::renew](crate::PowerCreep::renew).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.renew).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L319)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum RenewErrorCode {
    NotOwner = -1,
    Busy = -4,
    InvalidTarget = -7,
    NotInRange = -9,
}

impl FromReturnCode for RenewErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(RenewErrorCode::NotOwner)),
            -4 => Some(Err(RenewErrorCode::Busy)),
            -7 => Some(Err(RenewErrorCode::InvalidTarget)),
            -9 => Some(Err(RenewErrorCode::NotInRange)),
            _ => None,
        }
    }
}

impl fmt::Display for RenewErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            RenewErrorCode::NotOwner => "you are not the owner of this creep",
            RenewErrorCode::Busy => "the power creep is not spawned in the world",
            RenewErrorCode::InvalidTarget => "the target is not a valid power bank object",
            RenewErrorCode::NotInRange => "the target is too far away",
        };

        write!(f, "{}", msg)
    }
}

impl Error for RenewErrorCode {}

impl From<RenewErrorCode> for ErrorCode {
    fn from(value: RenewErrorCode) -> Self {
        // Safety: RenewErrorCode is repr(i8), so we can cast it to get the discriminant
        // 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
        // Safety: RenewErrorCode discriminants are always error code values, and thus
        // the Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [AccountPowerCreep::spawn](crate::AccountPowerCreep::spawn).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.spawn).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L162)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum SpawnErrorCode {
    NotOwner = -1,
    Busy = -4,
    InvalidTarget = -7,
    Tired = -11,
    RclNotEnough = -14,
}

impl FromReturnCode for SpawnErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(SpawnErrorCode::NotOwner)),
            -4 => Some(Err(SpawnErrorCode::Busy)),
            -7 => Some(Err(SpawnErrorCode::InvalidTarget)),
            -11 => Some(Err(SpawnErrorCode::Tired)),
            -14 => Some(Err(SpawnErrorCode::RclNotEnough)),
            _ => None,
        }
    }
}

impl fmt::Display for SpawnErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            SpawnErrorCode::NotOwner => "you are not the owner of the creep or the spawn",
            SpawnErrorCode::Busy => "the power creep is already spawned in the world",
            SpawnErrorCode::InvalidTarget => "the specified object is not a power spawn",
            SpawnErrorCode::Tired => "the power creep cannot be spawned because of the cooldown",
            SpawnErrorCode::RclNotEnough => "room controller level insufficient to use the spawn",
        };

        write!(f, "{}", msg)
    }
}

impl Error for SpawnErrorCode {}

impl From<SpawnErrorCode> for ErrorCode {
    fn from(value: SpawnErrorCode) -> Self {
        // Safety: SpawnErrorCode is repr(i8), so we can cast it to get the discriminant
        // 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
        // Safety: SpawnErrorCode discriminants are always error code values, and thus
        // the Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [AccountPowerCreep::upgrade](crate::AccountPowerCreep::upgrade).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.upgrade).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L217)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum UpgradeErrorCode {
    NotOwner = -1,
    NotEnoughResources = -6,
    Full = -8,
    InvalidArgs = -10,
}

impl FromReturnCode for UpgradeErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(UpgradeErrorCode::NotOwner)),
            -6 => Some(Err(UpgradeErrorCode::NotEnoughResources)),
            -8 => Some(Err(UpgradeErrorCode::Full)),
            -10 => Some(Err(UpgradeErrorCode::InvalidArgs)),
            _ => None,
        }
    }
}

impl fmt::Display for UpgradeErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            UpgradeErrorCode::NotOwner => "you are not the owner of the creep",
            UpgradeErrorCode::NotEnoughResources => "you account power level is not enough",
            UpgradeErrorCode::Full => "the specified power cannot be upgraded on this creep's level, or the creep reached the maximum level",
            UpgradeErrorCode::InvalidArgs => "the specified power id is not valid",
        };

        write!(f, "{}", msg)
    }
}

impl Error for UpgradeErrorCode {}

impl From<UpgradeErrorCode> for ErrorCode {
    fn from(value: UpgradeErrorCode) -> Self {
        // Safety: UpgradeErrorCode is repr(i8), so we can cast it to get the
        // discriminant 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
        // Safety: UpgradeErrorCode discriminants are always error code values, and thus
        // the Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by [PowerCreep::use_power](crate::PowerCreep::use_power).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#PowerCreep.usePower).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/power-creeps.js#L246)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum UsePowerErrorCode {
    NotOwner = -1,
    Busy = -4,
    NotEnoughResources = -6,
    InvalidTarget = -7,
    Full = -8,
    NotInRange = -9,
    InvalidArgs = -10,
    Tired = -11,
    NoBodypart = -12,
}

impl FromReturnCode for UsePowerErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(UsePowerErrorCode::NotOwner)),
            -4 => Some(Err(UsePowerErrorCode::Busy)),
            -6 => Some(Err(UsePowerErrorCode::NotEnoughResources)),
            -7 => Some(Err(UsePowerErrorCode::InvalidTarget)),
            -8 => Some(Err(UsePowerErrorCode::Full)),
            -9 => Some(Err(UsePowerErrorCode::NotInRange)),
            -10 => Some(Err(UsePowerErrorCode::InvalidArgs)),
            -11 => Some(Err(UsePowerErrorCode::Tired)),
            -12 => Some(Err(UsePowerErrorCode::NoBodypart)),
            _ => None,
        }
    }
}

impl fmt::Display for UsePowerErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            UsePowerErrorCode::NotOwner => "you are not the owner of the creep",
            UsePowerErrorCode::Busy => "the creep is not spawned in the world",
            UsePowerErrorCode::NotEnoughResources => {
                "the creep doesn't have enough resources to use the power"
            }
            UsePowerErrorCode::InvalidTarget => "the specified target is not valid",
            UsePowerErrorCode::Full => "the target has the same active effect of a higher level",
            UsePowerErrorCode::NotInRange => "the specified target is too far away",
            UsePowerErrorCode::InvalidArgs => "using powers is not enabled on the room controller",
            UsePowerErrorCode::Tired => "the power ability is still on cooldown",
            UsePowerErrorCode::NoBodypart => "the creep doesn't have the specified power ability",
        };

        write!(f, "{}", msg)
    }
}

impl Error for UsePowerErrorCode {}

impl From<UsePowerErrorCode> for ErrorCode {
    fn from(value: UsePowerErrorCode) -> Self {
        // Safety: UsePowerErrorCode is repr(i8), so we can cast it to get the
        // discriminant 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
        // Safety: UsePowerErrorCode discriminants are always error code values, and
        // thus the Result returned here will always be an `Err` variant, so we can
        // always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}