screeps/enums/action_error_codes/
room_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(
15 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
16)]
17#[repr(i8)]
18pub enum RoomCreateConstructionSiteErrorCode {
19 NotOwner = -1,
20 InvalidTarget = -7,
21 Full = -8,
22 InvalidArgs = -10,
23 RclNotEnough = -14,
24}
25
26impl FromReturnCode for RoomCreateConstructionSiteErrorCode {
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(RoomCreateConstructionSiteErrorCode::NotOwner)),
43 -7 => Some(Err(RoomCreateConstructionSiteErrorCode::InvalidTarget)),
44 -8 => Some(Err(RoomCreateConstructionSiteErrorCode::Full)),
45 -10 => Some(Err(RoomCreateConstructionSiteErrorCode::InvalidArgs)),
46 -14 => Some(Err(RoomCreateConstructionSiteErrorCode::RclNotEnough)),
47 _ => None,
48 }
49 }
50}
51
52impl fmt::Display for RoomCreateConstructionSiteErrorCode {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 let msg: &'static str = match self {
55 RoomCreateConstructionSiteErrorCode::NotOwner => "the room is claimed or reserved by a hostile player",
56 RoomCreateConstructionSiteErrorCode::InvalidTarget => "the structure cannot be placed at the specified location",
57 RoomCreateConstructionSiteErrorCode::Full => "you have too many construction sites. the maximum number of construction sites per player is 100",
58 RoomCreateConstructionSiteErrorCode::InvalidArgs => "the location is incorrect",
59 RoomCreateConstructionSiteErrorCode::RclNotEnough => "room controller level insufficient. learn more",
60 };
61
62 write!(f, "{}", msg)
63 }
64}
65
66impl Error for RoomCreateConstructionSiteErrorCode {}
67
68impl From<RoomCreateConstructionSiteErrorCode> for ErrorCode {
69 fn from(value: RoomCreateConstructionSiteErrorCode) -> Self {
70 Self::result_from_i8(value as i8).unwrap_err()
77 }
78}
79
80#[derive(
86 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
87)]
88#[repr(i8)]
89pub enum RoomCreateFlagErrorCode {
90 NameExists = -3,
91 Full = -8,
92 InvalidArgs = -10,
93}
94
95impl FromReturnCode for RoomCreateFlagErrorCode {
96 type Error = Self;
97
98 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
99 let maybe_result = Self::try_result_from_i8(val);
100 #[cfg(feature = "unsafe-return-conversion")]
101 unsafe {
102 maybe_result.unwrap_unchecked()
103 }
104 #[cfg(not(feature = "unsafe-return-conversion"))]
105 maybe_result.unwrap()
106 }
107
108 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
109 match val {
110 -3 => Some(Err(RoomCreateFlagErrorCode::NameExists)),
111 -8 => Some(Err(RoomCreateFlagErrorCode::Full)),
112 -10 => Some(Err(RoomCreateFlagErrorCode::InvalidArgs)),
113 _ => None,
114 }
115 }
116}
117
118impl fmt::Display for RoomCreateFlagErrorCode {
119 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120 let msg: &'static str = match self {
121 RoomCreateFlagErrorCode::NameExists => "there is a flag with the same name already",
122 RoomCreateFlagErrorCode::Full => {
123 "you have too many flags. the maximum number of flags per player is 10000"
124 }
125 RoomCreateFlagErrorCode::InvalidArgs => {
126 "the location or the name or the color constant is incorrect"
127 }
128 };
129
130 write!(f, "{}", msg)
131 }
132}
133
134impl Error for RoomCreateFlagErrorCode {}
135
136impl From<RoomCreateFlagErrorCode> for ErrorCode {
137 fn from(value: RoomCreateFlagErrorCode) -> 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 FindExitToErrorCode {
158 NoPath = -2,
159 InvalidArgs = -10,
160}
161
162impl FromReturnCode for FindExitToErrorCode {
163 type Error = Self;
164
165 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
166 let maybe_result = Self::try_result_from_i8(val);
167 #[cfg(feature = "unsafe-return-conversion")]
168 unsafe {
169 maybe_result.unwrap_unchecked()
170 }
171 #[cfg(not(feature = "unsafe-return-conversion"))]
172 maybe_result.unwrap()
173 }
174
175 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
176 match val {
177 -2 => Some(Err(FindExitToErrorCode::NoPath)),
178 -10 => Some(Err(FindExitToErrorCode::InvalidArgs)),
179 _ => None,
180 }
181 }
182}
183
184impl fmt::Display for FindExitToErrorCode {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 let msg: &'static str = match self {
187 FindExitToErrorCode::NoPath => "path can not be found",
188 FindExitToErrorCode::InvalidArgs => "the location is incorrect",
189 };
190
191 write!(f, "{}", msg)
192 }
193}
194
195impl Error for FindExitToErrorCode {}
196
197impl From<FindExitToErrorCode> for ErrorCode {
198 fn from(value: FindExitToErrorCode) -> Self {
199 Self::result_from_i8(value as i8).unwrap_err()
206 }
207}