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