screeps/enums/action_error_codes/
spawning_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 [Spawning::cancel](crate::Spawning::cancel).
9///
10/// [Screeps API Docs](https://docs.screeps.com/api/#StructureSpawn.Spawning.cancel).
11///
12/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L1328)
13#[derive(
14    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
15)]
16#[repr(i8)]
17pub enum CancelErrorCode {
18    NotOwner = -1,
19}
20
21impl FromReturnCode for CancelErrorCode {
22    type Error = Self;
23
24    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
25        let maybe_result = Self::try_result_from_i8(val);
26        #[cfg(feature = "unsafe-return-conversion")]
27        unsafe {
28            maybe_result.unwrap_unchecked()
29        }
30        #[cfg(not(feature = "unsafe-return-conversion"))]
31        maybe_result.unwrap()
32    }
33
34    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
35        match val {
36            0 => Some(Ok(())),
37            -1 => Some(Err(CancelErrorCode::NotOwner)),
38            _ => None,
39        }
40    }
41}
42
43impl fmt::Display for CancelErrorCode {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        let msg: &'static str = match self {
46            CancelErrorCode::NotOwner => "you are not the owner of this spawn",
47        };
48
49        write!(f, "{}", msg)
50    }
51}
52
53impl Error for CancelErrorCode {}
54
55impl From<CancelErrorCode> for ErrorCode {
56    fn from(value: CancelErrorCode) -> Self {
57        // Safety: CancelErrorCode is repr(i8), so we can cast it to get the
58        // discriminant value, which will match the raw return code value that ErrorCode
59        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
60        // Safety: CancelErrorCode discriminants are always error code values, and thus
61        // the Result returned here will always be an `Err` variant, so we can always
62        // extract the error without panicking
63        Self::result_from_i8(value as i8).unwrap_err()
64    }
65}
66
67/// Error codes used by
68/// [Spawning::set_directions](crate::Spawning::set_directions).
69///
70/// [Screeps API Docs](https://docs.screeps.com/api/#StructureSpawn.Spawning.setDirections).
71///
72/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L1312)
73#[derive(
74    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
75)]
76#[repr(i8)]
77pub enum SetDirectionsErrorCode {
78    NotOwner = -1,
79    InvalidArgs = -10,
80}
81
82impl FromReturnCode for SetDirectionsErrorCode {
83    type Error = Self;
84
85    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
86        let maybe_result = Self::try_result_from_i8(val);
87        #[cfg(feature = "unsafe-return-conversion")]
88        unsafe {
89            maybe_result.unwrap_unchecked()
90        }
91        #[cfg(not(feature = "unsafe-return-conversion"))]
92        maybe_result.unwrap()
93    }
94
95    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
96        match val {
97            0 => Some(Ok(())),
98            -1 => Some(Err(SetDirectionsErrorCode::NotOwner)),
99            -10 => Some(Err(SetDirectionsErrorCode::InvalidArgs)),
100            _ => None,
101        }
102    }
103}
104
105impl fmt::Display for SetDirectionsErrorCode {
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        let msg: &'static str = match self {
108            SetDirectionsErrorCode::NotOwner => "you are not the owner of this spawn",
109            SetDirectionsErrorCode::InvalidArgs => "the directions is array is invalid",
110        };
111
112        write!(f, "{}", msg)
113    }
114}
115
116impl Error for SetDirectionsErrorCode {}
117
118impl From<SetDirectionsErrorCode> for ErrorCode {
119    fn from(value: SetDirectionsErrorCode) -> Self {
120        // Safety: SetDirectionsErrorCode is repr(i8), so we can cast it to get the
121        // discriminant value, which will match the raw return code value that ErrorCode
122        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
123        // Safety: SetDirectionsErrorCode discriminants are always error code values,
124        // and thus the Result returned here will always be an `Err` variant, so we can
125        // always extract the error without panicking
126        Self::result_from_i8(value as i8).unwrap_err()
127    }
128}