screeps/enums/action_error_codes/
structuretower_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(
14 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
15)]
16#[repr(i8)]
17pub enum TowerAttackErrorCode {
18 NotOwner = -1,
19 NotEnoughEnergy = -6,
20 InvalidTarget = -7,
21 RclNotEnough = -14,
22}
23
24impl FromReturnCode for TowerAttackErrorCode {
25 type Error = Self;
26
27 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
28 let maybe_result = Self::try_result_from_i8(val);
29 #[cfg(feature = "unsafe-return-conversion")]
30 unsafe {
31 maybe_result.unwrap_unchecked()
32 }
33 #[cfg(not(feature = "unsafe-return-conversion"))]
34 maybe_result.unwrap()
35 }
36
37 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
38 match val {
39 0 => Some(Ok(())),
40 -1 => Some(Err(TowerAttackErrorCode::NotOwner)),
41 -6 => Some(Err(TowerAttackErrorCode::NotEnoughEnergy)),
42 -7 => Some(Err(TowerAttackErrorCode::InvalidTarget)),
43 -14 => Some(Err(TowerAttackErrorCode::RclNotEnough)),
44 _ => None,
45 }
46 }
47}
48
49impl fmt::Display for TowerAttackErrorCode {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 let msg: &'static str = match self {
52 TowerAttackErrorCode::NotOwner => "you are not the owner of this structure",
53 TowerAttackErrorCode::NotEnoughEnergy => "the tower does not have enough energy",
54 TowerAttackErrorCode::InvalidTarget => "the target is not a valid attackable object",
55 TowerAttackErrorCode::RclNotEnough => {
56 "room controller level insufficient to use this structure"
57 }
58 };
59
60 write!(f, "{}", msg)
61 }
62}
63
64impl Error for TowerAttackErrorCode {}
65
66impl From<TowerAttackErrorCode> for ErrorCode {
67 fn from(value: TowerAttackErrorCode) -> Self {
68 Self::result_from_i8(value as i8).unwrap_err()
75 }
76}
77
78#[derive(
84 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
85)]
86#[repr(i8)]
87pub enum TowerHealErrorCode {
88 NotOwner = -1,
89 NotEnoughEnergy = -6,
90 InvalidTarget = -7,
91 RclNotEnough = -14,
92}
93
94impl FromReturnCode for TowerHealErrorCode {
95 type Error = Self;
96
97 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
98 let maybe_result = Self::try_result_from_i8(val);
99 #[cfg(feature = "unsafe-return-conversion")]
100 unsafe {
101 maybe_result.unwrap_unchecked()
102 }
103 #[cfg(not(feature = "unsafe-return-conversion"))]
104 maybe_result.unwrap()
105 }
106
107 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
108 match val {
109 0 => Some(Ok(())),
110 -1 => Some(Err(TowerHealErrorCode::NotOwner)),
111 -6 => Some(Err(TowerHealErrorCode::NotEnoughEnergy)),
112 -7 => Some(Err(TowerHealErrorCode::InvalidTarget)),
113 -14 => Some(Err(TowerHealErrorCode::RclNotEnough)),
114 _ => None,
115 }
116 }
117}
118
119impl fmt::Display for TowerHealErrorCode {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 let msg: &'static str = match self {
122 TowerHealErrorCode::NotOwner => "you are not the owner of this structure",
123 TowerHealErrorCode::NotEnoughEnergy => "the tower does not have enough energy",
124 TowerHealErrorCode::InvalidTarget => "the target is not a valid creep object",
125 TowerHealErrorCode::RclNotEnough => {
126 "room controller level insufficient to use this structure"
127 }
128 };
129
130 write!(f, "{}", msg)
131 }
132}
133
134impl Error for TowerHealErrorCode {}
135
136impl From<TowerHealErrorCode> for ErrorCode {
137 fn from(value: TowerHealErrorCode) -> Self {
138 Self::result_from_i8(value as i8).unwrap_err()
145 }
146}
147
148#[derive(
154 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
155)]
156#[repr(i8)]
157pub enum TowerRepairErrorCode {
158 NotOwner = -1,
159 NotEnoughEnergy = -6,
160 InvalidTarget = -7,
161 RclNotEnough = -14,
162}
163
164impl FromReturnCode for TowerRepairErrorCode {
165 type Error = Self;
166
167 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
168 let maybe_result = Self::try_result_from_i8(val);
169 #[cfg(feature = "unsafe-return-conversion")]
170 unsafe {
171 maybe_result.unwrap_unchecked()
172 }
173 #[cfg(not(feature = "unsafe-return-conversion"))]
174 maybe_result.unwrap()
175 }
176
177 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
178 match val {
179 0 => Some(Ok(())),
180 -1 => Some(Err(TowerRepairErrorCode::NotOwner)),
181 -6 => Some(Err(TowerRepairErrorCode::NotEnoughEnergy)),
182 -7 => Some(Err(TowerRepairErrorCode::InvalidTarget)),
183 -14 => Some(Err(TowerRepairErrorCode::RclNotEnough)),
184 _ => None,
185 }
186 }
187}
188
189impl fmt::Display for TowerRepairErrorCode {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 let msg: &'static str = match self {
192 TowerRepairErrorCode::NotOwner => "you are not the owner of this structure",
193 TowerRepairErrorCode::NotEnoughEnergy => "the tower does not have enough energy",
194 TowerRepairErrorCode::InvalidTarget => "the target is not a valid repairable object",
195 TowerRepairErrorCode::RclNotEnough => {
196 "room controller level insufficient to use this structure"
197 }
198 };
199
200 write!(f, "{}", msg)
201 }
202}
203
204impl Error for TowerRepairErrorCode {}
205
206impl From<TowerRepairErrorCode> for ErrorCode {
207 fn from(value: TowerRepairErrorCode) -> Self {
208 Self::result_from_i8(value as i8).unwrap_err()
215 }
216}