screeps/enums/action_error_codes/
structurecontroller_error_codes.rs1use 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(
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 Self::result_from_i8(value as i8).unwrap_err()
75 }
76}
77
78#[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 Self::result_from_i8(value as i8).unwrap_err()
135 }
136}